1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
(() => {
const br = chrome || browser;
const s = document.createElement("script");
const src = br.runtime.getURL("webauthn.js");
//const bg = br.runtime.connect({'name': 'content-script'});
const site = window.location.hostname;
let pagePorts = {};
s.setAttribute('src', src);
s.setAttribute('id', "sphinx-webauthn-page-script");
(document.head || document.documentElement).appendChild(s);
window.addEventListener('message', webauthnEventHandler);
br.runtime.onMessage.addListener((m) => {
let port = pagePorts[m.results.id];
port.postMessage(m.results);
delete pagePorts[m.results.id];
});
function genId() {
return Math.random().toString(32).slice(2) + Math.random().toString(32).slice(2);
}
function webauthnEventHandler(msg) {
let options = msg.data;
if(!options.type || options.type != "sphinxWebauthnEvent") {
return;
}
if(msg.origin != window.origin) {
console.log("invalid webauthnEvent sender");
return;
}
const site = window.location.hostname;
// TODO
let bgMsg = {
"site": site,
"action": options.action,
"params": options.params,
"id": genId(),
};
pagePorts[bgMsg.id] = msg.ports[0];
br.runtime.sendMessage(bgMsg);
//br.runtime.sendMessage(bgMsg).then(
// function(response) {
// console.log('response received from bg', response);
// pagePort.postMessage(response);
// },
// function(error) {
// console.log('error received from bg', error);
// pagePort.postMessage(error);
// }
//);
}
})();
|