-
Notifications
You must be signed in to change notification settings - Fork 0
/
rStore.js
153 lines (136 loc) · 4.22 KB
/
rStore.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: store-alt;
const _info = {
name: 'rStore',
version: '1.1',
updated_at: '2023-12-04 17:45:00',
author: 'ycrao',
description: 'rStore - App for Scriptable',
repo_file_url: 'https://github.com/ycrao/scripts-for-scriptable/blob/main/app/rStore.js',
raw_file_url: 'https://raw.githubusercontent.com/ycrao/scripts-for-scriptable/main/app/rStore.js'
}
const rStoreUrl = 'https://raoyc.com/rstore/app.html'
const usingICloud = true
class RStore {
constructor() {
if (usingICloud) {
this.fm = FileManager.iCloud()
} else {
this.fm = FileManager.local()
}
this.request = new Request('')
this.path = this.fm.documentsDirectory()
}
formatFileName = (fileName) => {
const fileNameStrArr = fileName.split('.')
const len = fileNameStrArr.length
if (len > 1) {
const suffixExt = fileNameStrArr[len - 1]
if (suffixExt.toLowerCase() == 'js') {
return `${fileName}`
}
}
return `${fileName}.js`
}
writeToFile = (fileName, content) => {
let formattedFileName = this.formatFileName(fileName)
const filePath = `${this.path}/${formattedFileName}`
this.fm.writeString(filePath, content)
}
fetchContent = async(url, headers = {}, callback = () => {}) => {
this.request.url = url
this.request.method = 'GET'
this.request.headers = {
...headers
}
const data = await this.request.loadString()
callback(this.request.response, data)
return data
}
injectListener = async() => {
const event = await webView.evaluateJavaScript(
`(() => {
const controller = new AbortController()
const listener = (e) => {
completion(e.detail)
controller.abort()
}
window.addEventListener(
'JBridge',
listener,
{ signal: controller.signal }
)
})()`,
true
).catch((err) => {
console.error(err)
})
const { code, data } = event;
if (code === 'installDep' || code === 'installApp') {
await this.installScript(data)
}
await injectListener()
}
installScript = async(data) => {
console.log(data)
let scriptName = data[0]
let url = data[3]
console.log(scriptName, url)
await this.saveFile(scriptName, url)
await this.notify("rStore", `${scriptName} have been installed`, `scriptable:///run/${encodeURIComponent(scriptName)}`)
// Safari.open(`scriptable:///run/${encodeURIComponent(scriptName)}`)
}
notify = async(title, body, url) => {
let notification = new Notification()
notification.title = title
notification.body = body
if (url) {
notification.openURL = url
}
return await notification.schedule()
}
saveFile = async(appName, url) => {
const headerStr = `// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: laptop-code;\n
`
const content = await this.fetchContent(url)
const scriptableFileString = content.includes('icon-color') ? content : `${headerStr}${content}`
this.writeToFile(appName, scriptableFileString)
}
renderAppStore = async() => {
const wv = new WebView()
await wv.loadURL(rStoreUrl)
const injectListener = async () => {
const event = await wv.evaluateJavaScript(
`(() => {
const controller = new AbortController()
const listener = (e) => {
completion(e.detail)
controller.abort()
}
window.addEventListener(
'JBridge',
listener,
{ signal: controller.signal }
)
})()`,
true
).catch((err) => {
console.error(err);
});
const { code, data } = event;
if (code === 'installDep' || code === 'installApp') {
await this.installScript(data);
}
await injectListener()
}
injectListener().catch((e) => {
console.log(e);
});
await wv.present()
}
}
rStore = new RStore()
rStore.renderAppStore()