NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Grant None Shim // @namespace http://userscripts.org/users/arantius // ==/UserScript== /* This script is intended to be used with @require, for Greasemonkey scripts using "@grant none". It emulates the GM_ APIs as closely as possible, using modern browser features like DOM storage. So far, it has only been tested in Greasemonkey on Firefox. Scripts should plan to remove usage of GM_ APIs, but this shim offers a short-term workaround to gain the benefits of running in the security restriction free "@grant none" mode before that is completed. Read the comments on each function to learn if its emulation is good enough for your purposes. NOT IMPLEMENTED: * GM_getResourceText * GM_openInTab * GM_registerMenuCommand */ function GM_addStyle(aCss) { 'use strict'; let head = document.getElementsByTagName('head')[0]; if (head) { let style = document.createElement('style'); style.setAttribute('type', 'text/css'); style.textContent = aCss; head.appendChild(style); return style; } return null; } const GM_log = console.log; // This naive implementation will simply fail to do cross-domain requests, // just like any javascript in any page would. function GM_xmlhttpRequest(aOpts) { 'use strict'; let req = new XMLHttpRequest(); __setupRequestEvent(aOpts, req, 'abort'); __setupRequestEvent(aOpts, req, 'error'); __setupRequestEvent(aOpts, req, 'load'); __setupRequestEvent(aOpts, req, 'progress'); __setupRequestEvent(aOpts, req, 'readystatechange'); req.open(aOpts.method, aOpts.url, !aOpts.synchronous, aOpts.user || '', aOpts.password || ''); if (aOpts.overrideMimeType) { req.overrideMimeType(aOpts.overrideMimeType); } if (aOpts.headers) { for (let prop in aOpts.headers) { if (Object.prototype.hasOwnProperty.call(aOpts.headers, prop)) { req.setRequestHeader(prop, aOpts.headers[prop]); } } } let body = aOpts.data ? aOpts.data : null; if (aOpts.binary) { return req.sendAsBinary(body); } else { return req.send(body); } } function __setupRequestEvent(aOpts, aReq, aEventName) { 'use strict'; if (!aOpts['on' + aEventName]) return; aReq.addEventListener(aEventName, function(aEvent) { let responseState = { responseText: aReq.responseText, responseXML: aReq.responseXML, readyState: aReq.readyState, responseHeaders: null, status: null, statusText: null, finalUrl: null }; switch (aEventName) { case "progress": responseState.lengthComputable = aEvent.lengthComputable; responseState.loaded = aEvent.loaded; responseState.total = aEvent.total; break; case "error": break; default: if (4 != aReq.readyState) break; responseState.responseHeaders = aReq.getAllResponseHeaders(); responseState.status = aReq.status; responseState.statusText = aReq.statusText; break; } aOpts['on' + aEventName](responseState); }); } const __GM_STORAGE_PREFIX = [ '', GM_info.script.namespace, GM_info.script.name, ''].join('***'); // All of the GM_*Value methods rely on DOM Storage's localStorage facility. // They work like always, but the values are scoped to a domain, unlike the // original functions. The content page's scripts can access, set, and // remove these values. A function GM_deleteValue(aKey) { 'use strict'; localStorage.removeItem(__GM_STORAGE_PREFIX + aKey); } function GM_getValue(aKey, aDefault) { 'use strict'; let val = localStorage.getItem(__GM_STORAGE_PREFIX + aKey) if (null === val && 'undefined' != typeof aDefault) return aDefault; return val; } function GM_listValues() { 'use strict'; let prefixLen = __GM_STORAGE_PREFIX.length; let values = []; let i = 0; for (let i = 0; i < localStorage.length; i++) { let k = localStorage.key(i); if (k.substr(0, prefixLen) === __GM_STORAGE_PREFIX) { values.push(k.substr(prefixLen)); } } return values; } function GM_setValue(aKey, aVal) { 'use strict'; localStorage.setItem(__GM_STORAGE_PREFIX + aKey, aVal); } function GM_getResourceURL(aName) { 'use strict'; return 'greasemonkey-script:' + GM_info.uuid + '/' + aName; }