javascript - Using web workers with chrome extension -
what trying achieve here, execute xhrhttprequest()
on worker speedup extension. using worker_proxy.js
here. working totally fine except unable figure out how pass string worker. here code:
manifest.json
"permissions": [ "alarms", "activetab", "tabs", "webnavigation", "http://*/*", "https://*/*", "cookies" ], "options_page": "options.html", "background": { "persistent": false, "scripts": [ "worker_proxy.js","background.js"] }, "content_scripts": [ { "matches": ["https://*/*","http://*/*"], "js": ["jquery-2.1.4.js","hmac-sha256.js","enc-base64-min.js","worker_proxy.js","content.js"] } ], "web_accessible_resources": [ "worker_proxy.html","worker.js"],
worker.js
var somestring=getstringfromcontentjs() var xhr=new xmlhttprequest(); var request="getgoogle.com" xhr.open("get", request, false); xhr.send(somestring); postmessage('result\n' + xhr.responsetext);
content.js
var az_worker = new worker(chrome.runtime.geturl('getazproducts.js')); az_worker.onmessage = function(event) { alert('message worker: ' + event.data); };
i able receive data worker.js how send data it, i.e.,
var somestring=getstringfromcontentjs()
to send data worker, add following top of worker.js:
self.onmessage = function(event) { // event.data };
and use az_worker.postmessage(somestring);
send data it.
but code unnecessarily complex. if goal "to speedup extension", should not using web workers make synchronous xhr, use asynchronous xhr on main thread. web workers great cpu-bound tasks, don't provide advantage i/o tasks such network requests.
see https://developer.mozilla.org/en-us/docs/ajax/getting_started , https://developer.mozilla.org/en-us/docs/web/api/xmlhttprequest/using_xmlhttprequest tutorials on using xmlhttprequest.
Comments
Post a Comment