jsnes/webxdc.js

73 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-12-31 00:27:45 +00:00
// debug friend: document.writeln(JSON.stringify(value));
// maybe helpful: window.sessionStorage, https://www.w3schools.com/html/html5_webstorage.asp
2022-01-01 17:47:14 +00:00
window.webxdc = (() => {
2021-12-31 00:27:45 +00:00
var updateListener = () => {};
return {
selfAddr: () => window.xdcSelfAddr || "device0@local.host",
2021-12-31 00:27:45 +00:00
setUpdateListener: (cb) => (window.xdcUpdateListener = cb),
getAllUpdates: () => {
return JSON.parse(
'[]'
//'[{"action":"configure", "question":"your favorite colorxx", "answers":["red","green","blue","yellow","purple1"]},{"action":"vote", "sender":"foo2@bar.de", "vote":0},{"action":"vote", "sender":"foo@bar.de", "vote":0}]'
);
},
sendUpdate: (description, payload) => {
// alert(description+"\n\n"+JSON.stringify(payload));
var all = getAllXdcWindows();
all.forEach((w) => {
//alert(w.xdcUpdateListener);
w.xdcUpdateListener({payload: payload});
});
},
};
})();
window.allXdcWindows = [window];
window.xdcUpdateListener = 12;
var styleControlPanel = 'position: fixed; bottom:1em; left:1em; background-color: #000; opacity:0.8; padding:.5em; font-family: sans-serif; width: 50%;';
var styleMenuLink = 'color:#fff; text-decoration: none;';
function getXdcRoot() {
var test = window;
while (typeof test.xdcRoot != 'undefined') {
test = test.xdcRoot;
}
return test;
}
function getAllXdcWindows() {
var xdcRoot = getXdcRoot();
return xdcRoot.allXdcWindows;
}
function addPeer() {
var xdcChild = window.open(window.location);
var xdcRoot = getXdcRoot();
xdcChild.xdcRoot = xdcRoot;
xdcChild.xdcSelfAddr = "device" + xdcRoot.allXdcWindows.length + "@local.host";
2021-12-31 00:27:45 +00:00
xdcRoot.allXdcWindows.push(xdcChild);
}
function alterApp() {
var title = document.getElementsByTagName('title')[0];
if (typeof title == 'undefined') {
title = document.createElement('title');
document.getElementsByTagName('head')[0].append(title);
}
2022-01-01 17:47:14 +00:00
title.innerText = window.webxdc.selfAddr();
if (getXdcRoot() == window) {
var div = document.createElement('div');
div.innerHTML =
'<div style="' + styleControlPanel + '">' +
'<a href="javascript:addPeer();" style="' + styleMenuLink + '">Add Peer</a>' +
'<div>';
document.getElementsByTagName('body')[0].append(div.firstChild);
}
2021-12-31 00:27:45 +00:00
}
window.addEventListener("load", alterApp);