2021-12-31 00:27:45 +00:00
|
|
|
// debug friend: document.writeln(JSON.stringify(value));
|
2022-01-02 23:18:15 +00:00
|
|
|
window.xdcStorage = (() => {
|
|
|
|
var updatesKey = "__xdcUpdatesKey__";
|
|
|
|
var fakeStorage = {
|
|
|
|
_data: {},
|
|
|
|
|
|
|
|
setItem: function (id, val) {
|
|
|
|
return this._data[id] = String(val);
|
|
|
|
},
|
|
|
|
|
|
|
|
getItem: function (id) {
|
|
|
|
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeItem: function (id) {
|
|
|
|
return delete this._data[id];
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function () {
|
|
|
|
return this._data = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var localStorageSupported = () => {
|
|
|
|
var testKey = "__xdcTestKey__";
|
|
|
|
try {
|
|
|
|
var storage = window.localStorage;
|
|
|
|
storage.setItem(testKey, "1");
|
|
|
|
storage.removeItem(testKey);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var storage = localStorageSupported() ? window.localStorage : fakeStorage;
|
|
|
|
|
|
|
|
return {
|
|
|
|
getUpdates: () => {
|
|
|
|
var updatesJSON = storage.getItem(updatesKey);
|
|
|
|
return updatesJSON ? JSON.parse(updatesJSON) : [];
|
|
|
|
},
|
|
|
|
saveUpdate: (update) => {
|
|
|
|
var updates = window.xdcStorage.getUpdates();
|
|
|
|
updates.push(update);
|
|
|
|
storage.setItem(updatesKey, JSON.stringify(updates));
|
|
|
|
},
|
|
|
|
clear: () => {
|
|
|
|
storage.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2021-12-31 00:27:45 +00:00
|
|
|
|
2022-01-01 17:47:14 +00:00
|
|
|
window.webxdc = (() => {
|
2021-12-31 00:27:45 +00:00
|
|
|
var updateListener = () => {};
|
|
|
|
|
|
|
|
return {
|
2021-12-31 09:26:06 +00:00
|
|
|
selfAddr: () => window.xdcSelfAddr || "device0@local.host",
|
2022-01-02 20:47:10 +00:00
|
|
|
selfName: () => window.xdcSelfName || "device0",
|
2021-12-31 00:27:45 +00:00
|
|
|
setUpdateListener: (cb) => (window.xdcUpdateListener = cb),
|
2022-01-02 23:18:15 +00:00
|
|
|
getAllUpdates: () => {return getXdcRoot().xdcStorage.getUpdates();},
|
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};
|
|
|
|
getXdcRoot().xdcStorage.saveUpdate(update);
|
2021-12-31 00:27:45 +00:00
|
|
|
var all = getAllXdcWindows();
|
|
|
|
all.forEach((w) => {
|
|
|
|
//alert(w.xdcUpdateListener);
|
2022-01-02 21:19:04 +00:00
|
|
|
w.xdcUpdateListener(update);
|
2021-12-31 00:27:45 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
window.allXdcWindows = [window];
|
|
|
|
window.xdcUpdateListener = 12;
|
|
|
|
|
2022-01-02 23:18:15 +00:00
|
|
|
var styleControlPanel = 'position: fixed; bottom:1em; left:1em; background-color: #000; opacity:0.8; padding:.5em; font-family: sans-serif; width: 50%;color:#fff;';
|
2021-12-31 00:27:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-02 23:18:15 +00:00
|
|
|
function addXdcPeer() {
|
2021-12-31 00:27:45 +00:00
|
|
|
var xdcChild = window.open(window.location);
|
|
|
|
var xdcRoot = getXdcRoot();
|
|
|
|
xdcChild.xdcRoot = xdcRoot;
|
2022-01-02 20:47:10 +00:00
|
|
|
xdcChild.xdcSelfName = "device" + getAllXdcWindows().length;
|
|
|
|
xdcChild.xdcSelfAddr = xdcChild.xdcSelfName + "@local.host";
|
2021-12-31 00:27:45 +00:00
|
|
|
xdcRoot.allXdcWindows.push(xdcChild);
|
|
|
|
}
|
|
|
|
|
2022-01-02 23:18:15 +00:00
|
|
|
function clearXdcStorage() {
|
|
|
|
getXdcRoot().xdcStorage.clear();
|
|
|
|
alert("Done.");
|
|
|
|
}
|
|
|
|
|
2021-12-31 09:26:06 +00:00
|
|
|
function alterApp() {
|
|
|
|
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-02 21:01:17 +00:00
|
|
|
if (getXdcRoot() === window) {
|
|
|
|
var div = document.createElement('div');
|
|
|
|
div.innerHTML =
|
|
|
|
'<div style="' + styleControlPanel + '">' +
|
2022-01-02 23:26:38 +00:00
|
|
|
'<a href="javascript:addXdcPeer();" style="' + styleMenuLink + '">Add Peer</a> | ' +
|
|
|
|
'<a href="javascript:clearXdcStorage();" style="' + styleMenuLink + '">Clear Storage</a>' +
|
2022-01-02 21:01:17 +00:00
|
|
|
'<div>';
|
|
|
|
document.getElementsByTagName('body')[0].append(div.firstChild);
|
2021-12-31 09:26:06 +00:00
|
|
|
}
|
2021-12-31 00:27:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 09:26:06 +00:00
|
|
|
window.addEventListener("load", alterApp);
|