File indexing completed on 2025-01-05 04:35:04

0001 // Modified from https://gist.githubusercontent.com/arantius/3123124/raw/grant-none-shim.js
0002 //
0003 // %1 - unique script id
0004 
0005 function GM_deleteValue(aKey) {
0006     localStorage.removeItem("%1" + aKey);
0007 }
0008 
0009 function GM_getValue(aKey, aDefault) {
0010     var val = localStorage.getItem("%1" + aKey)
0011     if (null === val) return aDefault;
0012     return val;
0013 }
0014 
0015 function GM_listValues() {
0016     var values = [];
0017     for (var i = 0; i < localStorage.length; i++) {
0018         var k = localStorage.key(i);
0019         if (k.indexOf("%1") === 0) {
0020             values.push(k.replace("%1", ""));
0021         }
0022     }
0023     return values;
0024 }
0025 
0026 function GM_setValue(aKey, aVal) {
0027     localStorage.setItem("%1" + aKey, aVal);
0028 }
0029 
0030 // GreaseMonkey 4.0 support
0031 var asyncCall = (func) => {
0032     if (window._falkon_external) {
0033         func();
0034     } else {
0035         document.addEventListener("_falkon_external_created", func);
0036     }
0037 };
0038 
0039 var decode = (val) => {
0040     val = String(val);
0041     if (!val.length) {
0042         return val;
0043     }
0044     var v = val.substr(1);
0045     if (val[0] == "b") {
0046         return Boolean(v == "true" ? true : false);
0047     } else if (val[0] == "i") {
0048         return Number(v);
0049     } else if (val[0] == "s") {
0050         return v;
0051     } else {
0052         return undefined;
0053     }
0054 };
0055 
0056 var encode = (val) => {
0057     if (typeof val == "boolean") {
0058         return "b" + (val ? "true" : "false");
0059     } else if (typeof val == "number") {
0060         return "i" + String(val);
0061     } else if (typeof val == "string") {
0062         return "s" + val;
0063     } else {
0064         return "";
0065     }
0066 };
0067 
0068 GM.deleteValue = function(name) {
0069     return new Promise((resolve, reject) => {
0070         asyncCall(() => {
0071             external.extra.greasemonkey.deleteValue("%1", name, (res) => {
0072                 if (res) {
0073                     resolve();
0074                 } else {
0075                     reject();
0076                 }
0077             });
0078         });
0079     });
0080 };
0081 
0082 GM.getValue = function(name, value) {
0083     return new Promise((resolve) => {
0084         asyncCall(() => {
0085             external.extra.greasemonkey.getValue("%1", name, encode(value), (res) => {
0086                 resolve(decode(res));
0087             });
0088         });
0089     });
0090 };
0091 
0092 GM.setValue = function(name, value) {
0093     return new Promise((resolve, reject) => {
0094         asyncCall(() => {
0095             external.extra.greasemonkey.setValue("%1", name, encode(value), (res) => {
0096                 if (res) {
0097                     resolve();
0098                 } else {
0099                     reject();
0100                 }
0101             });
0102         });
0103     });
0104 };
0105 
0106 GM.listValues = function() {
0107     return new Promise((resolve) => {
0108         asyncCall(() => {
0109             external.extra.greasemonkey.listValues("%1", resolve);
0110         });
0111     });
0112 };