This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathhelpers.ts
44 lines (39 loc) · 1.44 KB
/
helpers.ts
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
export class Helpers {
/**
* Wrapper around the chrome and general implementation of *.i18n.getMessage.
* @param messageName The message to get
*/
public static GetLocalizedString(messageName: string, substitutions?: any): string {
if (typeof browser === 'undefined' || !browser) {
return chrome.i18n.getMessage(messageName, substitutions);
} else {
return browser.i18n.getMessage(messageName, substitutions);
}
}
/**
* Localize by replacing __MSG_***__ tags
*/
public static LocalizeHtmlPage() {
// Get all objects
const objects = document.getElementsByTagName('html');
// Loop through all the objects
for (let j = 0; j < objects.length; j++) {
const obj = objects[j];
const valStrH = obj.innerHTML.toString();
const valNewH = valStrH.replace(/__MSG_(\w+)__/g, (match, v1) => {
// Wrap in a try catch as Microsoft Edge throws an exception if the
// message does not exist
try {
// Use the string helper to get the string
return v1 ? Helpers.GetLocalizedString(v1) : "";
} catch {
return "";
}
});
if (valNewH !== valStrH) {
// This has to use innerHTML
obj.innerHTML = valNewH;
}
}
}
}