Ce script ne doit pas être installé directement. C'est une librairie destinée à être incluse dans d'autres scripts avec la méta-directive // @require https://update.greatest.deepsurf.us/scripts/10208/150762/GM%20API%20script.js
- // ==UserScript==
- // @name GM API script
- // @namespace https://greatest.deepsurf.us
- // @include *
- // @version 1.1
- // @description Allows use of GM_getValue and GM_setValue, with @grant none
- // @grant none
- // ==/UserScript==
-
-
-
- /*
- 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
- */
-
- 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;
- }