是否可以在 Greasemonkey 脚本中使用工人?
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
多年来我一直认为不可能在 GM 中使用网络工作者。当然,第一个想法是使用 data-urls。但 Worker 构造函数似乎不接受它们。
今天我又尝试了一下,一开始没有任何问题。仅当我开始使用 GM API 的函数时,
Worker
构造函数才停止工作。Firefox 似乎有一个错误,会阻止您从具有 X 射线视觉的沙箱访问
Worker
。即使评估typeof Worker
也会引发异常。所以使用workers的唯一方法是从未包装的窗口中获取未包装的版本:当然,你必须小心特殊字符。最好使用 base64 对脚本进行编码:
或者,您也可以使用 blob-urls:
如果您确实想使用托管在不同域上的脚本,这不是问题,因为同源策略不适用于
GM_xmlhttpRequest< /代码>:
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 evaluatingtypeof Worker
throws an exception. So the only way to use workers is to get the unwrapped version from the unwrapped window:Of course, you have to be careful about special characters. It's better to encode the script with base64:
Alternatively, you can also use blob-urls:
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
:到目前为止(10 年后),可以将 Web Workers 与 Firefox 77 和 Tampermonkey 一起使用。我已经使用内联工作人员成功进行了测试:
使用 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:
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/).
请参阅:
我可以从以下位置加载 Web Worker 脚本吗绝对网址?
See:
Can I load a web worker script from an absolute URL?