1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 21:00:23 +01:00

Navigator test: added nested worker and worker from blob

Fixes #458
This commit is contained in:
kkapsner 2020-04-15 00:25:24 +02:00
parent 308d7c4005
commit 49f3c166a2
2 changed files with 73 additions and 10 deletions

View File

@ -67,19 +67,48 @@ iframeAPI.forEachMethod(function(windowToUse, name){
processNavigatorObject(navigator, Object.keys(navigator.__proto__), name); processNavigatorObject(navigator, Object.keys(navigator.__proto__), name);
}); });
const worker = new Worker("navigatorTestWorker.js"); function processWorkerNavigatorObject(data, name){
"use strict";
processNavigatorObject(data.values, Object.keys(data.values), name);
if (data.nestedValues){
processWorkerNavigatorObject(data.nestedValues, "nested " + name);
}
}
const worker = new Worker("navigatorTestWorker.js", {name: "Worker"});
worker.addEventListener("message", function(event){ worker.addEventListener("message", function(event){
"use strict"; "use strict";
processNavigatorObject(event.data, Object.keys(event.data), "Worker"); processWorkerNavigatorObject(event.data, "Worker");
worker.terminate(); worker.terminate();
}); });
const sharedWorker = new SharedWorker("navigatorTestWorker.js"); fetch("navigatorTestWorker.js").then(function(response){
"use strict";
return response.text();
}).then(function(code){
"use strict";
const blob = new Blob([code], {type: "text/javascript"});
const blobWorker = new Worker(URL.createObjectURL(blob), {name: "BlobWorker"});
blobWorker.addEventListener("message", function(event){
processWorkerNavigatorObject(event.data, "BlobWorker");
blobWorker.terminate();
});
return blobWorker;
}).catch(function(error){
"use strict";
console.error("Unable to create BlobWorker:", error);
});
const sharedWorker = new SharedWorker("navigatorTestWorker.js", {name: "SharedWorker"});
sharedWorker.port.addEventListener("message", function(event){ sharedWorker.port.addEventListener("message", function(event){
"use strict"; "use strict";
processNavigatorObject(event.data, Object.keys(event.data), "SharedWorker"); processWorkerNavigatorObject(event.data, "SharedWorker");
sharedWorker.port.close(); sharedWorker.port.close();
}); });
sharedWorker.port.start(); sharedWorker.port.start();
@ -89,7 +118,7 @@ navigator.serviceWorker.register("navigatorTestWorker.js").then(function(registr
const worker = (registration.active || registration.waiting || registration.installing); const worker = (registration.active || registration.waiting || registration.installing);
navigator.serviceWorker.addEventListener("message", function(event){ navigator.serviceWorker.addEventListener("message", function(event){
processNavigatorObject(event.data, Object.keys(event.data), "ServiceWorker"); processWorkerNavigatorObject(event.data, "ServiceWorker");
registration.unregister(); registration.unregister();
}); });
if (worker.state !== "activated"){ if (worker.state !== "activated"){

View File

@ -2,6 +2,7 @@
"use strict"; "use strict";
const values = {}; const values = {};
let nestedValues = false;
const navigator = self.navigator; const navigator = self.navigator;
Object.keys(navigator.__proto__).sort().forEach(function(property){ Object.keys(navigator.__proto__).sort().forEach(function(property){
const value = navigator[property]; const value = navigator[property];
@ -9,16 +10,49 @@
values[property] = value; values[property] = value;
} }
}); });
const ports = [];
const sources = [];
let sendDelayed = false;
function sendData(){
sendDelayed = true;
if (self.postMessage){ if (self.postMessage){
self.postMessage(values); self.postMessage({values, nestedValues});
}
ports.forEach(function(port){
port.postMessage({values, nestedValues});
});
sources.forEach(function(source){
source.postMessage({values, nestedValues});
});
} }
self.addEventListener("connect", function(event){ self.addEventListener("connect", function(event){
event.ports.forEach(function(port){ event.ports.forEach(function(port){
port.postMessage(values); ports.push(port);
}); });
if (sendDelayed){
sendData();
}
}); });
self.addEventListener("message", function(event){ self.addEventListener("message", function(event){
event.source.postMessage(values); sources.push(event.source);
return values; if (sendDelayed){
sendData();
}
}); });
if (
!(self.name && self.name.startsWith("nested nested")) &&
self.Worker
){
const worker = new Worker(self.location, {name: "nested " + (self.name || "")});
worker.addEventListener("message", function(event){
nestedValues = event.data;
worker.terminate();
sendData();
});
}
else {
sendData();
}
}()); }());