[removed] “Uncaught SecurityError” when running JS

2019-09-05 06:41发布

(This question pertains to the JS-XSL demo found here)

To briefly tell you what this demo is for; it takes a MS Excel file as input, parses the data, and outputs the data in text-only format. I downloaded the package (zip) and ran it locally, simply by opening the html file with Chrome.

The problem is, I just cannot seem to get over the following error:

Uncaught SecurityError: Failed to construct 'Worker': Script at 'file:///C:/Users/David/Desktop/Xlsx%20Demo/xlsworker.js' cannot be accessed from origin 'null'.

And above error points to line 34 of the html file, which has the following code:

/* I changed the file path from './xlsworker.js' to 'xlsworker.js' */
var worker = new Worker('xlsworker.js');

There are only three files for this demo: the html file itself, and two javascript files, one is named xls.js and the other xlsworker.js. All three files are in the same directory and at the same level.

What's rather baffling to me is, I successfully ran this same demo about a couple months ago! I cannot imagine if I am doing anything differently now. Any insight?

2条回答
一纸荒年 Trace。
2楼-- · 2019-09-05 07:01

https://code.google.com/p/chromium/issues/detail?id=278883#c9

You are basically prevented by Chromium to use workers on the file:// protocol, you have to host your files and access them through the http:// protocol.

You need a server (even something simple like http://docs.python.org/2/library/simplehttpserver.html)

查看更多
叼着烟拽天下
3楼-- · 2019-09-05 07:10

IMO, the below is a superior answer, because it does not require running a web-server. It's extremely quick and simple.

(the downvote is explained in my Note 5, below)

I have tested and verified that this solution works with the demo linked by the asker, when run locally as described by the asker. Tested on Windows 10, Chrome Stable x64 48.0.2564.103 m.

A script is not allowed to access to your local file system in Chrome. If you'd like to test web workers locally, you have to open Chrome with a special flag.

On Windows, launch chrome with the flag:

chrome.exe --allow-file-access-from-files

After that Chrome will be launched and you can test workers during this session.

Note1: if Chrome is already running when you run this command, the new instance of Chrome will not allow Workers to run-- you must first exit from Chrome (if necessary, use Windows Task Manager to ensure Chrome is not running).

Note 2: the source for this answer (link below) includes the --args flag on Windows, but i have not found the "args" to be necessary on Windows. In fact, i cannot find any documentation of the "args" flag anywhere-- not sure what it does. So i don't include it in the Windows command, above.*

Note 3: For those trying to do something similar on Chrome-Mac, or Windows-Firefox, the link below includes both, along with the Windows-Chrome solution above. But my solution described above is only the Windows-Chrome method.

http://js-workout.tompascall.com/web-workers-and-responsiveness/

Note 4: Be aware that this solution is not the same as running a web-server, but for your purpose it should be a completely adequate solution. Also, be aware that to browse the web with this chrome startup-switch enabled may compromise your local file-security, so it's advised to only use this method for your local file purpose, and disable it for web-browsing.*

Note 5: Google states that using startup flags "should only be used for temporary cases and may break in the future." Web search for chrome startup switches returns about 2,000 hits, so lots of people use them and blog about them. If your need is temporary, then this currently works great.*

查看更多
登录 后发表回答