Skip to content

Instantly share code, notes, and snippets.

@trlkly
Last active May 7, 2021 21:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trlkly/c1c97d07fdc0210adeed4c6a436e885d to your computer and use it in GitHub Desktop.
Save trlkly/c1c97d07fdc0210adeed4c6a436e885d to your computer and use it in GitHub Desktop.
Updated Greasemonkey "@grant none" compatibility shim.
/*
Forked from https://gist.github.com/arantius/3123124
The MIT License (MIT)
Copyright (c) 2014 Anthony Lieuallen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
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.
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
*/
if ('undefined' == typeof GM_addStyle) var GM_addStyle = function (aCss) {
'use strict';
let head = document.getElementsByTagName('head')[0];
if (!head) head = document.documentElement;
if (head) {
let style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.textContent = aCss;
head.appendChild(style);
return style;
}
return null;
};
if ('undefined' == typeof GM_log) var GM_log = console.log;
// This naive implementation will simply fail to do cross-domain requests,
// just like any javascript in any page would.
if ('undefined' == typeof GM_xmlhttpRequest) var GM_xmlhttpRequest = function(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.
if ('undefined' == typeof GM_deleteValue) var GM_deleteValue = function (aKey) {
'use strict';
localStorage.removeItem(__GM_STORAGE_PREFIX + aKey);
};
if ('undefined' == typeof GM_getValue) var GM_getValue = function (aKey, aDefault) {
'use strict';
let val = localStorage.getItem(__GM_STORAGE_PREFIX + aKey);
if (null === val && 'undefined' != typeof aDefault) return aDefault;
return val;
};
if ('undefined' == typeof GM_listValues) var GM_listValues = function () {
'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;
};
if ('undefined' == typeof GM_setValue) var GM_setValue = function (aKey, aVal) {
'use strict';
localStorage.setItem(__GM_STORAGE_PREFIX + aKey, aVal);
};
if ('undefined' == typeof GM_getResourceURL) var GM_getResourceURL = function (aName) {
'use strict';
return 'greasemonkey-script:' + GM_info.uuid + '/' + aName;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment