forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
308 lines (294 loc) · 9.71 KB
/
Copy pathdev.js
File metadata and controls
308 lines (294 loc) · 9.71 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
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
import {parse} from "./utils";
async function getRemoteFileContents(url) {
let r = {};
await fetch(url).then(response => {
if (!response.ok) throw Error(response.statusText);
return response.text();
}).then(text => {
r.contents = text;
}).catch(error => {
console.log(error);
r.error = "Remote url bad response!";
});
return r;
}
// example file contents
const exampleCSSContent = `/* ==UserStyle==
@name Example CSS UserStyle
@description This is an example of a UserStyle. It applies css to webpages rather than js.
@match https://userstyles.org/*
==/UserStyle== */
#id,
.class,
.pseudo-element::after {
background-color: tomato;
}
`;
const exampleJSContent = `// ==UserScript==
// @name Example JS Userscript
// @description This a standard userscript with javascript code
// @match https://github.com/quoid/userscripts
// @exclude-match *://*.*
// @version 1.0
// @updateURL https://www.k21p.com/example.user.js
// @noframes
// ==/UserScript==
console.log("I am an example userscript");
`;
// dummy file directory
let files = [
{
content: exampleCSSContent,
filename: "Example CSS UserStyle.css",
lastModified: 1606009623000
},
{
content: exampleJSContent,
filename: "Example JS Userscript.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript2.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript3.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript4.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript5.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript6.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript7.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript8.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript9.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript10.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript11.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript12.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript13.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript14.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript15.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript16.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript17.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript18.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript19.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript20.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript21.js",
lastModified: 1605862023000
},
{
content: exampleJSContent,
filename: "Example JS Userscript22.js",
lastModified: 1605862023000
},
];
function saveFile(content, lastMod, newFilename, oldName) {
const ind = files.findIndex(f => f.filename === oldName);
const s = {
content: content,
filename: newFilename,
lastModified: lastMod
};
if (ind != -1) {
// overwrite at index
files[ind] = s;
} else {
// add to beginning of array
files.unshift(s);
}
}
const _browser = {
runtime: {
async sendNativeMessage(message, responseCallback) {
console.log(`Got message: ${message.name}`);
let response = {};
const name = message.name;
if (name === "PAGE_INIT_DATA") {
response = {
active: "true",
autoCloseBrackets: "true",
autoHint: "true",
blacklist: [],
descriptions: "true",
languageCode: "en",
lint: "true",
log: "false",
saveLocation: "/Users/someone/Library/Directory",
showCount: "true",
showInvisibles: "true",
sortOrder: "lastModifiedDesc",
tabSize: "4",
version: "4.0.0"
};
} else if (name === "PAGE_ALL_FILES") {
response = [];
files.forEach(file => {
const content = file.content;
const parsed = parse(content);
const metadata = parsed.metadata;
const canUpdate = (metadata.version && metadata.updateURL) ? true : false;
const scriptData = {
canUpdate: canUpdate,
content: content,
description: metadata.description[0],
disabled: false,
filename: file.filename,
lastModified: file.lastModified,
metadata: metadata,
name: metadata.name[0],
type: file.filename.substring(file.filename.lastIndexOf(".") + 1)
};
response.push(scriptData);
});
} else if (name === "TOGGLE_ITEM") {
response = {success: true};
//response = {error: true};
} else if (name === "PAGE_UPDATE_SETTINGS") {
response = {success: true};
} else if (name === "PAGE_UPDATE_BLACKLIST") {
response = {success: true};
} else if (name === "PAGE_NEW_REMOTE") {
let r = await getRemoteFileContents(message.url);
response = r;
} else if (name === "PAGE_SAVE") {
console.log(message);
const newContent = message.content;
const oldFilename = message.item.filename;
const parsed = parse(newContent);
const lastModified = Date.now();
let canUpdate = false;
// script failed to parse
if (!parsed) {
return {error: "save failed, file has invalid metadata"};
}
const name = parsed.metadata.name[0];
const newFilename = `${name}.${message.item.type}`;
// filename length too long
if (newFilename.length > 255) {
return {error: "save failed, filename too long!"};
}
// check if file can be remotely updated
if (parsed.metadata.version && parsed.metadata.updateURL) {
canUpdate = true;
}
let success = {
canUpdate: canUpdate,
content: newContent,
filename: newFilename,
lastModified: lastModified,
name: name,
};
// add description if in file metadata
if (parsed.metadata.description) {
success.description = parsed.metadata.description[0];
}
// overwriting
if (newFilename.toLowerCase() === oldFilename.toLowerCase()) {
saveFile(newContent, lastModified, newFilename, oldFilename);
return success;
}
// not overwriting, check if filename for that type is taken
if (files.find(a => a.filename.toLowerCase() === newFilename.toLowerCase())) {
return {error: "save failed, name already taken!"};
}
// not overwriting but all validation passed
saveFile(newContent, lastModified, newFilename, oldFilename);
return success;
} else if (name === "PAGE_UPDATE") {
response.content = message.content + "\n//I was updated";
}
if (!responseCallback) {
return new Promise(resolve => setTimeout(() => resolve(response), 500));
}
setTimeout(() => {
responseCallback(response);
}, 500);
}
},
tabs: {
query(message, responseCallback) {
const response = [
{id: 1, url: "https://www.filmgarb.com/"},
{id: 2, url: "https://www.k21p.com/"}
];
if (!responseCallback) {
return new Promise(resolve => setTimeout(() => resolve(response), 500));
}
setTimeout(() => {
responseCallback(response);
}, 500);
}
}
};
window.browser = _browser;