-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
30 lines (26 loc) · 821 Bytes
/
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
function injectCode(code) {
return Function(code)();
}
function downloadScript(code) {
var el = document.createElement("a");
el.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(code));
el.setAttribute("download", "userscript.js");
el.style.display = "none";
document.body.appendChild(el);
el.click();
document.body.removeChild(el);
}
function handleMessage(event) {
if (event.name === "SEND_SAVED_CODE") {
injectCode(unescape(event.message.code));
}
if (event.name === "DOWNLOAD_SCRIPT") {
if (window.top === window) {
downloadScript(event.message.code)
}
}
}
if (window.top === window) {
safari.extension.dispatchMessage("REQUEST_SAVED_CODE");
}
safari.self.addEventListener("message", handleMessage);