GM_worker

supahgreg edited this page Jul 3, 2011 · 8 revisions

IMPORTANT: Due to changes in Firefox's Worker functionality (bug 608142), GM_worker has been removed from Scriptish.

This page has been retained for archival purposes.

Description

GM_worker is intended to be an implementation of Worker available for use in a user script.

The problem with using a Worker is that it follows the same origin policy. For our purposes, this means a user script could not create a Worker unless it knew of an appropriate script on the same origin as the website being modified. GM_worker is not subject to this policy.

Arguments

String resourceName

This argument is the name of a resource provided by an @resource in the Metadata Block.

Returns

Worker with the following:

Properties

Function onmessage

Function onerror

Methods

Function postMessage

Function terminate

Example

// ==UserScript==
// @name         GM_worker test
// @namespace    gWorkerTest
// @include      *
// @resource     worker http://erikvold.com/tests/greasemonkey/gmWorker/worker.js
// ==/UserScript==
var worker = GM_worker("worker");
worker.onmessage = function(event) {
  alert(event.data);
};
worker.postMessage('hello world!');

Source of the worker.js:

onmessage = function(event) {
  postMessage(event.data);
}