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