firefox addon sdk page-worker blocks main thread

末鹿安然 提交于 2020-01-17 03:59:16

问题


I'm crawling about 20 Web sites in the background, when a page loads, within the addon script, with the page-worker. Unfortunately the browser freezes, unpredictable during that time.

I tried to use timers.setTimeout(..., 0-400ms) and also tried the example from the wiki

function executeSoon(aFunc) {
    var tm = Cc["@mozilla.org/thread-manager;1"]
        .getService(Ci.nsIThreadManager);

    tm.mainThread.dispatch({
        run: function () {
            aFunc();
        }
    }, Ci.nsIThread.DISPATCH_NORMAL);
}

but this also freezes the UI. Is there any other solution?

The crawling code:

...
timer.setTimeout(function () {
    let pageWorker = require("sdk/page-worker").Page({
       contentScriptFile: self.data.url("js/extractor.js"),
           contentURL:        url
    });

    pageWorker.port.on("loaded", function (content) {
        if (typeof callback === 'function') {
            callback(content);
        }
    });
}, 200)
...

The extractor.js, even in simpler cases, where it return body.textContent, is blocking.


回答1:


The page-worker API just creates an invisible page, it does not do so on a background thread since it has to create a complete window/document environment including layout to allow for full dom/styling functionality and layout always happens on the main thread.

If you want to do calculations in the background you should use the Worker or ChromeWorker APIs, in which you won't have access to DOM and many other APIs.

In the SDK you can use

const { ChromeWorker } = require("chrome");



回答2:


This is because Firefox uses the main thread for page-workers, in Firefox nightly content pages uses separate processes, which means that page-workers will use a separate processes too, so give Firefox Nightly a try, it should work there, and this will be released in a few months.



来源:https://stackoverflow.com/questions/27097098/firefox-addon-sdk-page-worker-blocks-main-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!