-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
76 lines (67 loc) · 2.02 KB
/
utility.js
File metadata and controls
76 lines (67 loc) · 2.02 KB
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
"use strict";
const darkSchemeDetection = window.matchMedia("(prefers-color-scheme: dark)");
export function onSchemeChanged(listener) {
darkSchemeDetection?.addEventListener("change", listener);
}
export function getSystemScheme() {
return darkSchemeDetection?.matches ? "dark" : "light";
}
export async function getCurrentScheme() {
try {
const webAppearanceSetting = await browser.browserSettings?.overrideContentColorScheme?.get({});
const webAppearance = webAppearanceSetting?.value;
return webAppearance === "light" || webAppearance === "dark" ? webAppearance : getSystemScheme();
} catch {
return getSystemScheme();
}
}
export function localise(webDocument) {
webDocument.querySelectorAll("[data-text]").forEach((element) => {
element.textContent = i18n(element.dataset.text);
});
webDocument.querySelectorAll("[data-title]").forEach((element) => {
element.title = i18n(element.dataset.title);
});
webDocument.querySelectorAll("[data-placeholder]").forEach((element) => {
element.placeholder = i18n(element.dataset.placeholder);
});
}
export function i18n(handle) {
const localisedMessage = browser.i18n.getMessage(handle);
if (!localisedMessage) {
return `i18n <${handle}>`;
} else if (localisedMessage === "__EMPTY__") {
return "";
} else {
return localisedMessage;
}
}
let _supportsThemeAPI = null;
export function supportsThemeAPI() {
if (_supportsThemeAPI === null) {
_supportsThemeAPI =
typeof browser.theme !== "undefined" &&
typeof browser.theme.update === "function";
}
return _supportsThemeAPI;
}
export async function getAddonId(url) {
const uuid = url.split(/\/|\?/)[2];
const addonList = await browser.management.getAll();
let addonId;
for (const addon of addonList) {
if (addon.type !== "extension" || !addon.hostPermissions) {
continue;
} else if (addonId) {
break;
} else {
for (const host of addon.hostPermissions) {
if (host.startsWith("moz-extension:") && uuid === host.split(/\/|\?/)[2]) {
addonId = addon.id;
break;
}
}
}
}
return addonId;
}