-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplatform.js
84 lines (74 loc) · 2.07 KB
/
platform.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
var Platform = window.Platform || (function() {
const langregex = /lang/i;
return class Platform {
constructor(spec, config={}) {
this.spec = spec;
this.config = config;
this.geolocation = config.geolocation;
this.language = config.language || navigator.language;
this.acceptWebP = config.acceptWebP || false;
this.acceptJXR = config.acceptJXR || false;
}
_expandVars(name, value) {
if (name.toLowerCase() === "accept") {
if (!this.acceptWebP) {
value = value.replace(",image/webp", "");
}
if (!this.acceptJXR) {
value = value.replace(",image/jxr", "");
}
} else if (name.match(langregex)) {
value = value.replace("LANGLC", this._langLC).
replace("LANGMAJOR", this._langMajor).
replace("LANG", this._lang);
}
return value;
}
get language() {
return this._lang;
}
set language(lang) {
this._lang = lang;
this._langLC = lang.toLowerCase();
this._langMajor = lang.split("-")[0];
}
get label() {
return browser.i18n.getMessage(this.spec.label, this.spec.version);
}
get version() {
return this.spec.version;
}
get icon() {
return this.spec.icon;
}
*headers() {
for (let [name, value] of Object.entries(this.spec.headers || {})) {
yield {
name,
value: this._expandVars(name, value)
};
}
}
*overrides() {
for (let [subobjname, subobj] of Object.entries(this.spec.overrides || {})) {
for (let [name, value] of Object.entries(subobj)) {
yield {
name: `${subobjname}.${name}`,
value: this._expandVars(name, value)
};
}
}
}
toSpec() {
return {
label: this.label,
icon: this.icon,
version: this.version,
language: this.language,
geolocation: this.geolocation,
headers: [...this.headers()],
overrides: [...this.overrides()],
}
}
}
}());