-
Notifications
You must be signed in to change notification settings - Fork 8
/
script.js
346 lines (321 loc) · 13.2 KB
/
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: magic;
// This script was downloaded using ScriptDude.
// Do not remove these lines, if you want to benefit from automatic updates.
// source: https://scriptdu.de/script.js; docs: https://scriptdu.de/; hash: -1503760114;
class ScriptDude {
constructor() {
try {
this.fileManager = FileManager.iCloud()
} catch(e) {
this.fileManager = FileManager.local()
}
this.documentsDirectory = this.fileManager.documentsDirectory()
this.updateableScripts = [];
this.uptodateScripts = [];
this.table = new UITable();
}
ensureCorrectScriptNaming() {
const scriptName = 'ScriptDude';
if(Script.name() != scriptName) {
let alert = new Alert();
alert.title = 'Wrong script name';
alert.message = `In order to work properly, this script needs to be named "${scriptName}". Please rename the script by clicking on its title and run it again.`;
alert.addCancelAction("Okay");
alert.presentAlert();
Script.complete();
throw "Wrong script name";
}
}
makeUrlUpdateable(url) {
// Strip revision from gist
if(url.startsWith("https://gist.githubusercontent.com/")) {
let parts = url.split('/');
if(parts.length == 8) {
url = parts.filter((el, i) => i != 6).join('/');
}
}
return url;
}
hashCode(input) {
return Array.from(input).reduce((accumulator, currentChar) => Math.imul(31, accumulator) + currentChar.charCodeAt(0), 0)
}
render() {
this.table.removeAllRows();
this.getPackageUI().map(row => this.table.addRow(row));
this.table.reload();
}
getPackageUI() {
let rows = [];
if(this.updateableScripts.length) {
rows.push(...this.getPackageUISection("Updates", this.updateableScripts));
}
if(this.uptodateScripts.length) {
rows.push(...this.getPackageUISection("Installed", this.uptodateScripts));
}
let manualInstall = new UITableRow();
let manualInstallButton = manualInstall.addButton("Install from URL");
manualInstallButton.onTap = () => {
this.getInstallationUI();
}
rows.push(manualInstall);
let scriptablesLink = new UITableRow();
let scriptablesLinkButton = scriptablesLink.addButton("Browse scriptables.net");
scriptablesLinkButton.onTap = () => {
Safari.open('https://scriptables.net/');
}
rows.push(scriptablesLink);
return rows;
}
getPackageUISection(title, scripts) {
let rows = [];
let header = new UITableRow();
let text = header.addText(title);
text.titleFont = Font.headline();
rows.push(header, ...scripts.map(this.getPackageUIRow.bind(this)), new UITableRow());
return rows;
}
mergeHeaders(codeOld, codeNew)
{
let icon = null, color = null;
if (!!codeOld)
{
let lines = codeOld.split("\n", 20);
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
const line = lines[lineIndex];
let match = line.match(/icon-color: ([\w\-]*);/);
if (match)
color = match[1];
match = line.match(/icon-glyph: ([\w\-]*);/);
if (match)
icon = match[1];
if (icon != null && color != null)
break;
}
}
let scriptableHeader = ""; // detect Scriptable header
if (codeNew.trim().startsWith('// Variables used by Scriptable'))
{
let lines = codeNew.split("\n"); // HINT: to improve speed, we could use just the first 20 lines of the code -> code.split("\n", 20);
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
let line = lines[lineIndex];
if (line.indexOf('// Variables used by Scriptable.') != -1 || line.indexOf('// These must be at the very top of the file') != -1)
{
scriptableHeader += line + "\n";
codeNew = codeNew.substr(line.length + 1); // Remove scriptable header from original code
}
else if (line.match(/\/\/( [\w\-]*: [\w\-]*;)+/)) // Find "key: value;" list used by scriptable
{
codeNew = codeNew.substr(line.length + 1); // Remove scriptable header from original code
let match = line.match(/icon-color: ([\w\-]*);/);
if (match)
line = line.replace(match[1], color);
match = line.match(/icon-glyph: ([\w\-]*);/);
if (match)
line = line.replace(match[1], icon);
scriptableHeader += line + "\n";
}
else
{
break;
}
}
}
else
{
scriptableHeader = `// Variables used by Scriptable.\n// These must be at the very top of the file. Do not edit.\n// icon-color: ${color || 'blue'}; icon-glyph: ${icon || 'circle'};\n`;
}
return [scriptableHeader, codeNew];
}
getPackageUIRow(script) {
const iconWidth = 60;
let row = new UITableRow();
let text = row.addText(script.name, script.source.split('/')[2]);
text.subtitleFont = Font.systemFont(10)
if(script.updateAvailable) {
let updateButton = row.addButton("Update");
updateButton.titleColor = Color.red()
updateButton.rightAligned();
updateButton.onTap = () => {
this.updateScript(script);
}
updateButton.widthWeight = iconWidth;
}
let documentationButton = row.addButton("Docs");
documentationButton.rightAligned();
documentationButton.onTap = () => {
Safari.open(script.docs)
}
documentationButton.widthWeight = iconWidth;
let width = Device.screenSize().width - (script.updateAvailable ? iconWidth*2 : iconWidth);
text.widthWeight = width;
return row;
}
async installScript(name, sourceUrl, documentationUrl, icon, color, showMessage) {
sourceUrl = this.makeUrlUpdateable(sourceUrl);
let filePath = this.fileManager.joinPath(this.documentsDirectory, name + '.js');
if(this.fileManager.fileExists(filePath)) {
let error = new Alert();
error.title = `A script with the name ${name} does already exist.`;
error.presentAlert();
return;
}
if(false != showMessage) {
let warning = new Alert();
warning.title = "Warning";
warning.message = `Scriptable scripts can access sensitive data on your device. Make sure to check downloaded scripts before running them for the first time. Do you want to continue downloading "${name}" from "${sourceUrl}"?`;
warning.addAction("Continue");
warning.addCancelAction("Cancel");
let result = await warning.presentAlert();
if(-1 == result) {
return;
}
}
let req = new Request(sourceUrl);
let codeOriginal = await req.loadString();
let hash = this.hashCode(codeOriginal);
let [scriptableHeader, code] = this.mergeHeaders(null, codeOriginal);
let codeToStore = Data.fromString(`${scriptableHeader}// This script was downloaded using ScriptDude.\n// Do not remove these lines, if you want to benefit from automatic updates.\n// source: ${sourceUrl}; docs: ${documentationUrl}; hash: ${hash};\n\n${code}`);
this.fileManager.write(filePath, codeToStore);
this.showLoadingIndicator();
this.updateScriptsData().then(() => { this.render() });
}
async getInstallationUI() {
let install = new Alert()
install.title = "Install"
install.message = "ScriptDude makes downloading and updating Scriptable scripts easy.";
install.addTextField("Script Name")
install.addTextField("Source URL")
install.addTextField("Documentation URL")
install.addAction("Install")
install.addCancelAction("Cancel")
let result = await install.presentAlert()
if(0 == result) {
let name = install.textFieldValue(0)
let source = install.textFieldValue(1)
let documentation = install.textFieldValue(2)
await this.installScript(name, source, documentation)
}
}
async run()
{
if(config.runsInWidget) {
await this.updateScriptsData();
Script.setWidget(this.getWidget())
} else {
this.ensureCorrectScriptNaming();
this.showLoadingIndicator();
await this.updateScriptsData();
this.render();
this.table.present(true);
this.checkForInstallationRequestFromWeb();
}
}
getWidget() {
let list = new ListWidget();
let header = list.addText("🧑🚀 ScriptDude".toUpperCase());
header.font = Font.mediumSystemFont(13);
list.addSpacer();
let number = list.addText(this.updateableScripts.length+"");
number.font = Font.largeTitle();
number.rightAlignText();
let title = list.addText("Updates".toUpperCase());
title.font = Font.mediumSystemFont(13);
title.rightAlignText();
list.refreshAfterDate = new Date(Date.now() + 60*60*1000);
return list;
}
checkForInstallationRequestFromWeb() {
try {
let data = args.queryParameters;
if(data.name && data.source && data.docs) {
this.installScript(data.name, data.source, data.docs, data.icon, data.color, true);
}
} catch(e) {
// Input malformed
}
}
showLoadingIndicator() {
let row = new UITableRow();
row.addText("Loading", "Please be patient, ScriptDude is collecting information about your scripts and checks for available updates.");
row.height = 100;
this.table.removeAllRows();
this.table.addRow(row);
this.table.reload();
}
updateScript(script)
{
let [scriptableHeader, code] = this.mergeHeaders(script.content, script.updatePayload.code);
let codeToStore = Data.fromString(`${scriptableHeader}// This script was downloaded using ScriptDude.\n// Do not remove these lines, if you want to benefit from automatic updates.\n// source: ${script.source}; docs: ${script.docs}; hash: ${script.updatePayload.hash};\n\n${code}`);
this.fileManager.write(script.path, codeToStore);
this.showLoadingIndicator();
this.updateScriptsData().then(() => { this.render() })
}
async updateScriptsData() {
let files = this.fileManager.listContents(this.documentsDirectory)
let managedScripts = files
// Convert to full paths
.map(fileName => this.fileManager.joinPath(this.documentsDirectory, fileName))
// Remove directories
.filter(filePath => !this.fileManager.isDirectory(filePath))
// Add file name and content metadata
.map(filePath => {
return {
path: filePath,
name: this.fileManager.fileName(filePath),
content: this.fileManager.read(filePath).toRawString()
};
})
// Filter for scripts that show up in Scriptable
.filter(file => file.content && file.content.trimLeft().startsWith("// Variables used by Scriptable."))
// Add source and origin metadata
.map(file => {
let potentialScriptData = file.content
.split("\n", 50) // Scan first max. 50 lines
.filter(line => line.length != 0) // Skip empty lines
.filter(line => line.indexOf('//') != -1) // Take only comments
.map(line => line.substr(2).trim()) // Remove comment slashes
.filter(line =>
line.indexOf('source:') != -1
&& line.indexOf('hash:') != -1
&& line.indexOf('docs:') != -1
);
if(!!potentialScriptData && potentialScriptData.length > 0) {
let customMetadata = potentialScriptData[0]
.split(';')
.map(keyValue => keyValue.split(':').map(text => text.trim()))
.filter(keyValue => keyValue[0].length)
.reduce((dict, addable) => {
dict[addable.shift()] = addable.join(':');
return dict;
}, {});
if(!!customMetadata['source']
&& !!customMetadata['hash']) {
file.source = this.makeUrlUpdateable(customMetadata['source']);
file.hash = customMetadata['hash'];
file.docs = customMetadata['docs'] || '';
}
}
return file;
})
// Filter for scripts managed by Scriptstore
.filter(file => !!file.source && !!file.hash);
managedScripts = await Promise.all(managedScripts.map(async (script) => {
let req = new Request(script.source);
let code = await req.loadString();
let hash = this.hashCode(code);
script.updateAvailable = hash != script.hash;
script.updatePayload = {
hash: hash,
code: code
}
return script;
}));
managedScripts = managedScripts.sort((a, b) => a.name < b.name ? -1 : 1);
this.updateableScripts = managedScripts.filter(script => script.updateAvailable);
this.uptodateScripts = managedScripts.filter(script => !script.updateAvailable);
}
}
await new ScriptDude().run();
Script.complete();