-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_script.js
48 lines (40 loc) · 1.26 KB
/
content_script.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
const server = "wss://jsonecho.herokuapp.com"
WebSocket.prototype.sendJSON = function(m) { this.send(JSON.stringify(m)) }
if (typeof WebKitMutationObserver != 'function') {
throw Error('PageMirror requires MutationObserver.');
}
chrome.runtime.onMessage.addListener( ({id, peer}) => {
if (!peer) return
// Add <base> so the recipient can download necessary CSS, images, &c.
if ($("base").length < 1) {
base = location.href.match(/^(.*\/)[^\/]*$/)[1]
$("head").prepend($("<base>").attr("href", base))
console.log('Added <base href="' + base + '">')
}
sock = new WebSocket(server);
sock.onopen = () => {
sock.sendJSON({id})
}
sock.onmessage = m => {
try { d = JSON.parse(m.data) }
catch (err) { return; }
if (! d.begin) { return; }
var mirrorClient = new TreeMirrorClient(document, {
initialize: (rootId, children) => {
sock.sendJSON({
to: peer,
f: 'initialize',
args: [rootId, children]
});
},
applyChanged: (removed, addedOrMoved, attributes, text) => {
sock.sendJSON({
to: peer,
f: 'applyChanged',
args: [removed, addedOrMoved, attributes, text]
});
}
});
sock.onclose = mirrorClient.disconnect;
};
});