-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMCommonAPI.js
559 lines (504 loc) · 21.9 KB
/
GMCommonAPI.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* GM Common API is a library for userscripts. A "Synchronious API" compatible
* with both Greasemonkey 4 WebExtension and all the other commonly used
* userscript managers. Using GM Common API can be a fast and easy way to add
* Greasemonkey 4 WebExtension compatibility to (some) existing userscripts.
*
* https://greasyfork.org/scripts/34527-gmcommonapi-js
* https://github.com/StigNygaard/GMCommonAPI.js
*/
var GMC = GMC || (function api() {
// CHANGELOG - The most important updates/versions:
let changelog = [
{version: '2017.12.16', description: 'GM4 actually supports GM.openInTab.'},
{version: '2017.12.07', description: 'Fixing an error seen in Chrome/Tampermonkey.'},
{version: '2017.12.06', description: 'Prefetch @resource objects in GM4+ for potential performance improvements (Requires @grant GM.getResourceUrl).'},
{version: '2017.12.03', description: 'Some refactoring. Fix: getResourceUrl would sometimes only return one resource in GM4.'},
{version: '2017.11.18', description: 'Adding GMC.xmlHttpRequest().'},
{version: '2017.11.11', description: 'Advanced options for menus (Via GMC.registerMenuCommand() using new options parameter).'},
{version: '2017.10.29', description: 'Adding GMC.listValues(), GMC.listLocalStorageValues() and GMC.listSessionStorageValues().'},
{version: '2017.10.28', description: '@grant not needed for use of GM.info/GM_info.'},
{version: '2017.10.25', description: 'Initial release.'}
];
/*
* GMC.info
*
* Maps to GM_info or GM.info object.
* Grants: none needed.
*/
let info = (GM_info ? GM_info : (typeof GM === 'object' && GM !== null && typeof GM.info === 'object' ? GM.info : null) );
/*
* GMC.registerMenuCommand(caption, commandFunc, accessKey)
* GMC.registerMenuCommand(caption, commandFunc, options)
*
* Currently the GM4 API is missing GM.registerMenuCommand, but luckily Firefox supports HTML5
* context menus, which are created by this method when supported (Currently only supported by
* the Firefox family). AccessKey is currently ignored for context menus.
* Instead of the accessKey string parameter, there's an option to pass an options object
* adding multiple configuration options for fine-tuning menus. Example:
* GMC.registerMenuCommand( 'Hide the top', toggleTop , {accessKey: 'T', type: 'checkbox', checked: isHiddenTop()} );
* Currently supported properties in the options object are:
* accessKey, topLevel, id, name, type, checked, disabled & icon.
* Todo: Document the properties in the options object.
*
* Grants:
* GM_registerMenuCommand
* GM.registerMenuCommand (Optional for possible future support. Currently not available with any userscript manager)
*/
function registerMenuCommand(caption, commandFunc, options) {
if (typeof options === 'string') {
options = {'accessKey': options};
} else if (typeof options === 'undefined') {
options = {};
}
// "Legacy" menu:
if (!options['disabled']) {
let prefix = '';
if (options['type'] === 'radio') {
prefix = options['checked'] ? '\u26AB ' : '\u26AA '; // ⚫/⚪
} else if (options['type'] === 'checkbox') {
prefix = options['checked'] ? '\u2B1B ' : '\u2B1C '; // ⬛/⬜
}
if (typeof GM_registerMenuCommand === 'function') {
// Supported by most userscript managers, but NOT with Greasemonkey 4 WebExtension
GM_registerMenuCommand(prefix + caption, commandFunc, options['accessKey']);
} else if (typeof GM === 'object' && GM !== null && typeof GM.registerMenuCommand === 'function') {
// NOT implemented in Greasemonkey 4.0 WebExtension, but if later?...
GM.registerMenuCommand(prefix + caption, commandFunc, options['accessKey']);
}
}
// HTML5 context menu (currently only supported by the Firefox family):
if (contextMenuSupported()) {
if (!document.body) {
alert('GMC Error: Body element for context menu not found. If running userscript at "document-start" you might need to delay initialization of menus.');
return;
}
let topMenu = null;
if (document.body.getAttribute('contextmenu')) {
// If existing context menu on body, don't replace but use/extend it...
topMenu = document.querySelector('menu#' + document.body.getAttribute('contextmenu'));
}
if (!topMenu) {
// if not already defined, create the "top menu container"
topMenu = document.createElement('menu');
topMenu.setAttribute('type', 'context');
topMenu.setAttribute('id', 'gm-registered-menu');
document.body.appendChild(topMenu);
document.body.setAttribute('contextmenu', topMenu.getAttribute('id'));
}
// Create menu item
let menuItem = document.createElement('menuitem');
menuItem.setAttribute('type', options['type'] ? options['type'] : 'command'); // command, checkbox or radio
menuItem.setAttribute('label', caption);
if (options['id']) menuItem.setAttribute('id', options['id']);
if (options['name']) menuItem.setAttribute('name', options['name']);
if (options['checked']) menuItem.setAttribute('checked', 'checked');
if (options['disabled']) menuItem.setAttribute('disabled', 'disabled');
if (options['icon']) menuItem.setAttribute('icon', options['icon']); // does icon work on radio/checkbox or only command?
// Append menuitem
if (options['topLevel']) {
topMenu.appendChild(menuItem);
} else { // script menu
let scriptMenu = topMenu.querySelector('menu[label="' + getScriptName() + '"]');
if (!scriptMenu) {
// if not already defined, create a "sub-menu" for current userscript
scriptMenu = document.createElement('menu');
scriptMenu.setAttribute('label', getScriptName());
// icon = @icon from metadata??? NO, icon not working for menu elements :-(
topMenu.appendChild(scriptMenu);
}
scriptMenu.appendChild(menuItem);
}
menuItem.addEventListener('click', commandFunc, false);
}
}
/*
* GMC.getResourceURL(resourceName)
* GMC.getResourceUrl(resourceName)
*
* This will use GM_getResourceURL if available, and otherwise try to find an url directly via
* the GMC.info object properties.
* Grants:
* GM_getResourceURL
* GM.getResourceUrl (Optional, but add this for potential performance improvement in GM4+)
*/
function getResourceUrl(resourceName) {
if (typeof GM_getResourceURL === 'function') {
return GM_getResourceURL(resourceName);
} else if (info) {
if (typeof info.script === 'object' && typeof info.script.resources === 'object' && typeof info.script.resources[resourceName] === 'object' && info.script.resources[resourceName].url) {
return info.script.resources[resourceName].url;
} else if (info.scriptMetaStr) {
// Parse metadata block to find the original "remote url" instead:
let ptrn = new RegExp('^\\s*\\/\\/\\s*@resource\\s+([^\\s]+)\\s+([^\\s]+)\\s*$','igm');
let result;
while((result = ptrn.exec(info.scriptMetaStr)) !== null) {
if (result[1] === resourceName) return result[2];
// and do a GM4 "prefetch"?
}
}
alert('GMC Error: Cannot find url of resource=' + resourceName + ' in GMC.info object');
} else {
alert('GMC Error: Cannot lookup resourceURL (Missing @grant for GM_getResourceURL?)');
}
}
/*
* GMC.setValue(name, value)
*
* When supported, this points to GM_setValue which stores values in a userscript specific
* database. Otherwise the HTML5 Web Storage is used, which is a domain(+protocol) specific
* database in the browser.
* IMPORTANT: If your userscript is a "single-domain type", the difference in storage type is
* probably not a problem, but for "multiple-domain userscripts" GMC.setValue() might not be a
* satisfying solution.
* To prevent mistakenly overwriting or reading other clientscript's values when using Web
* Storage, a prefix based on userscript namespace and scriptname is added to name used in Web
* Storage.
* Grants:
* GM_setValue
*/
function setValue(name, value) {
if (typeof GM_setValue === 'function') {
GM_setValue(name, value);
} else {
setLocalStorageValue(name, value);
}
}
/*
* GMC.getValue(name, defvalue)
*
* Get the values stored using GMC.setValue(). When supported via GM_getValue and otherwise from
* HTML5 Web Storage.
* Grants:
* GM_getValue
*/
function getValue(name, defvalue) { // getLocalStorageValue: function(name, defvalue) {
if (typeof GM_getValue === 'function') {
return GM_getValue(name, defvalue);
} else {
return getLocalStorageValue(name, defvalue);
}
}
/*
* GMC.deleteValue(name)
*
* Deletes a value stored using GMC.setValue(). When supported via GM_deleteValue and otherwise
* from HTML5 Web Storage.
* Grants:
* GM_deleteValue
*/
function deleteValue(name) {
if (typeof GM_deleteValue === 'function') {
GM_deleteValue(name);
} else {
deleteLocalStorageValue(name);
}
}
/*
* GMC.listValues()
*
* Returns the values (key-names) stored using GMC.setValue(). When supported via GM_listValues
* and otherwise from HTML5 Web Storage.
* Grants:
* GM_listValues
*/
function listValues() {
if (typeof GM_listValues === 'function') {
return GM_listValues();
} else {
return listLocalStorageValues();
}
}
/*
* GMC.setLocalStorageValue(name, value)
*
* Save value in HTML5 Web Storage (window.localStorage), which is a domain(+protocol) specific
* database in the browser. To prevent mistakenly overwriting or reading other clientscript's
* values when using Web Storage, a prefix based on userscript namespace and scriptname is added
* to the name used in Web Storage.
* Grants: none needed.
*/
function setLocalStorageValue(name, value) {
localStorage.setItem(getScriptIdentifier() + '_' + name, value);
}
/*
* GMC.getLocalStorageValue(name, defvalue)
*
* Get a value that was stored using GMC.setLocalStorageValue().
* Grants: none needed.
*/
function getLocalStorageValue(name, defvalue) {
if ((getScriptIdentifier()+'_'+name) in localStorage) {
return localStorage.getItem(getScriptIdentifier()+'_'+name);
} else {
return defvalue;
}
}
/*
* GMC.deleteLocalStorageValue(name)
*
* Deletes a value that was stored using GMC.setLocalStorageValue().
* Grants: none needed.
*/
function deleteLocalStorageValue(name) {
localStorage.removeItem(getScriptIdentifier() + '_' + name);
}
/*
* GMC.listLocalStorageValues()
*
* Returns the values (key-names) stored using GMC.setLocalStorageValue().
* Grants: none needed.
*/
function listLocalStorageValues() {
let values = [];
let prefix = getScriptIdentifier();
let prelen = getScriptIdentifier().length;
for (let i = 0; i < localStorage.length; i++) {
if (localStorage.key(i).substr(0, prelen) === prefix) {
values.push(localStorage.key(i).substr(prelen+1));
}
}
return values;
}
/*
* GMC.setSessionStorageValue(name, value)
*
* Similar to setLocalStorageValue(), but setSessionStorageValue() only stores for the current
* session.
* Grants: none needed.
*/
function setSessionStorageValue(name, value) {
sessionStorage.setItem(getScriptIdentifier() + '_' + name, value);
}
/*
* GMC.getSessionStorageValue(name, defvalue)
*
* Get a value that was stored using GMC.setSessionStorageValue().
* Grants: none needed.
*/
function getSessionStorageValue(name, defvalue) {
if ((getScriptIdentifier()+'_'+name) in localStorage) {
return sessionStorage.getItem(getScriptIdentifier()+'_'+name);
} else {
return defvalue;
}
}
/*
* GMC.deleteSessionStorageValue(name)
*
* Deletes a value that was stored using GMC.setSessionStorageValue().
* Grants: none needed.
*/
function deleteSessionStorageValue(name) {
sessionStorage.removeItem(getScriptIdentifier() + '_' + name);
}
/*
* GMC.listSessionStorageValues()
*
* Returns the values (key-names) stored using GMC.setSessionStorageValue().
* Grants: none needed.
*/
function listSessionStorageValues() {
let values = [];
let prefix = getScriptIdentifier();
let prelen = getScriptIdentifier().length;
for (let i = 0; i < sessionStorage.length; i++) {
if (sessionStorage.key(i).substr(0, prelen) === prefix) {
values.push(sessionStorage.key(i).substr(prelen+1));
}
}
return values;
}
/*
* GMC.log(message)
*
* Writes a log-line to the console. It will use GM_log if supported/granted, otherwise
* it will do it using window.console.log().
* Grants:
* GM_log
*/
function log(message) {
if(typeof GM_log === 'function') {
GM_log(message);
} else if (window.console) {
if (info) {
window.console.log(getScriptName() + ' : ' + message);
} else {
window.console.log('GMC : ' + message);
}
}
}
/*
* GMC.setClipboard(text)
*
* Sets content of the clipboard by using either GM.setClipboard or GM_setClipboard.
* Grants:
* GM.setClipboard
* GM_setClipboard
*/
let setClipboard = (typeof GM_setClipboard === 'function' ? GM_setClipboard : (typeof GM === 'object' && GM !== null && typeof GM.setClipboard === 'function' ? GM.setClipboard : null) );
/*
* GMC.addStyle(style)
*
* Adds style in a an element in html header.
* Grants:
* GM_addStyle (Optional. Will be used when available, but this method should normally work fine without)
* GM.addStyle (Optional for possible future support. Currently not available with any userscript manager)
*/
function addStyle(style) {
if (typeof GM_addStyle === 'function') {
return GM_addStyle(style);
} else if (typeof GM === 'object' && GM !== null && typeof GM.addStyle === 'function') {
return GM.addStyle(style); // For possible future support. Will Probably return undefined.
} else {
let head = document.getElementsByTagName('head')[0];
if (head) {
let styleElem = document.createElement('style');
styleElem.setAttribute('type', 'text/css');
styleElem.textContent = style;
head.appendChild(styleElem);
return styleElem;
}
alert('GMC Error: Unable to add style element to head element. If running userscript at "document-start" you might need to delay initialization of styles.');
}
}
/*
* GMC.openInTab(url, open_in_background)
*
* Opens url in a new tab.
* Grants:
* GM.openInTab
* GM_openInTab
*/
function openInTab(url, open_in_background) {
if (typeof GM_openInTab === 'function') {
return GM_openInTab(url, open_in_background);
} else if (typeof GM === 'object' && GM !== null && typeof GM.openInTab === 'function') {
return GM.openInTab(url, open_in_background);
}
return window.open(url);
}
/*
* GMC.xmlHttpRequest(details)
* GMC.xmlhttpRequest(details)
*
* Forwards to either GM_xmlhttpRequest or GM.xmlHttpRequest.
* When adding @grant declarations, make sure to take notice of the case differences between
* the APIs. GMC supports both case-variants (GMC.xmlHttpRequest and GMC.xmlhttpRequest).
* Also remember to add needed @connect declarations for Tampermonkey:
* https://tampermonkey.net/documentation.php#_connect
*
* Grants:
* GM.xmlHttpRequest
* GM_xmlhttpRequest
*/
function xmlHttpRequest(details) {
if (typeof GM_xmlhttpRequest === 'function') {
return GM_xmlhttpRequest(details);
} else if (typeof GM === 'object' && GM !== null && typeof GM.xmlHttpRequest === 'function') {
return GM.xmlHttpRequest(details); // probably undefined return value!
}
alert('GMC Error: xmlHttpRequest not found! Missing or misspelled @grant declaration? (Be aware of case differences in the APIs!)');
}
/*
* GMC.getScriptName()
*
* Simply returns script name as defined in meta data. If no name was defined, returns "Userscript".
* Grants: none needed.
*/
function getScriptName() {
if (typeof info.script.name === 'string' && info.script.name.trim().length > 0) {
return info.script.name.trim();
} else {
return 'Userscript';
}
}
/*
* GMC.getScriptNamespace()
*
* Simply returns the script's namespace as defined in meta data.
* Grants: none needed.
*/
function getScriptNamespace() {
if (typeof info.script.namespace === 'string') {
return info.script.namespace.trim();
} else {
return '';
}
}
// Internal, temporary and experimental stuff:
function isGreasemonkey4up() {
if (typeof info.scriptHandler === 'string' && typeof info.version === 'string') {
return info.scriptHandler === 'Greasemonkey' && parseInt(info.version,10)>=4;
}
return false;
}
function contextMenuSupported() { // Argh, it's a bit ugly, not 100% accurate (and probably not really necessary)
let oMenu = document.createElement('menu');
return (oMenu.type !== 'undefined'); // type="list|context|toolbar" if supported ?
}
function getScriptIdentifier() { // A "safe" identifier without any special characters (but doesn't work well for non-latin :-/ )
if (info && typeof info.script === 'object') {
return 'gmc' + getScriptNamespace().replace(/[^\w]+/g,'x') + getScriptName().replace(/[^\w]+/g,'x');
} else {
alert('GMC Error: Script Namespace or Name not found.');
}
}
function inspect(obj) { // for some debugging
let output='';
Object.keys(obj).forEach(function(key, idx) {
output+=key+': ' + typeof obj[key] + ((typeof obj[key] === 'string' || typeof obj[key] === 'boolean' || typeof obj[key] === 'number') ? ' = ' + obj[key] : '') + '\n';
});
alert(output);
}
if (typeof GM === 'object' && GM !== null && typeof GM.getResourceUrl === 'function' && typeof info === 'object' && typeof info.script === 'object' && typeof info.script.resources === 'object' ) {
// Prefetch @resource objects and update info.script.resources[resourcename].url with local address, using GM.getResourceUrl().
prefetchResource = function(name) {
if (typeof info === 'object' && typeof info.script === 'object' && typeof info.script.resources === 'object') {
let obj = info.script.resources[name];
if (typeof GM === 'object' && GM !== null && typeof GM.getResourceUrl === 'function' && obj) {
GM.getResourceUrl(obj.name).then(function (url) {
obj.url = url;
}, function (err) {
log('Error fetching resource ' + obj.name + ': ' + err);
});
} else {
log('Error, info.script.resources[' + name + '] not found in resources object.');
}
}
};
Object.keys(info.script.resources).forEach(function(key, idx) {
prefetchResource(key);
});
}
return {
info: info,
registerMenuCommand: registerMenuCommand,
getResourceUrl: getResourceUrl,
getResourceURL: getResourceUrl,
setValue: setValue,
getValue: getValue,
deleteValue: deleteValue,
listValues: listValues,
setLocalStorageValue: setLocalStorageValue,
getLocalStorageValue: getLocalStorageValue,
deleteLocalStorageValue: deleteLocalStorageValue,
listLocalStorageValues: listLocalStorageValues,
setSessionStorageValue: setSessionStorageValue,
getSessionStorageValue: getSessionStorageValue,
deleteSessionStorageValue: deleteSessionStorageValue,
listSessionStorageValues: listSessionStorageValues,
log: log,
setClipboard: setClipboard,
addStyle: addStyle,
openInTab: openInTab,
xmlHttpRequest: xmlHttpRequest,
xmlhttpRequest: xmlHttpRequest,
getScriptName: getScriptName,
getScriptNamespace: getScriptNamespace,
// Temporary and experimental stuff:
isGreasemonkey4up: isGreasemonkey4up,
contextMenuSupported: contextMenuSupported,
inspect: inspect
};
})();