-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebxdc.js
141 lines (130 loc) · 4.77 KB
/
webxdc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// debug friend: document.writeln(JSON.stringify(value));
//@ts-check
/** @type {import("webxdc-types").WebXdc<any>} */
window.webxdc = (() => {
var updateListener = (_) => { };
var updatesKey = '__xdcUpdatesKey__';
window.addEventListener('storage', (event) => {
if (event.key == null) {
window.location.reload();
} else if (event.key === updatesKey) {
var updates = JSON.parse(event.newValue);
var update = updates[updates.length - 1];
update.max_serial = updates.length;
console.log('[Webxdc] ' + JSON.stringify(update));
updateListener(update);
}
});
function getUpdates() {
var updatesJSON = window.localStorage.getItem(updatesKey);
return updatesJSON ? JSON.parse(updatesJSON) : [];
}
var params = new URLSearchParams(window.location.search.substr(1));
return {
selfAddr: params.get('addr') || '[email protected]',
selfName: params.get('name') || 'device0',
setUpdateListener: (cb, serial = 0) => {
var updates = getUpdates();
var maxSerial = updates.length;
updates.forEach((update) => {
if (update.serial > serial) {
update.max_serial = maxSerial;
cb(update);
}
});
updateListener = cb;
return Promise.resolve();
},
getAllUpdates: () => {
console.log('[Webxdc] WARNING: getAllUpdates() is deprecated.');
return Promise.resolve([]);
},
sendUpdate: (update, description) => {
var updates = getUpdates();
var serial = updates.length + 1;
var _update = {
payload: update.payload,
summary: update.summary,
info: update.info,
serial: serial,
};
updates.push(_update);
window.localStorage.setItem(updatesKey, JSON.stringify(updates));
_update.max_serial = serial;
console.log(
'[Webxdc] description="' + description + '", ' + JSON.stringify(_update)
);
updateListener(_update);
},
};
})();
window.addXdcPeer = () => {
var loc = window.location;
// get next peer ID
var params = new URLSearchParams(loc.search.substr(1));
var peerId = Number(params.get('next_peer')) || 1;
// 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);
// update next peer ID
params.set('next_peer', String(peerId + 1));
window.location.search = '?' + params.toString();
};
window.clearXdcStorage = () => {
window.localStorage.clear();
window.location.reload();
};
window.alterXdcApp = () => {
var styleControlPanel =
'position: fixed; bottom:1em; left:1em; background-color: #000; opacity:0.8; padding:.5em; font-size:16px; font-family: sans-serif; color:#fff; z-index: 9999';
var styleMenuLink =
'color:#fff; text-decoration: none; vertical-align: middle';
var styleAppIcon =
'height: 1.5em; width: 1.5em; margin-right: 0.5em; border-radius:10%; vertical-align: middle';
var title = document.getElementsByTagName('title')[0];
if (typeof title == 'undefined') {
title = document.createElement('title');
document.getElementsByTagName('head')[0].append(title);
}
title.innerText = window.webxdc.selfAddr;
if (window.webxdc.selfName === 'device0') {
var div = document.createElement('div');
div.innerHTML =
'<div id="webxdc-panel" style="' +
styleControlPanel +
'">' +
'<a href="javascript:window.addXdcPeer();" style="' +
styleMenuLink +
'">Add Peer</a>' +
'<span style="' +
styleMenuLink +
'"> | </span>' +
'<a id="webxdc-panel-clear" href="javascript:window.clearXdcStorage();" style="' +
styleMenuLink +
'">Clear Storage</a>' +
'<div>';
var controlPanel = div.firstChild;
function loadIcon(name) {
var tester = new Image();
tester.onload = () => {
div.innerHTML = '<img src="' + name + '" style="' + styleAppIcon + '">';
controlPanel.insertBefore(div.firstChild, controlPanel.firstChild);
};
tester.src = name;
}
loadIcon('icon.png');
loadIcon('icon.jpg');
document.getElementsByTagName('body')[0].append(controlPanel);
}
};
window.addEventListener('load', window.alterXdcApp);