jsnes/webxdc.js

83 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-12-31 00:27:45 +00:00
// debug friend: document.writeln(JSON.stringify(value));
window.webxdc = (() => {
var updateListener = () => {};
var updatesKey = "__xdcUpdatesKey__";
window.addEventListener('storage', (event) => {
if (event.key == null) {
window.location.reload();
} else if (event.key === updatesKey) {
var updates = JSON.parse(event.newValue);
updateListener(updates[updates.length-1]);
2022-01-02 23:34:13 +00:00
}
});
return {
selfAddr: () => {
var params = new URLSearchParams(window.location.hash.substr(1));
return params.get("addr") || "device0@local.host";
2022-01-02 23:34:13 +00:00
},
selfName: () => {
var params = new URLSearchParams(window.location.hash.substr(1));
return params.get("name") || "device0";
},
setUpdateListener: (cb) => (updateListener = cb),
getAllUpdates: () => {
var updatesJSON = window.localStorage.getItem(updatesKey);
return updatesJSON ? JSON.parse(updatesJSON) : [];
2022-01-02 23:34:13 +00:00
},
2022-01-07 00:16:58 +00:00
sendUpdate: (payload, description) => {
2021-12-31 00:27:45 +00:00
// alert(description+"\n\n"+JSON.stringify(payload));
var update = {payload: payload};
updateListener(update);
var updatesJSON = window.localStorage.getItem(updatesKey);
var updates = updatesJSON ? JSON.parse(updatesJSON) : [];
updates.push(update);
window.localStorage.setItem(updatesKey, JSON.stringify(updates));
2021-12-31 00:27:45 +00:00
},
};
})();
window.addXdcPeer = () => {
var loc = window.location;
// get next peer ID
var params = new URLSearchParams(loc.hash.substr(1));
var peerId = Number(params.get("next_peer")) || 1;
2021-12-31 00:27:45 +00:00
// open a new window
var peerName = "device" + peerId;
var url = loc.protocol + "//" + loc.host + loc.pathname + "#name=" + peerName + "&addr=" + peerName + "@local.host";
window.open(url);
2021-12-31 00:27:45 +00:00
// update next peer ID
params.set("next_peer", peerId + 1);
window.location.hash = "#" + params.toString();
2021-12-31 00:27:45 +00:00
}
window.clearXdcStorage = () => {
window.localStorage.clear();
window.location.reload();
}
window.alterXdcApp = () => {
var styleControlPanel = 'position: fixed; bottom:1em; left:1em; background-color: #000; opacity:0.8; padding:.5em; font-family: sans-serif; width: 50%;color:#fff;';
var styleMenuLink = 'color:#fff; text-decoration: none;';
var title = document.getElementsByTagName('title')[0];
if (typeof title == 'undefined') {
2022-01-02 21:01:17 +00:00
title = document.createElement('title');
document.getElementsByTagName('head')[0].append(title);
}
2022-01-01 17:47:14 +00:00
title.innerText = window.webxdc.selfAddr();
if (window.webxdc.selfName() === "device0") {
2022-01-02 21:01:17 +00:00
var div = document.createElement('div');
div.innerHTML =
2022-01-02 23:34:13 +00:00
'<div style="' + styleControlPanel + '">' +
'<a href="javascript:window.addXdcPeer();" style="' + styleMenuLink + '">Add Peer</a> | ' +
'<a href="javascript:window.clearXdcStorage();" style="' + styleMenuLink + '">Clear Storage</a>' +
2022-01-02 23:34:13 +00:00
'<div>';
2022-01-02 21:01:17 +00:00
document.getElementsByTagName('body')[0].append(div.firstChild);
}
2021-12-31 00:27:45 +00:00
}
window.addEventListener("load", window.alterXdcApp);