-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarklets_manager.js
417 lines (383 loc) · 12.2 KB
/
bookmarklets_manager.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//This is the current version of the bookmarklet manager.
ubm_version = "1.0.0";
//This is the version of the bookmark manager when the respective format changed. This is used to detect if the client, repo, or site is using legacy formatting.
ubm_clientFormatVersion = "1.0.0";
ubm_repoFormatVersion = "1.0.0";
ubm_siteFormatVersion = "1.0.0";
//This is used to confirm that the client has a version.
function checkCompatability() {
if (typeof my_ubmVersion !== "string") {
var s = "The bookmarklet does not have a setting! Please make sure yours has one!";
alert(s);
throw s;
}
if ([-1, NaN].includes(versionCompare(my_ubmVersion, ubm_clientFormatVersion))) { /*aka is the my_ubmVersion less than ubm_clientFormatVersion*/
var s = "The client has an older version 'v" + my_ubmVersion + "' than is compatable with the bookarklet manager 'v" + ubm_clientFormatVersion + "'! Because of this, the script will not load until you update your bookmarklet. Possibly will add the ability to redirect to the main site and then update it for you.";
alert(s);
throw s;
}
if (typeof repos !== "undefined") {
console.log("Reloading script!");
}
if (typeof my_settings !== "object") {
console.warn("The settings object was not found or is not an object!");
settings = {};
}
}
checkCompatability();
//Settings
/*
repos is all of the bookmarklet repos.`
Each Repo class contains the following items (In same order):
Name (str): This is the name of the repo
Base Url (str): This is the base url to the bookmarklets.
Repo Info (str) [Default: 'bookmarklets_repo.js']: This file contains all the scripts.
Repo Fn (bool): If true, will also run the custom script on the repo. Only do this if you trust the repo.
Check Regardless (bool): This will make this repo get checked to load the script regardless of if it is in the database.
*/
//This stands for load bookmark menu
if (typeof ubm_lbmnu === "undefined") {
ubm_lbmnu = false;
}
function ubm_main(loadr) {
checkCompatability();
if (ubm_lbmnu) {
ubm_lbmnu = false;
loadAlertifyjs(ubm_menu);
return;
} else {
ubm_lbmnu = true;
setTimeout(function(){
ubm_lbmnu = false;
}, 2000);
}
if (loadr || true) {
if(typeof my_repos === "undefined") {
alert("Please make sure you have defined 'my_repos' correctly!");
throw "[ERROR]: Haulting script!";
}
repos = [
//new Repo("ThaumicMekanism's Repo", "https://thaumicmekanism.github.io/Unimarklet/repo/", "../bookmarklets_repo.js", true, false),
];
for (var i = 0; i < my_repos.length; i++) {
r = my_repos[i];
for (var j = repos.length - 1; j >= 0; j--) {
if (r[1] == repos[j].baseurl && r[2] == repos[j].repojs) {
r = null;
}
}
if (r) {
repos.push(new Repo(r[0], r[1], r[2], r[3], r[4]));
}
}
loadRepos();
} else {
finish_load();
}
}
//--------- Helper functions ----------
/*
This is the format for the
*/
Repo = class Repo {
constructor(name, baseurl, repojs, repofn, alwayscheck) {
this.name = name;
this.baseurl = baseurl;
if (repojs == ""){
this.repojs = "bookmarklets_repo.js";
} else {
this.repojs = repojs;
}
this.repofn = repofn;
this.alwayscheck = alwayscheck;
}
}
/*
This is for the repo to add a site.
*/
Site = class Site {
constructor(hostname) {
this.hostname = hostname;
//The urlid uses the base url as a key and the uid as the value.
this.urlID = {};
}
add(baseurl, uid) {
this.urlID[baseurl] = uid;
}
merge(site) {
let keys = site.getKeys();
for (let i in keys) {
i = keys[i];
this.add(i, site.getID(i));
}
}
getID(baseurl) {
return this.urlID[baseurl];
}
getKeys() {
return Object.keys(this.urlID);
}
remove(baseurl) {
return delete this.urlID[baseurl];
}
}
/*
This is for repos to have custom settings
*/
Repo_Settings = class Repo_Settings {
constructor(name, version){
this.name = name;
this.version = version;
}
}
/**
* Compares two software version numbers (e.g. "1.7.1" or "1.2b").
*
* @param {string} v1 The first version to be compared.
* @param {string} v2 The second version to be compared.
* @param {object} [options] Optional flags that affect comparison behavior:
* <ul>
* <li>
* <tt>lexicographical: true</tt> compares each part of the version strings lexicographically instead of
* naturally; this allows suffixes such as "b" or "dev" but will cause "1.10" to be considered smaller than
* "1.2".
* </li>
* <li>
* <tt>zeroExtend: true</tt> changes the result if one version string has less parts than the other. In
* this case the shorter string will be padded with "zero" parts instead of being considered smaller.
* </li>
* </ul>
* @returns {number|NaN}
* <ul>
* <li>0 if the versions are equal</li>
* <li>a negative integer iff v1 < v2</li>
* <li>a positive integer iff v1 > v2</li>
* <li>NaN if either version string is in the wrong format or are null</li>
* </ul>
*/
function versionCompare(v1, v2, options) {
if (v1 == "equal" || v2 == "equal") {
return 0;
}
if (v1 == null || v2 == null) {
return NaN;
}
var lexicographical = options && options.lexicographical,
zeroExtend = options && options.zeroExtend,
v1parts = v1.split('.'),
v2parts = v2.split('.');
function isValidPart(x) {
return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x);
}
if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
return NaN;
}
if (zeroExtend) {
while (v1parts.length < v2parts.length) v1parts.push("0");
while (v2parts.length < v1parts.length) v2parts.push("0");
}
if (!lexicographical) {
v1parts = v1parts.map(Number);
v2parts = v2parts.map(Number);
}
for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) {
return 1;
}
if (v1parts[i] == v2parts[i]) {
continue;
}
else if (v1parts[i] > v2parts[i]) {
return 1;
}
else {
return -1;
}
}
if (v1parts.length != v2parts.length) {
return -1;
}
return 0;
}
/*
This is what runs if the script fails.
*/
function scriptfail(element, fn) {
//console.log("Failed to load element:");
//console.log(element);
fn();
}
/*
Make script tag and load it to page
*/
function loadScript(url, onfail, onload) {
var urlelm = document.getElementById(url);
if (urlelm) {
urlelm.parentNode.removeChild(urlelm)
}
var script = document.createElement('script');
script.setAttribute("onerror", onfail);
script.setAttribute("onload", onload);
script.setAttribute("src", url);
script.setAttribute("id", url);
document.getElementsByTagName("head")[0].appendChild(script);
}
function loadStylesheet(url) {
var urlelm = document.getElementById(url);
if (urlelm) {
urlelm.parentNode.removeChild(urlelm)
}
var css = document.createElement('link');
css.setAttribute("rel", "stylesheet");
css.setAttribute("href", url);
css.setAttribute("id", url);
document.getElementsByTagName("head")[0].appendChild(css);
}
/*
Confirms that a setting is in there. If not, it will help you generate it.
*/
function settingExists(siteSettings, setting, required) {
if (siteSettings && setting in siteSettings){
return true;
} else {
alert("The setting '" + setting + "' does not exist in your settings for this site or you do not have settings for this site. In the future, there will be added functionality to be easily able to add settings.");
if (required) {
throw "The setting '" + setting + "' is required! Make sure it is in the client (your bookmarklet) before rerunning this script.";
}
return false;
}
}
/*
This function will load alertify. It is not called unless you want to use it since it takes some time to load.
*/
function loadAlertifyjs(callback) {
if (typeof ubm_ajsload !== "undefined") {
if (typeof callback === "function") {
return callback();
}
return;
}
loadScript("https://thaumicmekanism.github.io/packages/alertifyjs/alertify.min.js","","");
loadStylesheet("https://thaumicmekanism.github.io/packages/alertifyjs/css/alertify.min.css");
loadStylesheet("https://thaumicmekanism.github.io/packages/alertifyjs/css/themes/default.min.css");
ubm_ajsload = setInterval(function(){
if (typeof alertify !== "undefined") {
clearInterval(ubm_ajsload);
ubm_ajsload = false;
if (typeof callback === "function") {
callback();
}
}
}, 250);
}
//-------- END Helper functions -------
//Load Repos info
function loadRepos() {
ubm_db = {};
ubm_alwayscheck = [];
ubm_i = repos.length;
ubm_loaded = true;
ubm_lfailed = false;
repo_sites = {};
repo_ubmVersion = "equal";
function repo_fn(){}
r = null;
ubm_interval = setInterval(function(){
if (ubm_loaded) {
ubm_loaded = false;
if (ubm_lfailed) {
console.log("Could not load repo: " + ubm_lfailed);
ubm_lfailed = false;
} else if ([-1, NaN].includes(versionCompare(repo_ubmVersion, ubm_repoFormatVersion))) {
console.warn("The repo '" + r.name + `' is running an outdated format which is not compatable with this version of the bookmarklet manager. If you are the repo manager, please update your repos format and version. If you are just a user, please contac the repo manager to update their repo.
\n\nThe repo '` + r.name + "' will be ignored until it is updated. (Unless always check is active)");
} else {
for (var ubm_j = 0; ubm_j < repo_sites.length; ubm_j++) {
var site = repo_sites[ubm_j];
site.add(r.baseurl, "");
if (site.hostname in ubm_db) {
ubm_db[site.hostname].merge(site);
} else {
ubm_db[site.hostname] = site;
}
}
if (r && r.repofn) {
repo_fn();
}
}
ubm_i--;
if (ubm_i < 0) {
clearInterval(ubm_interval);
finish_load();
return;
}
r = repos[ubm_i];
repo_sites = {};
loadScript(r.baseurl + r.repojs, `scriptfail(this, function(){ubm_loaded = true; ubm_lfailed = "` + r.name + `";})` ,``);
if (r.alwayscheck) {
ubm_alwayscheck.push(r);
}
}
}, 5);
}
//TODO Make capable of running multiple items in db.
function finish_load(){
window.ubm_loadedids = {};
window.ubm_incompScripts = new Set([]);
//Get current url
var thisurl = new URL(window.location.href);
var hostname = thisurl.hostname;
var s = ubm_db[hostname];
if (s) {
var keys = s.getKeys();
for (let baseurl of keys) {
loadScript(baseurl + hostname + ".js", `scriptfail(this, function(){console.log('Could not load script from site: ` + baseurl + `!')})`, `exeScript(\`` + baseurl + `\`, function(){loadAlwaysCheck(0, "` + baseurl + `");})`);
}
} else {
loadAlwaysCheck(0, hostname);
}
}
function loadAlwaysCheck(id, baseurl) {
if (id >= ubm_alwayscheck.length) {
return;
}
var r = ubm_alwayscheck[id];
if(r.baseurl !== baseurl) {
loadScript(r.baseurl + window.location.hostname + ".js", `scriptfail(this, function(){console.log('Could not load script from site: ` + r.baseurl + `!'); loadAlwaysCheck(` + (id + 1) + `);})`, `exeScript(\`` + baseurl + `\`, function(){loadAlwaysCheck(` + (id + 1) + `);});`);
}
}
//TODO Add better tracking abilities to see which scripts are incompatable which which..
function exeScript(baseurl, callback) {
if (![-1, NaN].includes(versionCompare(site_ubmVersion, ubm_siteFormatVersion))) {
var uid = siteID();
ubm_db[window.location.hostname].add(baseurl, uid);
if (!ubm_loadedids[uid]){
if (!ubm_incompScripts.has(uid)) {
setTimeout(function(){
main();
}, 0);
is = incompatableScripts()
if (typeof is === "object") {
is.forEach(function(v1, v2, set){
ubm_incompScripts.add(v1);
});
}
window.ubm_loadedids[uid] = true;
} else {
alert("Could not run '" + uid + "' because it is incompatable with another script which was run before this one.");
}
} else {
console.log("Script with same siteID has already been loaded!");
}
} else {
var uid = siteID();
console.warn("Sorry but the script for the site with unique identifier '" + uid + "' is not up to date with the current format. Because of this, the script could not be loaded. Please contact the owner to update the script to the current compatable version.");
}
if (typeof callback === "function") {
callback();
}
}
function ubm_menu() {
alertify.success("Menu activated!");
}
ubm_main(true);