-
-
Save creationix/75cb3d540418980f32a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var blob = new Blob([ | |
"onmessage = function(e) { postMessage('msg from worker'); }"], | |
{type:"application/javascript"} | |
); | |
// Obtain a blob URL reference to our worker 'file'. | |
var blobURL = window.URL.createObjectURL(blob); | |
var worker = new Worker(blobURL); | |
worker.onmessage = function(e) { | |
var p = document.createElement('p'); | |
p.textContent = e.data; | |
document.body.appendChild(p); | |
console.log(e.data); | |
// e.data == 'msg from worker' | |
}; | |
worker.postMessage(""); // Start the worker. |
Read the code again, all the worker is doing is onmessage = function(e) { postMessage('msg from worker'); }"
. The dom manipulation is done in the main context where it belongs.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not a CSP problem. Workers don't have access to the root's global environment. Which means, they can't access document or window objects.
Workers can do XMLRequests to the same domain, but the only way to communicate with the parent environment which is CSP enforced is via messages.