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

152 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-12-24 00:54:17 +01:00
/* globals iframeAPI*/
2019-12-16 19:27:28 +01:00
const createLog = function(){
2019-02-27 23:49:00 +01:00
"use strict";
2019-12-16 19:27:28 +01:00
const div = document.getElementById("log");
2019-02-27 23:49:00 +01:00
return function createLog(){
2019-12-16 19:27:28 +01:00
const logDiv = document.createElement("div");
2019-02-27 23:49:00 +01:00
logDiv.className = "log";
div.appendChild(logDiv);
return function createLine(str){
2019-12-16 19:27:28 +01:00
const logLine = document.createElement("div");
2019-02-27 23:49:00 +01:00
logLine.className = "logLine";
logDiv.appendChild(logLine);
logLine.textContent = str;
2019-12-21 23:47:07 +01:00
return {
update: function updateLine(str){
str = str.replace("{old content}", logLine.textContent);
logLine.textContent = str;
},
2019-12-24 00:54:17 +01:00
mark: function mark(marktext){
2019-12-21 23:47:07 +01:00
logLine.classList.add("marked");
2019-12-24 00:54:17 +01:00
logLine.title += marktext + "\n";
2019-12-21 23:47:07 +01:00
}
2019-02-27 23:49:00 +01:00
};
};
};
}();
2019-12-21 23:47:07 +01:00
const log = createLog();
2019-02-27 23:49:00 +01:00
2019-12-21 23:47:07 +01:00
const userAgentIsConsistent = document.getElementById("serverUserAgent").text === navigator.userAgent;
const consistencyLine = log("user agent equal between server and client: " + userAgentIsConsistent);
if (!userAgentIsConsistent){
consistencyLine.mark();
}
const lines = {};
2019-12-24 00:54:17 +01:00
function processNavigatorObject(navigator, keys, name){
2019-05-22 23:39:56 +02:00
"use strict";
keys.sort().forEach(function(property){
2019-12-21 23:47:07 +01:00
const value = navigator[property];
if ((typeof value) === "string"){
if (!lines[property]){
lines[property] = {
values: [],
log: log(property + ": ")
};
}
const propertyLine = lines[property];
if (propertyLine.values.indexOf(value) === -1){
propertyLine.log.update("{old content}" + (propertyLine.values.length? " | ": "") + value);
propertyLine.values.push(value);
}
if (propertyLine.values[0] !== value){
2019-12-24 00:54:17 +01:00
propertyLine.log.mark("failed test " + name);
2019-12-21 23:47:07 +01:00
}
}
});
}
iframeAPI.forEachMethod(async function(windowToUse, name){
"use strict";
const navigator = windowToUse.navigator;
const values = {};
const keys = Object.keys(navigator.__proto__);
keys.forEach(function(property){
const value = navigator[property];
if ((typeof value) === "string"){
values[property] = value;
}
});
const storage = await navigator.storage.estimate();
values.storage_quota = storage.quota.toString(10);
keys.push("storage_quota");
console.log(name, values);
processNavigatorObject(values, keys, name);
});
function processWorkerNavigatorObject(data, name){
"use strict";
console.log(name, data);
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){
"use strict";
processWorkerNavigatorObject(event.data, "Worker");
worker.terminate();
});
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){
"use strict";
processWorkerNavigatorObject(event.data, "SharedWorker");
sharedWorker.port.close();
});
sharedWorker.port.start();
navigator.serviceWorker.register("navigatorTestWorker.js").then(function(registration){
"use strict";
const worker = (registration.active || registration.waiting || registration.installing);
navigator.serviceWorker.addEventListener("message", function(event){
processWorkerNavigatorObject(event.data, "ServiceWorker");
registration.unregister();
});
if (worker.state !== "activated"){
worker.addEventListener("statechange", function(){
if (worker.state === "activated"){
worker.postMessage("send");
}
});
}
else {
worker.postMessage("send");
}
return registration;
}).catch(function(error){
"use strict";
console.error("Unable to register service worker:", error);
2019-05-22 23:39:56 +02:00
});