pouët.net

Go to bottom

javascript webworkers without external file

category: code [glöplog]
 
anyone knows if it's possible to have javascript webworkers without external file? for size optimization would be nice to keep it all just 1 file. haven't done any proper research on this, couple quick searches on google says it's not possible. anyone knows any potential work arounds?
added on the 2013-10-30 11:52:36 by psenough psenough
Shot in the dark, but maybe if you put your code into a data-url it counts as external file?
added on the 2013-10-30 12:14:50 by mog mog
new worker('data:text/javascript,....');
Quote:
Thus, scripts must either be external files with the same scheme, host, and port as the original page, or data: URLs. For example, you can't load a script from a javascript: URL, and an https: page couldn't start workers using scripts with http: URLs.
-- Source: TFM

;)
added on the 2013-10-30 12:15:07 by p01 p01
haha :D
<3
added on the 2013-10-30 12:16:06 by mog mog
Of course you can have the worker code in your main JS, get the string version of the methods you want in the worker, btoa the whole thing and feed it to the data:URL to instantiate the worker.

HTH,
added on the 2013-10-30 12:17:05 by p01 p01
awesome
added on the 2013-10-30 12:22:01 by psenough psenough
I read
Quote:
javascript webworkers without external life
Me too.
added on the 2013-10-30 14:35:48 by Preacher Preacher
germany is making you dyslexic ;)
added on the 2013-10-30 15:22:20 by psenough psenough
Have you ever been to the engineering level?

Oh, never mind.

Code:Function.prototype.workerify = function(){ var b = this+""; return window.URL.createObjectURL(new Blob([b.slice(b.indexOf("{")+1,b.lastIndexOf("}"))],{type:"text/javascript"})) }
added on the 2013-10-30 19:05:12 by Suborg Suborg
Instead of &q uot; there must be just a ".
added on the 2013-10-30 19:05:46 by Suborg Suborg
Okay, this was overkill. This should work too (pass the function as a parameter).
Code:workerify=function(f){return URL.createObjectURL(new Blob([(f+"").trim().match(/{(.*)}/)[1]]))}
added on the 2013-10-30 19:31:28 by Suborg Suborg
Hm. even trim isn't necessary.
Code:workerify=function(f){return URL.createObjectURL(new Blob([(f+"").match(/{(.*)}/)[1]]))}


> haven't done any proper research on this, couple quick searches on google says it's not possible.
With modern Web tech, everything is possible.
added on the 2013-10-30 19:39:40 by Suborg Suborg

login

Go to top