-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMWA.js
76 lines (65 loc) · 2.6 KB
/
MWA.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: orange; icon-glyph: magic;
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: download;
const DEBUG = true;
const log = DEBUG ? console.log.bind(console) : function () {};
// configure the library
const libraryInfo = {
name: 'MostWantedAudiWidget',
version: 'main',
// forceDownload: DEBUG,
user: 'MaLub',
project: 'mwa-ios-widget',
token: '231c86de00d0aabcbe2c7095cb7b156f2966d8f4'
};
// download and import library
let libraryFile = await downloadLibrary(libraryInfo);
log("LibraryFile: "+libraryFile);
let library = importModule(libraryFile);
// create the widget
const params = {
widgetParameter: args.widgetParameter,
debug: DEBUG
};
const widget = await library.createWidget(params);
// preview the widget
if (!config.runsInWidget) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
/**
* - creates directory for library if not existing
* - downloads library file if forced or not yet existing
* - returns relative path to library module
* @param {{name: string, version: string, gitlabProject: string, forceDownload: bool}} library
*/
async function downloadLibrary(library) {
let fm = FileManager.local();
// https://raw.githubusercontent.com/MaLub/mwa-ios-widget/main/MostWantedAudiWidget.js?token=AFTXSYKBBNVVLVAYEQG7BUC734MM4
let scriptPath = module.filename;
let libraryDir = scriptPath.replace(fm.fileName(scriptPath, true), fm.fileName(scriptPath, false));
if (fm.fileExists(libraryDir) && !fm.isDirectory(libraryDir)) {
fm.remove(libraryDir);
}
if (!fm.fileExists(libraryDir)) {
fm.createDirectory(libraryDir);
}
let libraryFilename = library.name + '_' + library.version + '.js';
let path = fm.joinPath(libraryDir, libraryFilename);
let forceDownload = (library.forceDownload) ? library.forceDownload : false;
if (fm.fileExists(path) && !forceDownload) {
log("Not downloading library file");
} else {
let r = Math.random().toString(36).substring(7);
let libraryUrl = "https://@raw.githubusercontent.com/" + library.user + "/" + library.project + "/" + library.version + "/" + library.name + ".js?random=" + r;
log("Downloading library file '" + libraryUrl + "' to '" + path + "'");
const req = new Request(libraryUrl);
let libraryFile = await req.load();
fm.write(path, libraryFile);
}
return fm.fileName(scriptPath, false) + '/' + libraryFilename;
}