te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Using web workers with chrome extension - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using web workers with chrome extension - Stack Overflow

programmeradmin3浏览0评论

What I am trying to achieve here, is to execute a XHRHttpRequest() on a worker to speedup my extension. I am using worker_proxy.js from here. It is working totally fine except that I am unable figure out how to pass a string to this worker. Here is my 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"
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 from worker: ' + event.data);
};

I am able to receive the data from worker.js, but how do I send data to it .i.e.,

var somestring=getStringFromContentJS()

What I am trying to achieve here, is to execute a XHRHttpRequest() on a worker to speedup my extension. I am using worker_proxy.js from here. It is working totally fine except that I am unable figure out how to pass a string to this worker. Here is my 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."
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 from worker: ' + event.data);
};

I am able to receive the data from worker.js, but how do I send data to it .i.e.,

var somestring=getStringFromContentJS()

Share Improve this question edited May 5, 2023 at 6:19 Pavindu 3,1128 gold badges52 silver badges87 bronze badges asked Sep 29, 2015 at 20:39 Harshil PansareHarshil Pansare 1,1472 gold badges13 silver badges38 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

To send data to a worker, simply add the following to the top of worker.js:

self.onmessage = function(event) {
    // Do something with event.data
};

and use az_worker.postMessage(somestring); to send data to it.


But your code is unnecessarily plex. If your goal is "to speedup my extension", then you should not be using Web workers to make synchronous XHR, but use asynchronous XHR on the main thread. Web Workers are great for CPU-bound tasks, but don't provide any advantage for I/O tasks such as network requests.

See https://developer.mozilla/en-US/docs/AJAX/Getting_Started and https://developer.mozilla/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest for tutorials on using XMLHttpRequest.

发布评论

评论列表(0)

  1. 暂无评论