/worker-dom-access

Access DOM in Web Worker :warning: Proof-of-Concept

Primary LanguageTypeScript

Access DOM in Web Worker

index.js

// Create Web Worker
const worker = new Worker('worker.js');

// Create proxy
const targetProxy = new TargetProxy(worker);

// Gave access to window object
targetProxy.register('window', window);

worker.js

// link proxy object
const window: any = WorkerProxyObject('window');

// eval native alert function in GUI thread from worker thread
window.alert('Hello From Worker');

// direct access to DOM from worker
window.document.body.innerText = 'Hello From Worker';

How it works

In two words Proxy & Promise. For more details, please see worker.ts file (less than 100 lines of code).

Fell free to conribute