-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.js
190 lines (182 loc) · 5.04 KB
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
globalThis.context = globalThis.context || (function Context() {
// Console:
const LOG = false; // false
const INFO = false; // false
const DEBUG = false; // false
const WARN = true;
const ERROR = true;
function log(...arg) {
if (console && LOG) console.log(...arg);
}
function info(...arg) {
if (console && INFO) console.info(...arg);
}
function debug(...arg) {
if (console && DEBUG) console.debug(...arg);
}
function warn(...arg) {
if (console && WARN) console.warn(...arg);
}
function error(...arg) {
if (console && ERROR) console.error(...arg);
}
// Options:
let defaults = {
dispMode: "defaultMode",
popupPos: "defaultPos",
deepSearchBiggerLimit: 175 * 175, // 30625
mlinkOSM: true,
mlinkGoogle: true,
mlinkBing: true,
mlinkMapQuest: false,
mlinkHere: false,
mlinkFlickr: true,
mlinkGeoHack: false,
devDisableDeepSearch: false,
devClickThumbnailBeta: false,
devFetchMode: "devAutoFetch",
initialOnboard: 0
};
function setOptions(o) {
return browser.storage.local.set(o);
}
function setOption(prop, value) {
const o = {};
o[prop] = value;
return setOptions(o);
}
function getOptions() {
function onError(error) {
console.warning(`xIFr: getOptions() error: ${error}`);
return defaults;
}
function setCurrentChoice(result) {
// Merge default with loaded values:
return Object.assign(defaults, result); // updates defaults and returns it
}
return browser.storage.local.get().then(setCurrentChoice, onError);
}
// Misc:
function supportsDeepSearch() {
return !!(typeof browser !== 'undefined' && browser?.menus?.getTargetElement); // Well, might not be enough. But for the time being this check should tell. In practice Firefox 63+ supports, Chrome does not...
}
function prefersDark(dispMode) {
return dispMode === "darkMode" || window?.matchMedia('(prefers-color-scheme: dark)')?.matches;
}
function isFirefox() {
return !!((typeof browser !== 'undefined') && browser?.runtime?.getURL("./")?.startsWith("moz-extension:https://"));
}
function firefoxExtId() {
return '{5e71bed2-2b15-40b8-a15b-ba89563aaf73}';
}
function isChromium() { // Besides Chrome, this also includes Edge & Opera. And likely most/all other Chromium based browsers too(?)
return !!((typeof browser !== 'undefined') && browser?.runtime?.getURL("./")?.startsWith("chrome-extension:https://"));
}
// API:
return {
LOG: LOG,
INFO: INFO,
DEBUG: DEBUG,
ERROR: ERROR,
log: log,
info: info,
debug: debug,
warn: warn,
error: error,
setOptions: setOptions,
setOption: setOption,
getOptions: getOptions,
isFirefox: isFirefox,
firefoxExtId : firefoxExtId,
isChromium: isChromium,
supportsDeepSearch: supportsDeepSearch,
prefersDark: prefersDark
};
})();
globalThis.versionnumber = globalThis.versionnumber || (function Versionnumber() { // major.minor.revision
function current() {
if (browser?.runtime) { // webextension versionnumber
return browser.runtime.getManifest().version;
}
// undefined
}
function compare(v1, v2) {
if (typeof v2 === 'undefined') {
v2 = v1;
v1 = current();
}
const v1parts = v1.split('.');
const v2parts = v2.split('.');
while (v1parts.length < v2parts.length) v1parts.push(0);
while (v2parts.length < v1parts.length) v2parts.push(0);
for (let i = 0; i < v1parts.length; ++i) {
if (parseInt(v1parts[i], 10) > parseInt(v2parts[i], 10)) {
return 1;
} else if (parseInt(v1parts[i], 10) < parseInt(v2parts[i], 10)) {
return -1;
}
}
return 0;
}
function parts(v, n) {
if (typeof n === 'undefined') {
n = v;
v = current();
}
const vparts = v.split('.', n);
vparts.map(function (item) {
return String(parseInt(item.trim(), 10))
});
while (vparts.length < n) vparts.push('0');
return vparts.join('.');
}
function major(v) {
if (typeof v === 'undefined') {
v = current();
}
return parts(v, 1);
}
function minor(v) {
if (typeof v === 'undefined') {
v = current();
}
return parts(v, 2);
}
function revision(v) {
if (typeof v === 'undefined') {
v = current();
}
return parts(v, 3);
}
function validate(v) {
if (typeof v === 'string' || v instanceof String) {
const vparts = v.split('.');
if (vparts.length >= 1 && vparts.length <= 3) {
for (const part of vparts) {
const parsed = parseInt(part, 10);
if (isNaN(parsed)) return false;
if (part !== String(parsed)) return false; // This doesn't allow leading 0s like in yyyy.mm.dd versionnumbers (ok?)
}
}
return true;
}
return false;
}
function validOr(v, alt) {
if (validate(v)) {
return v;
} else {
return alt;
}
}
// API:
return {
current: current,
compare: compare,
major: major,
minor: minor,
revision: revision,
validate: validate,
validOr: validOr
};
})();