2021-12-31 00:27:45 +00:00
|
|
|
// debug friend: document.writeln(JSON.stringify(value));
|
2022-01-06 21:10:26 +00:00
|
|
|
window.webxdc = (() => {
|
|
|
|
var updateListener = () => {};
|
2022-01-02 23:18:15 +00:00
|
|
|
var updatesKey = "__xdcUpdatesKey__";
|
2022-01-06 21:10:26 +00:00
|
|
|
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
|
|
|
}
|
2022-01-06 21:10:26 +00:00
|
|
|
});
|
2022-01-02 23:18:15 +00:00
|
|
|
|
|
|
|
return {
|
2022-01-06 21:10:26 +00:00
|
|
|
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
|
|
|
},
|
2022-01-06 21:10:26 +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
|
|
|
},
|
2021-12-31 00:27:45 +00:00
|
|
|
sendUpdate: (description, payload) => {
|
|
|
|
// alert(description+"\n\n"+JSON.stringify(payload));
|
2022-01-02 23:18:15 +00:00
|
|
|
var update = {payload: payload};
|
2022-01-06 21:10:26 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2022-01-06 21:10:26 +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
|
|
|
|
2022-01-06 21:10:26 +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
|
|
|
|
2022-01-06 21:10:26 +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
|
|
|
}
|
|
|
|
|
2022-01-06 21:10:26 +00:00
|
|
|
window.clearXdcStorage = () => {
|
|
|
|
window.localStorage.clear();
|
|
|
|
window.location.reload();
|
2022-01-02 23:18:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 21:10:26 +00:00
|
|
|
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;';
|
2021-12-31 09:26:06 +00:00
|
|
|
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);
|
2021-12-31 09:26:06 +00:00
|
|
|
}
|
2022-01-01 17:47:14 +00:00
|
|
|
title.innerText = window.webxdc.selfAddr();
|
2021-12-31 09:26:06 +00:00
|
|
|
|
2022-01-06 21:10:26 +00:00
|
|
|
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 + '">' +
|
2022-01-06 21:10:26 +00:00
|
|
|
'<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 09:26:06 +00:00
|
|
|
}
|
2021-12-31 00:27:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 21:10:26 +00:00
|
|
|
window.addEventListener("load", window.alterXdcApp);
|