Improve syntax Greasemonkey 4 APIs
This commit is contained in:
parent
f9412b3ba4
commit
09eea77390
@ -1,131 +1,130 @@
|
|||||||
/*
|
/*
|
||||||
This helper script bridges compatibility between the Greasemonkey 4 APIs and
|
This helper script bridges compatibility between the Greasemonkey 4 APIs and
|
||||||
existing/legacy APIs. Say for example your user script includes
|
existing/legacy APIs. Say for example your user script includes
|
||||||
|
|
||||||
// @grant GM_getValue
|
// @grant GM_getValue
|
||||||
|
|
||||||
And you'd like to be compatible with both Greasemonkey 3 and Greasemonkey 4
|
And you'd like to be compatible with both Greasemonkey 3 and Greasemonkey 4
|
||||||
(and for that matter all versions of Violentmonkey, Tampermonkey, and any other
|
(and for that matter all versions of Violentmonkey, Tampermonkey, and any other
|
||||||
user script engine). Add:
|
user script engine). Add:
|
||||||
|
|
||||||
// @grant GM.getValue
|
// @grant GM.getValue
|
||||||
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
|
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
|
||||||
|
|
||||||
And switch to the new (GM-dot) APIs, which return promises. If your script
|
And switch to the new (GM-dot) APIs, which return promises. If your script
|
||||||
is running in an engine that does not provide the new asynchronous APIs, this
|
is running in an engine that does not provide the new asynchronous APIs, this
|
||||||
helper will add them, based on the old APIs.
|
helper will add them, based on the old APIs.
|
||||||
|
|
||||||
If you use `await` at the top level, you'll need to wrap your script in an
|
If you use `await` at the top level, you'll need to wrap your script in an
|
||||||
`async` function to be compatible with any user script engine besides
|
`async` function to be compatible with any user script engine besides
|
||||||
Greasemonkey 4.
|
Greasemonkey 4.
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
let x = await GM.getValue('x');
|
let x = await GM.getValue('x');
|
||||||
})();
|
})();
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (typeof GM == 'undefined') {
|
if (typeof GM == 'undefined') {
|
||||||
this.GM = {};
|
this.GM = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (typeof GM_addStyle == 'undefined') {
|
if (typeof GM_addStyle == 'undefined') {
|
||||||
this.GM_addStyle = (aCss) => {
|
this.GM_addStyle = (aCss) => {
|
||||||
'use strict';
|
'use strict';
|
||||||
let head = document.getElementsByTagName('head')[0];
|
let head = document.getElementsByTagName('head')[0];
|
||||||
if (head) {
|
if (head) {
|
||||||
let style = document.createElement('style');
|
let style = document.createElement('style');
|
||||||
style.setAttribute('type', 'text/css');
|
style.setAttribute('type', 'text/css');
|
||||||
style.textContent = aCss;
|
style.textContent = aCss;
|
||||||
head.appendChild(style);
|
head.appendChild(style);
|
||||||
return style;
|
return style;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (typeof GM_registerMenuCommand == 'undefined') {
|
if (typeof GM_registerMenuCommand == 'undefined') {
|
||||||
this.GM_registerMenuCommand = (caption, commandFunc, accessKey) => {
|
this.GM_registerMenuCommand = (caption, commandFunc, accessKey) => {
|
||||||
if (!document.body) {
|
if (!document.body) {
|
||||||
if (document.readyState === 'loading'
|
if (document.readyState === 'loading'
|
||||||
&& document.documentElement && document.documentElement.localName === 'html') {
|
&& document.documentElement && document.documentElement.localName === 'html') {
|
||||||
new MutationObserver((mutations, observer) => {
|
new MutationObserver((mutations, observer) => {
|
||||||
if (document.body) {
|
if (document.body) {
|
||||||
observer.disconnect();
|
observer.disconnect();
|
||||||
GM_registerMenuCommand(caption, commandFunc, accessKey);
|
GM_registerMenuCommand(caption, commandFunc, accessKey);
|
||||||
}
|
}
|
||||||
}).observe(document.documentElement, {childList: true});
|
}).observe(document.documentElement, {childList: true});
|
||||||
} else {
|
} else {
|
||||||
console.error('GM_registerMenuCommand got no body.');
|
console.error('GM_registerMenuCommand got no body.');
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let contextMenu = document.body.getAttribute('contextmenu');
|
let contextMenu = document.body.getAttribute('contextmenu');
|
||||||
let menu = (contextMenu ? document.querySelector('menu#' + contextMenu) : null);
|
let menu = (contextMenu ? document.querySelector('menu#' + contextMenu) : null);
|
||||||
if (!menu) {
|
if (!menu) {
|
||||||
menu = document.createElement('menu');
|
menu = document.createElement('menu');
|
||||||
menu.setAttribute('id', 'gm-registered-menu');
|
menu.setAttribute('id', 'gm-registered-menu');
|
||||||
menu.setAttribute('type', 'context');
|
menu.setAttribute('type', 'context');
|
||||||
document.body.appendChild(menu);
|
document.body.appendChild(menu);
|
||||||
document.body.setAttribute('contextmenu', 'gm-registered-menu');
|
document.body.setAttribute('contextmenu', 'gm-registered-menu');
|
||||||
}
|
}
|
||||||
let menuItem = document.createElement('menuitem');
|
let menuItem = document.createElement('menuitem');
|
||||||
menuItem.textContent = caption;
|
menuItem.textContent = caption;
|
||||||
menuItem.addEventListener('click', commandFunc, true);
|
menuItem.addEventListener('click', commandFunc, true);
|
||||||
menu.appendChild(menuItem);
|
menu.appendChild(menuItem);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (typeof GM_getResourceText == 'undefined') {
|
if (typeof GM_getResourceText == 'undefined') {
|
||||||
this.GM_getResourceText = (aRes) => {
|
this.GM_getResourceText = (aRes) => {
|
||||||
'use strict';
|
'use strict';
|
||||||
return GM.getResourceUrl(aRes)
|
return GM.getResourceUrl(aRes)
|
||||||
.then(url => fetch(url))
|
.then(url => fetch(url))
|
||||||
.then(resp => resp.text())
|
.then(resp => resp.text())
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
GM.log('Request failed', error);
|
GM.log('Request failed', error);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Object.entries({
|
Object.entries({
|
||||||
'log': console.log.bind(console), // Pale Moon compatibility. See #13.
|
'log': console.log.bind(console), // Pale Moon compatibility. See #13.
|
||||||
'info': GM_info,
|
'info': GM_info,
|
||||||
}).forEach(([newKey, old]) => {
|
}).forEach(([newKey, old]) => {
|
||||||
if (old && (typeof GM[newKey] == 'undefined')) {
|
if (old && (typeof GM[newKey] == 'undefined')) {
|
||||||
GM[newKey] = old;
|
GM[newKey] = old;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
Object.entries({
|
Object.entries({
|
||||||
'GM_addStyle': 'addStyle',
|
'GM_addStyle': 'addStyle',
|
||||||
'GM_deleteValue': 'deleteValue',
|
'GM_deleteValue': 'deleteValue',
|
||||||
'GM_getResourceURL': 'getResourceUrl',
|
'GM_getResourceURL': 'getResourceUrl',
|
||||||
'GM_getValue': 'getValue',
|
'GM_getValue': 'getValue',
|
||||||
'GM_listValues': 'listValues',
|
'GM_listValues': 'listValues',
|
||||||
'GM_notification': 'notification',
|
'GM_notification': 'notification',
|
||||||
'GM_openInTab': 'openInTab',
|
'GM_openInTab': 'openInTab',
|
||||||
'GM_registerMenuCommand': 'registerMenuCommand',
|
'GM_registerMenuCommand': 'registerMenuCommand',
|
||||||
'GM_setClipboard': 'setClipboard',
|
'GM_setClipboard': 'setClipboard',
|
||||||
'GM_setValue': 'setValue',
|
'GM_setValue': 'setValue',
|
||||||
'GM_xmlhttpRequest': 'xmlHttpRequest',
|
'GM_xmlhttpRequest': 'xmlHttpRequest',
|
||||||
'GM_getResourceText': 'getResourceText',
|
'GM_getResourceText': 'getResourceText',
|
||||||
}).forEach(([oldKey, newKey]) => {
|
}).forEach(([oldKey, newKey]) => {
|
||||||
let old = this[oldKey];
|
let old = this[oldKey];
|
||||||
if (old && (typeof GM[newKey] == 'undefined')) {
|
if (old && (typeof GM[newKey] == 'undefined')) {
|
||||||
GM[newKey] = function(...args) {
|
GM[newKey] = function(...args) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
resolve(old.apply(this, args));
|
resolve(old.apply(this, args));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(e);
|
reject(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user