gm4-polyfill
Version:
A compatibility tool to ease making User Scripts compatible with both Greasemonkey 4 and other/older user script engines.
152 lines (127 loc) • 4.29 kB
JavaScript
;
var _this = void 0;
/*
This helper script bridges compatibility between the Greasemonkey 4 APIs and
existing/legacy APIs. Say for example your user script includes
// @grant GM_getValue
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
user script engine). Add:
// @grant GM.getValue
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
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
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
`async` function to be compatible with any user script engine besides
Greasemonkey 4.
(async () => {
let x = await GM.getValue('x');
})();
*/
if (typeof GM == 'undefined') {
(void 0).GM = {};
}
if (typeof GM_addStyle == 'undefined') {
(void 0).GM_addStyle = function (aCss) {
'use strict';
var head = document.getElementsByTagName('head')[0];
if (head) {
var style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.textContent = aCss;
head.appendChild(style);
return style;
}
return null;
};
}
if (typeof GM_registerMenuCommand == 'undefined') {
(void 0).GM_registerMenuCommand = function (caption, commandFunc, accessKey) {
if (!document.body) {
if (document.readyState === 'loading' && document.documentElement && document.documentElement.localName === 'html') {
new MutationObserver(function (mutations, observer) {
if (document.body) {
observer.disconnect();
GM_registerMenuCommand(caption, commandFunc, accessKey);
}
}).observe(document.documentElement, {
childList: true
});
} else {
console.error('GM_registerMenuCommand got no body.');
}
return;
}
var contextMenu = document.body.getAttribute('contextmenu');
var menu = contextMenu ? document.querySelector('menu#' + contextMenu) : null;
if (!menu) {
menu = document.createElement('menu');
menu.setAttribute('id', 'gm-registered-menu');
menu.setAttribute('type', 'context');
document.body.appendChild(menu);
document.body.setAttribute('contextmenu', 'gm-registered-menu');
}
var menuItem = document.createElement('menuitem');
menuItem.textContent = caption;
menuItem.addEventListener('click', commandFunc, true);
menu.appendChild(menuItem);
};
}
if (typeof GM_getResourceText == 'undefined') {
(void 0).GM_getResourceText = function (aRes) {
'use strict';
return GM.getResourceUrl(aRes).then(function (url) {
return fetch(url);
}).then(function (resp) {
return resp.text();
}).catch(function (error) {
GM.log('Request failed', error);
return null;
});
};
}
Object.entries({
'log': console.log.bind(console),
// Pale Moon compatibility. See #13.
'info': GM_info
}).forEach(function (_ref) {
var newKey = _ref[0],
old = _ref[1];
if (old && typeof GM[newKey] == 'undefined') {
GM[newKey] = old;
}
});
Object.entries({
'GM_addStyle': 'addStyle',
'GM_deleteValue': 'deleteValue',
'GM_getResourceURL': 'getResourceUrl',
'GM_getValue': 'getValue',
'GM_listValues': 'listValues',
'GM_notification': 'notification',
'GM_openInTab': 'openInTab',
'GM_registerMenuCommand': 'registerMenuCommand',
'GM_setClipboard': 'setClipboard',
'GM_setValue': 'setValue',
'GM_xmlhttpRequest': 'xmlHttpRequest',
'GM_getResourceText': 'getResourceText'
}).forEach(function (_ref2) {
var oldKey = _ref2[0],
newKey = _ref2[1];
var old = _this[oldKey];
if (old && typeof GM[newKey] == 'undefined') {
GM[newKey] = function () {
var _this2 = this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return new Promise(function (resolve, reject) {
try {
resolve(old.apply(_this2, args));
} catch (e) {
reject(e);
}
});
};
}
});