jsnes/webxdc.js

126 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-12-31 00:27:45 +00:00
// debug friend: document.writeln(JSON.stringify(value));
window.xdcStorage = (() => {
var updatesKey = "__xdcUpdatesKey__";
var fakeStorage = {
2022-01-02 23:34:13 +00:00
_data: {},
2022-01-02 23:34:13 +00:00
setItem: function (id, val) {
return this._data[id] = String(val);
},
2022-01-02 23:34:13 +00:00
getItem: function (id) {
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
},
2022-01-02 23:34:13 +00:00
removeItem: function (id) {
return delete this._data[id];
},
2022-01-02 23:34:13 +00:00
clear: function () {
return this._data = {};
}
};
var localStorageSupported = () => {
2022-01-02 23:34:13 +00:00
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 {
2022-01-02 23:34:13 +00:00
getUpdates: () => {
var updatesJSON = storage.getItem(updatesKey);
return updatesJSON ? JSON.parse(updatesJSON) : [];
2022-01-02 23:34:13 +00:00
},
saveUpdate: (update) => {
var updates = window.xdcStorage.getUpdates();
updates.push(update);
storage.setItem(updatesKey, JSON.stringify(updates));
2022-01-02 23:34:13 +00:00
},
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 {
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),
getAllUpdates: () => {return getXdcRoot().xdcStorage.getUpdates();},
2021-12-31 00:27:45 +00:00
sendUpdate: (description, payload) => {
// alert(description+"\n\n"+JSON.stringify(payload));
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;
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;
}
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);
}
function clearXdcStorage() {
getXdcRoot().xdcStorage.clear();
alert("Done.");
}
function alterXdcApp() {
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();
2022-01-02 21:01:17 +00:00
if (getXdcRoot() === window) {
var div = document.createElement('div');
div.innerHTML =
2022-01-02 23:34:13 +00:00
'<div style="' + styleControlPanel + '">' +
'<a href="javascript:addXdcPeer();" style="' + styleMenuLink + '">Add Peer</a> | ' +
'<a href="javascript:clearXdcStorage();" style="' + styleMenuLink + '">Clear Storage</a>' +
'<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", alterXdcApp);