Created
September 13, 2011 12:09
-
-
Save teramako/1213674 to your computer and use it in GitHub Desktop.
VimperatorプラグインからChromeWorkerを使うサンプル
This file contains hidden or 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
/* | |
* 事前にランタイムディレクトリ内に module ディレクトリを作成し workerとして使うファイルを置いておくこと | |
*/ | |
var resourceName = "vimp-module"; | |
var IO = services.get("io"); | |
var moduleDirs = io.getRuntimeDirectories("module"); | |
if (moduleDirs.length === 0) | |
throw new Error("module directory is not found"); | |
// resouceプロトコルに resourceName の名前でディレクトリを登録 | |
IO.getProtocolHandler("resource") | |
.QueryInterface(Ci.nsIResProtocolHandler) | |
.setSubstitution(resourceName, IO.newFileURI(modulesDirs[0])); | |
var worker = new Worker("resource://" + resourceName + "/test-worker.js"); | |
worker.onmessage = fucntion (event) { | |
console.log(event.data); | |
} | |
worker.postMessage("5"); // コンソールに 25 が出てくるはず |
This file contains hidden or 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
// 単純に2乗した値を返す | |
onmessage = function (event) { | |
var n = parseInt(event.data); | |
postMessage(n * n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment