javascript - Process message from Webworker created by unreachable code -
i'm new javascript, i'm sorry if answer obvious.
i want send message main thread web-worker. don't have access part of code create worker can't add worker.onmessage = [...]
. have access javascript code executed @ begining of process, not 1 create workers.
i wondering if there's way window.oneverymessagefromanyworker = dosomemagic()
thanks lot!
no, that's not possible. need handle web worker in order listen messages. make sure can't change code makes worker, if you can't read below:
hacky solution
if you're writing userscript or other "hacky" code, suggest override worker constructor:
// execute before worker being created window.oldworker = worker; window.worker = function(path) { console.log("worker created: ", path); // original worker applied on object return new oldworker(path); }
note nasty piece of code. might break things.
once have code, can access worker created:
window.worker = function(path) { console.log("worker created: ", path); var worker = new oldworker(path); if(path == "something.js") { // worker.addeventlistener("message", (e)=>{console.log("message:", e.data}); } // original worker applied on object return worker; }
note must use worker.addeventlistener
. setting .onmessage
can cause callback overridden, since there can 1 callback in onmessage
property.
Comments
Post a Comment