|
| 1 | +// debug friend: document.writeln(JSON.stringify(value)); |
| 2 | +window.webxdc = (() => { |
| 3 | + var updateListener = () => {}; |
| 4 | + var updatesKey = "__xdcUpdatesKey__"; |
| 5 | + window.addEventListener('storage', (event) => { |
| 6 | + if (event.key == null) { |
| 7 | + window.location.reload(); |
| 8 | + } else if (event.key === updatesKey) { |
| 9 | + var updates = JSON.parse(event.newValue); |
| 10 | + var update = updates[updates.length-1]; |
| 11 | + console.log("[Webxdc] " + JSON.stringify(update)); |
| 12 | + updateListener(update); |
| 13 | + } |
| 14 | + }); |
| 15 | + |
| 16 | + return { |
| 17 | + selfAddr: () => { |
| 18 | + var params = new URLSearchParams(window.location.hash.substr(1)); |
| 19 | + return params.get("addr") || "device0@local.host"; |
| 20 | + }, |
| 21 | + selfName: () => { |
| 22 | + var params = new URLSearchParams(window.location.hash.substr(1)); |
| 23 | + return params.get("name") || "device0"; |
| 24 | + }, |
| 25 | + setUpdateListener: (cb) => (updateListener = cb), |
| 26 | + getAllUpdates: () => { |
| 27 | + var updatesJSON = window.localStorage.getItem(updatesKey); |
| 28 | + return updatesJSON ? JSON.parse(updatesJSON) : []; |
| 29 | + }, |
| 30 | + sendUpdate: (update, description) => { |
| 31 | + // alert(description+"\n\n"+JSON.stringify(payload)); |
| 32 | + update = {payload: update.payload, summary: update.summary, info: update.info} |
| 33 | + console.log('[Webxdc] description="' + description + '", ' + JSON.stringify(update)); |
| 34 | + updateListener(update); |
| 35 | + var updatesJSON = window.localStorage.getItem(updatesKey); |
| 36 | + var updates = updatesJSON ? JSON.parse(updatesJSON) : []; |
| 37 | + updates.push(update); |
| 38 | + window.localStorage.setItem(updatesKey, JSON.stringify(updates)); |
| 39 | + }, |
| 40 | + }; |
| 41 | +})(); |
| 42 | + |
| 43 | +window.addXdcPeer = () => { |
| 44 | + var loc = window.location; |
| 45 | + // get next peer ID |
| 46 | + var params = new URLSearchParams(loc.hash.substr(1)); |
| 47 | + var peerId = Number(params.get("next_peer")) || 1; |
| 48 | + |
| 49 | + // open a new window |
| 50 | + var peerName = "device" + peerId; |
| 51 | + var url = loc.protocol + "//" + loc.host + loc.pathname + "#name=" + peerName + "&addr=" + peerName + "@local.host"; |
| 52 | + window.open(url); |
| 53 | + |
| 54 | + // update next peer ID |
| 55 | + params.set("next_peer", peerId + 1); |
| 56 | + window.location.hash = "#" + params.toString(); |
| 57 | +} |
| 58 | + |
| 59 | +window.clearXdcStorage = () => { |
| 60 | + window.localStorage.clear(); |
| 61 | + window.location.reload(); |
| 62 | +} |
| 63 | + |
| 64 | +window.alterXdcApp = () => { |
| 65 | + var styleControlPanel = 'position: fixed; bottom:1em; left:1em; background-color: #000; opacity:0.8; padding:.5em; font-family: sans-serif; color:#fff; z-index: 9999'; |
| 66 | + var styleMenuLink = 'color:#fff; text-decoration: none; vertical-align: middle'; |
| 67 | + var styleAppIcon = 'height: 1.5em; width: 1.5em; margin-right: 0.5em; border-radius:10%; vertical-align: middle'; |
| 68 | + var title = document.getElementsByTagName('title')[0]; |
| 69 | + if (typeof title == 'undefined') { |
| 70 | + title = document.createElement('title'); |
| 71 | + document.getElementsByTagName('head')[0].append(title); |
| 72 | + } |
| 73 | + title.innerText = window.webxdc.selfAddr(); |
| 74 | + |
| 75 | + if (window.webxdc.selfName() === "device0") { |
| 76 | + var div = document.createElement('div'); |
| 77 | + div.innerHTML = |
| 78 | + '<div style="' + styleControlPanel + '">' + |
| 79 | + '<a href="javascript:window.addXdcPeer();" style="' + styleMenuLink + '">Add Peer</a>' + |
| 80 | + '<span style="' + styleMenuLink + '"> | </span>' + |
| 81 | + '<a href="javascript:window.clearXdcStorage();" style="' + styleMenuLink + '">Clear Storage</a>' + |
| 82 | + '<div>'; |
| 83 | + var controlPanel = div.firstChild; |
| 84 | + |
| 85 | + function loadIcon(name) { |
| 86 | + var tester = new Image(); |
| 87 | + tester.onload = () => { |
| 88 | + div.innerHTML = '<img src="' + name + '" style="' + styleAppIcon +'">'; |
| 89 | + controlPanel.insertBefore(div.firstChild, controlPanel.firstChild); |
| 90 | + }; |
| 91 | + tester.src = name; |
| 92 | + } |
| 93 | + loadIcon("icon.png"); |
| 94 | + loadIcon("icon.jpg"); |
| 95 | + |
| 96 | + document.getElementsByTagName('body')[0].append(controlPanel); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +window.addEventListener("load", window.alterXdcApp); |
0 commit comments