[英]Azure DevOps WebExtension use WebWorker
I want to use a WebWorker inside a WebExtension on a Azure DevOps Server.我想在 Azure DevOps 服务器上的WebExtension 中使用WebWorker 。
Processing data of a large repository cost a lot, so I want to use a WebWorker to calculate in Background.处理大型repository的数据开销很大,所以想在后台用一个WebWorker来计算。
But when I call new Worker("static/js/WorkerLoadTree.js")
:但是当我调用new Worker("static/js/WorkerLoadTree.js")
:
//CREATE WORKER
console.log("BEFORE NEW WORKER CALL")
BackgroundWorker = new Worker("static/js/WorkerLoadTree.js");
console.log("AFTER NEW WORKER CALL")
I see in Edge:我在 Edge 中看到:
and I see in Chrome a bit more details:我在 Chrome 中看到了更多细节:
VSS.SDK.min.js:2 Rejected XDM promise with no reject callbacks n._reject @ VSS.SDK.min.js:2
VSS.SDK.min.js:2 DOMException: Failed to construct 'Worker': Script at 'http://136.310.18.216:8070/_apis/public/gallery/publisher/user/extension/SearchRepos/1.0.13/assetbyname/static/js/WorkerLoadTree.js' cannot be accessed from origin 'null'.
at WorkerStart (http://136.310.18.216:8070/_apis/public/gallery/publisher/user/extension/SearchRepos/1.0.13/assetbyname/static/js/WorkerMain.js:13:32)
at FillCode (http://136.310.18.216:8070/_apis/public/gallery/publisher/user/extension/SearchRepos/1.0.13/assetbyname/index.html:284:3)
at http://136.310.18.216:8070/_apis/public/gallery/publisher/user/extension/SearchRepos/1.0.13/assetbyname/index.html:144:6
at n._wrapCallback (http://136.310.18.216:8070/_apis/public/gallery/publisher/user/extension/SearchRepos/1.0.13/assetbyname/lib/VSS.SDK.min.js:2:951)
at Array.<anonymous> (http://136.310.18.216:8070/_apis/public/gallery/publisher/user/extension/SearchRepos/1.0.13/assetbyname/lib/VSS.SDK.min.js:2:647)
at http://136.310.18.216:8070/_apis/public/gallery/publisher/user/extension/SearchRepos/1.0.13/assetbyname/lib/VSS.SDK.min.js:2:1383 n._reject @ VSS.SDK.min.js:2
Failed to load resource: net::ERR_UNEXPECTED :8070/DefaultCollection/_apis/Contribution/HierarchyQuery/project/09737d31-b39f-49a1-8973-4a702cc4be92:1
It seems to be something wrong to load the WorkerLoadTree.js
.加载WorkerLoadTree.js
似乎有问题。 Is the way to call correct?调用方式正确吗? How can the worker access a js-file inside an extension?工作人员如何访问扩展中的 js 文件?
My Extension File-Structure looks like:我的扩展文件结构看起来像:
───SearchRepos
│ icon.png
│ index.html
│ SearchRepos-1.0.14.vsix
│ package-lock.json
│ package.json
│ vss-extension.json
│
├───node_modules
│ └───vss-web-extension-sdk
│ ...
│
└───static
├───css
│ main.css
│
├───js
│ main.js
│ WorkerLoadCode.js <- WebWorker
│ WorkerLoadTree.js <- WebWorker
│ WorkerMain.js
│
├───lib
│ jquery-3.4.1.min.js
│ jstree.js
│
└───themes
...
Meta:元:
Azure DevOps Server 17.143.28912.1 (AzureDevOps2019.0.1) Azure DevOps 服务器 17.143.28912.1 (AzureDevOps2019.0.1)
DOMException: Failed to construct 'Worker': Script at ' http://136.310.18.216:8070/_apis/xxxx/static/js/WorkerLoadTree.js ' cannot be accessed from origin 'null'. DOMException: 无法构造“Worker”:无法从源“null”访问“ http://136.310.18.216:8070/_apis/xxxx/static/js/WorkerLoadTree.js ”处的脚本。
This is a very normal error about access file across domain .这是跨域访问文件的一个非常正常的错误。 Use web worker has one limitation : Same Origin Policy .使用 web worker 有一个限制:同源策略。 And also the browser does not allow to create a worker with a URL which pointing to a different domain.而且浏览器也不允许创建一个带有指向不同域的 URL 的工作线程。 This "across domain" error caused by your call way is not correct.这种由您的调用方式引起的“跨域”错误是不正确的。
BackgroundWorker = new Worker("static/js/WorkerLoadTree.js");
Chrome doesn't let you load web workers when running scripts from a local file(), or the error will like this:当从本地 file() 运行脚本时,Chrome 不允许您加载网络工作者,否则错误将如下所示:
Note : I just do a example in my local machine.注意:我只是在我的本地机器上做一个例子。 Loading a local file, even with a relative URL, is same as loading a file with the file:
protocol.加载本地文件,即使使用相对 URL,也与使用file:
协议加载文件相同。
You should use the url which from the webserver where these files put, like this: http://xxxx:xx/static/js/WorkerLoadTree.js
.您应该使用来自这些文件所在的网络服务器的 url,如下所示: http://xxxx:xx/static/js/WorkerLoadTree.js
。 Or the chrome will consider this local file called way as using file across domain.或者 chrome 会认为这个本地文件调用方式为跨域使用文件。
Finally I stoped to follow the WebWorker approach.最后我停下来遵循 WebWorker 方法。 I couldn't get it work inside a Azure DevOps WebExtension.我无法在 Azure DevOps WebExtension 中使用它。 Instead I use the requestIdleCallback
/ setTimeout
aproach, which is working well in my usecase.相反,我使用requestIdleCallback
/ setTimeout
,它在我的用例中运行良好。
For that you need two functions.为此,您需要两个功能。
First:第一的:
function TaskCaller() {
//TASK CALLER
TaskRunner(0, int Interations);
}
and second:第二:
function TaskRunner(xCount, xEnd) {
//TASK RUNNER
xCount++;
//ENTER CODE HERE
console.log(xCount)
//NEXT ITERATION
if (xCount < xEnd)
if (xCount % 1000) setTimeout(function () { TaskRunner(xCount, xEnd) }, 0);
else TaskRunner(xCount, xEnd);
}
I realised that for task it's enough when the task runner call a timeout every 1000 nd iteration.我意识到对于任务,当任务运行器每 1000 次迭代调用一次超时时就足够了。 I could still control the page while the runner calculate in background.当跑步者在后台计算时,我仍然可以控制页面。
There are much better solutions out there, for instance background.js , but for now it's okay.那里有更好的解决方案,例如background.js ,但现在还可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.