是否可以在 Greasemonkey 脚本中使用工人?

发布于 2024-08-07 03:37:27 字数 704 浏览 3 评论 0原文

我想使用 Firefox 3.5 中引入的 Web Worker 工具来增强我正在开发的 Greasemonkey 脚本。

这可能吗?

我已经做了一些实验,但我无法解决从任意域加载工作脚本的问题。

例如,这不起作用:

var myWorker = new Worker("http://dl.getdropbox.com/u/93604/js/worker.js");

此代码在我的 Firebug 控制台中生成一条错误消息:

加载脚本失败: http://dl.getdropbox.com/u/93604/js/worker.js (ns结果 = 0x805303f4)

显然存在一个限制,不允许您从与调用脚本的基本 URL 无关的 URL 启动工作程序。您可以像这样在相对 URL 加载工作脚本:

var myWorker = new Worker("worker.js");

但是我无法在用户的文件系统上获取工作脚本,以便它位于相对于调用脚本的路径。

我在这里搞砸了吗?我应该放弃尝试在 Greasemonkey 脚本中使用工人吗?

I would like to use the Web Worker facility introduced in Firefox 3.5 to enhance a Greasemonkey script I'm working on.

Is this even possible?

I've done some experimentation, but I can't get past the issue of loading a worker script from an arbitrary domain.

For example, this does not work:

var myWorker = new Worker("http://dl.getdropbox.com/u/93604/js/worker.js");

This code generates an error message in my Firebug console:

Failed to load script:
http://dl.getdropbox.com/u/93604/js/worker.js
(nsresult = 0x805303f4)

Apparently there's a limitation that does not allow you to start a worker from a URL that's not relative to the base URL of the calling script. You can load a worker script at a relative URL like this just fine:

var myWorker = new Worker("worker.js");

But there's no way for me to get the worker script on the user's filesystem so that it could be at a path relative to the calling script.

Am I screwed here? Should I give up on trying to use workers within my Greasemonkey script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

¢好甜 2024-08-14 03:37:27

多年来我一直认为不可能在 GM 中使用网络工作者。当然,第一个想法是使用 data-urls。但 Worker 构造函数似乎不接受它们。

今天我又尝试了一下,一开始没有任何问题。仅当我开始使用 GM API 的函数时,Worker 构造函数才停止工作。

Firefox 似乎有一个错误,会阻止您从具有 X 射线视觉的沙箱访问 Worker。即使评估 typeof Worker 也会引发异常。所以使用workers的唯一方法是从未包装的窗口中获取未包装的版本:

var echoWorker = new unsafeWindow.Worker("data:text/javascript," +
    "self.onmessage = function(e) {\n" +
    "    self.postMessage(e.data);\n" +
    "};"
);

当然,你必须小心特殊字符。最好使用 base64 对脚本进行编码:

var dataURL = 'data:text/javascript;base64,' + btoa(script);
var worker = new unsafeWindow.Worker(dataURL);

或者,您也可以使用 blob-urls:

var blob = new Blob([script], {type: 'text/javascript'});
var blobURL = URL.createObjectURL(blob);
var worker = new unsafeWindow.Worker(blobURL);
URL.revokeObjectURL(blobURL);

如果您确实想使用托管在不同域上的脚本,这不是问题,因为同源策略不适用于 GM_xmlhttpRequest< /代码>:

function createWorkerFromExternalURL(url, callback) {
    GM_xmlhttpRequest({
        method: 'GET',
        url: url,
        onload: function(response) {
            var script, dataURL, worker = null;
            if (response.status === 200) {
                script = response.responseText;
                dataURL = 'data:text/javascript;base64,' + btoa(script);
                worker = new unsafeWindow.Worker(dataURL);
            }
            callback(worker);
        },
        onerror: function() {
            callback(null);
        }
    });
}

For years I thought it wasn't possible to use web workers in GM. Of course the first idea was to use data-urls. But the Worker constructor didn't seem to accept them.

Today I tried it again and it worked without any problems at first. Only when I started to use functions of the GM API the Worker constructor stopped working.

Seemingly Firefox has a bug that prevents you from accessing Worker from a sandbox with X-ray vision. Even evaluating typeof Worker throws an exception. So the only way to use workers is to get the unwrapped version from the unwrapped window:

var echoWorker = new unsafeWindow.Worker("data:text/javascript," +
    "self.onmessage = function(e) {\n" +
    "    self.postMessage(e.data);\n" +
    "};"
);

Of course, you have to be careful about special characters. It's better to encode the script with base64:

var dataURL = 'data:text/javascript;base64,' + btoa(script);
var worker = new unsafeWindow.Worker(dataURL);

Alternatively, you can also use blob-urls:

var blob = new Blob([script], {type: 'text/javascript'});
var blobURL = URL.createObjectURL(blob);
var worker = new unsafeWindow.Worker(blobURL);
URL.revokeObjectURL(blobURL);

If you really want to use a script hosted on a different domain that's not a problem because same origin policy doesn't apply for GM_xmlhttpRequest:

function createWorkerFromExternalURL(url, callback) {
    GM_xmlhttpRequest({
        method: 'GET',
        url: url,
        onload: function(response) {
            var script, dataURL, worker = null;
            if (response.status === 200) {
                script = response.responseText;
                dataURL = 'data:text/javascript;base64,' + btoa(script);
                worker = new unsafeWindow.Worker(dataURL);
            }
            callback(worker);
        },
        onerror: function() {
            callback(null);
        }
    });
}
花期渐远 2024-08-14 03:37:27

到目前为止(10 年后),可以将 Web Workers 与 Firefox 77 和 Tampermonkey 一起使用。我已经使用内联工作人员成功进行了测试:

var blob = new Blob(["onmessage = function(e){postMessage('whats up?');console.log(e.data)}"], {type: 'text/javascript'})

var url = URL.createObjectURL(blob)

var worker = new Worker(url)

worker.onmessage = function(e){
  console.log(e.data) 
}

worker.postMessage('hey there!')

使用 Chrome 或其他扩展(例如 Greasemonkey 或 Violentmonkey),由于 CSP worker-src 而无法工作(请参阅 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/内容安全策略/worker-src)。这就是为什么不能使用 HTTP URL 或字符串作为 Worker 构造函数的参数,只能在这种非常特殊的情况下使用 blob URL。

尽管如此,关于工人的背景还是有一个问题。他们无法访问 DOM、窗口、文档或父对象(请参阅工作人员可用的功能 https://www.html5rocks.com/en/tutorials/workers/basics/)。

By now (10 years later), it's possible to use Web Workers with Firefox 77 and Tampermonkey. I've tested sucessfully using inline workers:

var blob = new Blob(["onmessage = function(e){postMessage('whats up?');console.log(e.data)}"], {type: 'text/javascript'})

var url = URL.createObjectURL(blob)

var worker = new Worker(url)

worker.onmessage = function(e){
  console.log(e.data) 
}

worker.postMessage('hey there!')

With Chrome or other extension like Greasemonkey ou Violentmonkey, i'ts not working because of CSP worker-src (see violation cases at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/worker-src). This is why is not possible to use HTTP URLs or string as argument of Worker constructor, only works with blob URLs in this very specific case.

Still, there is a catch about the context of Workers. They can't access DOM, window, document or parent objects (see features available to workers at https://www.html5rocks.com/en/tutorials/workers/basics/).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文