-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptions.js
168 lines (152 loc) · 5.6 KB
/
options.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// jshint esversion: 6
var stubs = {
'notify': // simple notify command
`// basic command template with notification
CmdUtils.CreateCommand({
name: "1example",
description: "A short description of your command.",
author: "Your Name",
icon: "http://www.mozilla.com/favicon.ico",
execute: function execute(args) {
CmdUtils.notify("Execute:Your input is: " + args.text);
},
preview: function preview(pblock, args) {
pblock.innerHTML = "Preview:Your input is " + args.text + ".";
},
});
`
, 'search': // simple search / preview command (e. g. using ajax)
`// a simple search template
CmdUtils.CreateCommand({
name: "2example",
description: "Commence DuckGoGo search.",
icon: "http://www.duckduckgo.com/favicon.ico",
execute: function execute(args) {
CmdUtils.addTab("https://duckduckgo.com/?q=" + encodeURIComponent(args.text));
},
preview: function preview(pblock, args) {
var url = "https://duckduckgo.com/html?q=" + encodeURIComponent(args.text);
CmdUtils.ajaxGet(url, function(data) {
pblock.innerHTML = jQuery("#links", data).html();
});
},
});
`
, 'enhanced-search': // enhanced search / preview command
`// search template
CmdUtils.makeSearchCommand({
name: ["3example"],
description: "Searches quant.com",
icon: "🔎",
url: "https://www.qwant.com/?t=all&q={QUERY}",
prevAttrs: {zoom: 0.75, scroll: [100/*x*/, 0/*y*/], anchor: ["c_13", "c_22"]},
});
`
, 'options': // command with options
`
// template to demonstrate preview options, browse them with Ctrl+up/down arrows
CmdUtils.CreateCommand({
name: ["4example", "optionexample"],
execute: function execute(args) {
CmdUtils.setTip("chosen option idx:"+args._opt_idx+" chosen option val:"+args._opt_val+"<hr>");
},
preview: function preview(pblock, args) {
pblock.innerHTML = "use Ctrl+↓/↑ to choose an option, then press Enter";
pblock.innerHTML += "<div data-option data-option-value=one>option 1</div>";
pblock.innerHTML += "<div data-option data-option-value=two>option 2</div>";
pblock.innerHTML += "<div data-option data-option-value=thr>option 3</div>";
pblock.innerHTML += "<div data-option data-option-value=fou>option 4</div>";
pblock.innerHTML += "<div data-option data-option-value=fiv>option 5</div>";
},
});
`
};
// inserts stub (example command)
function insertExampleStub() {
var stub = stubs[this.id];
editor.replaceRange(stub, editor.getCursor());
saveScripts();
return false;
}
// evaluates and saves scripts from editor
async function saveScripts(instance, changeObj) {
// console.log("saveScripts",instance);
// console.log("saveScripts",changeObj);
var customscripts = editor.getValue();
// download link
var a = document.getElementById("download");
var file = new Blob([customscripts], {type: "text/plain"});
a.href = URL.createObjectURL(file);
a.download = "ubichr-custom-scripts-"+(new Date()).toISOString().substring(0,10)+".js";
if (changeObj && changeObj.origin=='setValue') return; // save on user input
if (customscripts.trim()=="") {
console.trace();
if (confirm("Are your sure you want remove all your scripts?")) {
editor.setValue("// removed all the scripts");
} else {
await chrome.storage.local.get('customscripts', function(result) { editor.setValue(result.customscripts || "// storage was empty, sorry"); });
}
}
// save
if (typeof chrome !== 'undefined' && chrome.storage) {
chrome.storage.local.set({'customscripts': customscripts});
}
$("#info").html("");
// eval
try {
$("#info").html("evaluated!");
eval(customscripts);
CmdUtils.unloadCustomScripts();
CmdUtils.loadCustomScripts();
} catch (e) {
var m = e.message;
var l = /anonymous\>:(\d+)\:/.exec(e.stack);
if (l != null) {
l = l[1];
m += " <a href=# id=linerror>LINE:"+l+"</a>";
}
console.log(e)
console.log(l);
$("#info").html("<span style='background-color:red'>"+m+"</span>");
if (l != null) $("a#linerror").click( ()=>{ editor.setCursor({line:l,ch:0}); });
}
}
function saveCursorPos() {
if (typeof chrome !== 'undefined' && chrome.storage) {
chrome.storage.local.set({'cursor': editor.getCursor()});
}
}
// initializes editor
editor = CodeMirror.fromTextArea( document.getElementById("code"), {
mode: "javascript",
theme: "ambiance",
lineWrapping: true,
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
gutters: ["CodeMirror-lint-markers"],
lint:true,
autofocus:true,
extraKeys: {
'Ctrl-/': 'toggleComment',
'Ctrl-S': function(instance) { saveScripts(null, {origin:'setValue'}); document.getElementById("download").click(); },
}
});
editor.on("cursorActivity", saveCursorPos);
editor.on("change", saveScripts);
saveScripts(null, {origin:'setValue'}); // prepare initial download
Object.keys(stubs).forEach( k=> {
var s = $(`<a href=# id='${k}'>${k}</a>`);
$(s).click( insertExampleStub );
$("#stubs").append( s );
});
$("#stubs > a:not(:last)").after(" - ");
// load scripts
if (typeof chrome !== 'undefined' && chrome.storage) {
chrome.storage.local.get('customscripts', function(result) {
editor.setValue(result.customscripts || "// jshint esversion: 8\n// check out examples and HELP in lower right corner!");
});
chrome.storage.local.get('cursor', function(result) {
editor.setCursor(result.cursor || {line:0,ch:0});
});
}