forked from acenturyandabit/polymorph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptrunner.js
More file actions
executable file
·99 lines (87 loc) · 3.39 KB
/
Copy pathscriptrunner.js
File metadata and controls
executable file
·99 lines (87 loc) · 3.39 KB
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
core.registerOperator("scriptrunner", {
displayName: "Scriptrunner",
description: "Runs scripts."
}, function (container) {
let me = this;
me.container = container;//not strictly compulsory bc this is expected and automatically enforced - just dont touch it pls.
this.settings = {};
this.rootdiv = document.createElement("div");
//Add content-independent HTML here.
this.rootdiv.innerHTML = `
<h1>WARNING: THIS SCRIPT IS POTENTIALLY INSECURE. ONLY RUN TRUSTED SCRIPTS.</h1>
<p>Press 'Update' to execute this script.</p>
<textarea style="width: 100%; height: 50%"; placeholder="Enter script here:"></textarea>
<br>
<button>Update</button>
<div id="output" style="overflow-y: auto; height: 30%;"></div>
`;
/*Example script:*/
/*
instance.on("updateItem",(d)=>{
console.log(core.items[d.id]);
})
*/
container.div.appendChild(this.rootdiv);
//////////////////Handle core item updates//////////////////
//this is called when an item is updated (e.g. by another operator)
core.on("updateItem", (d) => {
if (d.sender!="GARBAGE_COLLECTOR"){
if (this.currentInstance)this.currentInstance.fire("updateItem", d);
}
return false;
});
//Saving and loading
this.toSaveData = function () {
return this.settings;
}
function instance() {
this.log = function (data) {
let p = document.createElement("p");
p.innerHTML = JSON.stringify(data);
me.rootdiv.querySelector("#output").appendChild(p);
}
this.logEx=(data)=>{
this.log(String(data))
}
addEventAPI(this,this.logEx);
}
this.execute = () => {
this.currentInstance = new instance();
let wrapped = `(function factory(instance){
${this.settings.script}
})`;
eval(wrapped)(this.currentInstance);
}
this.fromSaveData = (d) => {
//this is called when your operator is started OR your operator loads for the first time
Object.assign(this.settings, d);
this.rootdiv.querySelector("textarea").value = this.settings.script || "";
//don't execute, just flag this as needing attention
textarea.style.background="green";
}
this.rootdiv.querySelector("button").addEventListener("click", () => {
textarea.style.background="white";
this.settings.script = this.rootdiv.querySelector("textarea").value;
this.execute();
})
//Handle the settings dialog click!
this.dialogDiv = document.createElement("div");
this.dialogDiv.innerHTML = `WARNING: DO NOT ACCEPT OTHERS' SCRIPTS IN GENERAL!`;
this.showDialog = function () {
// update your dialog elements with your settings
}
this.dialogUpdateSettings = function () {
// pull settings and update when your dialog is closed.
}
//Allow tab to work
let textarea = this.rootdiv.querySelector('textarea');
textarea.addEventListener("keydown", (e) => {
textarea.style.background="lightgreen";
if (e.keyCode == 9 || e.which == 9) {
e.preventDefault();
var s = e.target.selectionStart;
e.target.value = e.target.value.substring(0, e.target.selectionStart) + "\t" + e.target.value.substring(e.target.selectionEnd);
e.target.selectionEnd = s + 1;
}
});
});