Skip to content

Both copy text to your clipboard — Build with AI copies a setup prompt to paste into Claude Code, Cursor, Codex or Copilot; Copy page as Markdown copies this page to paste into a chat. How it works

importScriptInWorkers()

importScriptInWorkers(workerUrl: string): Promise<void[]>

Defined in: src/index.ts:174

Allows loading javascript code in the worker thread. Note that since this is using some very internal classes and flows it is considered experimental and can break at any point.

It can be useful for the following examples:

  1. Using self.addProtocol in the worker thread - note that you might need to also register the protocol on the main thread.
  2. Using self.registerWorkerSource(workerSource: WorkerSource) to register a worker source, which should come with addSourceType usually.
  3. using self.actor.registerMessageHandler to override some internal worker operations

Parameters

ParameterTypeDescription
workerUrlstringthe worker url e.g. a url of a javascript file to load in the worker

Returns

Promise<void[]>

Example

ts
// below is an example of sending a js file to the worker to load the method there
// Note that you'll need to call the global function `addProtocol` in the worker to register the protocol there.
// add-protocol-worker.js
async function loadFn(params, abortController) {
    const t = await fetch(`https://${params.url.split("://")[1]}`);
    if (t.status == 200) {
        const buffer = await t.arrayBuffer();
        return {data: buffer}
    } else {
        throw new Error(`Tile fetch error: ${t.statusText}`);
    }
}
self.addProtocol('custom', loadFn);

// main.js
importScriptInWorkers('add-protocol-worker.js');