Falkon Develop
Cross-platform Qt-based web browser
values.js
Go to the documentation of this file.
1// Modified from https://gist.githubusercontent.com/arantius/3123124/raw/grant-none-shim.js
2//
3// %1 - unique script id
4
5function GM_deleteValue(aKey) {
6 localStorage.removeItem("%1" + aKey);
7}
8
9function GM_getValue(aKey, aDefault) {
10 var val = localStorage.getItem("%1" + aKey)
11 if (null === val) return aDefault;
12 return val;
13}
14
15function GM_listValues() {
16 var values = [];
17 for (var i = 0; i < localStorage.length; i++) {
18 var k = localStorage.key(i);
19 if (k.indexOf("%1") === 0) {
20 values.push(k.replace("%1", ""));
21 }
22 }
23 return values;
24}
25
26function GM_setValue(aKey, aVal) {
27 localStorage.setItem("%1" + aKey, aVal);
28}
29
30// GreaseMonkey 4.0 support
31var asyncCall = (func) => {
32 if (window._falkon_external) {
33 func();
34 } else {
35 document.addEventListener("_falkon_external_created", func);
36 }
37};
38
39var decode = (val) => {
40 val = String(val);
41 if (!val.length) {
42 return val;
43 }
44 var v = val.substr(1);
45 if (val[0] == "b") {
46 return Boolean(v == "true" ? true : false);
47 } else if (val[0] == "i") {
48 return Number(v);
49 } else if (val[0] == "s") {
50 return v;
51 } else {
52 return undefined;
53 }
54};
55
56var encode = (val) => {
57 if (typeof val == "boolean") {
58 return "b" + (val ? "true" : "false");
59 } else if (typeof val == "number") {
60 return "i" + String(val);
61 } else if (typeof val == "string") {
62 return "s" + val;
63 } else {
64 return "";
65 }
66};
67
68GM.deleteValue = function(name) {
69 return new Promise((resolve, reject) => {
70 asyncCall(() => {
71 external.extra.greasemonkey.deleteValue("%1", name, (res) => {
72 if (res) {
73 resolve();
74 } else {
75 reject();
76 }
77 });
78 });
79 });
80};
81
82GM.getValue = function(name, value) {
83 return new Promise((resolve) => {
84 asyncCall(() => {
85 external.extra.greasemonkey.getValue("%1", name, encode(value), (res) => {
86 resolve(decode(res));
87 });
88 });
89 });
90};
91
92GM.setValue = function(name, value) {
93 return new Promise((resolve, reject) => {
94 asyncCall(() => {
95 external.extra.greasemonkey.setValue("%1", name, encode(value), (res) => {
96 if (res) {
97 resolve();
98 } else {
99 reject();
100 }
101 });
102 });
103 });
104};
105
106GM.listValues = function() {
107 return new Promise((resolve) => {
108 asyncCall(() => {
109 external.extra.greasemonkey.listValues("%1", resolve);
110 });
111 });
112};