-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_control.js
62 lines (58 loc) · 1.88 KB
/
js_control.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
// ==UserScript==
// @name js_control
// @namespace js_control
// @author DiamondDemon
// @description Remotely controls browser
// @include *
// @version 1.0.0
// ==/UserScript==
//Websocket constants, can be used by python
let ws
let tabdata
let send = (data) => {
ws.send(data)
}
//
let ws_init = (event) => {
tabdata = JSON.parse(JSON.stringify({"url": document.location.href, "title": document.title}))
var initdata = JSON.stringify({"type": "meta", "data": tabdata})
send(initdata)
console.log(initdata)
}
let ws_message = (event) => {
try {
var msg = JSON.parse(event.data)
} catch(err) {
return
}
console.log(msg)
if (msg === {}) {
return
} else if (msg.type === "meta") {
console.log("command detected")
if ( !(msg.data.command) ) {
return
} else if (msg.data.command === "resend_info") {
send(JSON.stringify({"type": "meta", "data": tabdata}) + '\n')
} else if (msg.data.command === "ping" && msg.tab.url === tabdata.url) {
console.log("hello")
send(JSON.stringify({"type": "meta", "tab": tabdata, "data": "pong"}))
}
} else if ( (!(msg.tab)) || (msg.tab.url !== tabdata.url) ) {
return
} else if (msg.type === "data" && msg.data.script) {
var tosend
try {
tosend = JSON.stringify({"type": "data", "tab": tabdata, "data": {"return": JSON.stringify(eval(msg.data.script)).replace(/(^"|"$)/g, '')}})
} catch(err) {
tosend = JSON.stringify({"type": "data", "tab": tabdata, "data": {"error": Error.prototype.toString.call(err)}})
} finally {
send(tosend)
console.log(tosend)
}
}
}
ws = new WebSocket("ws://localhost:16384")
ws.addEventListener("open", ws_init)
ws.addEventListener("message", ws_message)
console.log("Started up")