-
Notifications
You must be signed in to change notification settings - Fork 2
/
compat.js
76 lines (68 loc) · 2.46 KB
/
compat.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
export default class Compat {
static browserType() {
if (typeof navigator === 'undefined') {
return 'unknown';
}
let userAgent = navigator.userAgent.toLowerCase();
// The order is matter. Do not change it!
// You should know what you are doing.
if (userAgent.indexOf("edg") !== -1)
return "edge";
if (userAgent.indexOf("chrome") !== -1)
return "chrome";
if (userAgent.indexOf("safari") !== -1)
return "safari";
if (userAgent.indexOf("gecko") !== -1)
return "firefox";
return "unknown";
}
static isRunningInWebExtension() {
return typeof chrome === "object" && chrome.runtime && chrome.storage;
}
static omniboxPageSize() {
return { "firefox": 6, "edge": 7, "chrome": 8, "unknown": 6 }[Compat.browserType()];
}
// Escape the special characters to display them as text.
static escape(str) {
str = str || "";
if (Compat.browserType() === "firefox") {
// Firefox support <,> in omnibox and doesn't support escaped characters.
// So we can unescape them.
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/'/g, "'");
} else {
// Chromium based browsers not support <,> in omnibox.
// We should escape them to avoid xml parse error.
return Compat.escapeAmpersand(str)
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
}
// Escape ampersand & (with spaces around) to &.
// For example, "a & b" => "a & b"
static escapeAmpersand(str) {
return str.replace(" & ", " & ");
}
static normalizeDate(date) {
let month = '' + (date.getMonth() + 1), day = '' + date.getDate(), year = date.getFullYear();
return [year, month.padStart(2, "0"), day.padStart(2, "0")].join('-');
}
static capitalize(value) {
if (value) {
return value.charAt(0).toUpperCase() + value.slice(1);
}
return "";
}
static eliminateTags(value) {
if (value) {
return value.replace(/<\/?(match|dim|code|em|strong|span)>/g, "");
}
return "";
}
};