forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.ts
More file actions
152 lines (134 loc) · 4.35 KB
/
Copy pathurl.ts
File metadata and controls
152 lines (134 loc) · 4.35 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
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
import {UserSettings} from '../definitions';
import {isIPV6, compareIPV6} from './ipv6';
export function getURLHost(url: string) {
return url.match(/^(.*?\/{2,3})?(.+?)(\/|$)/)[2];
}
export function compareURLPatterns(a: string, b: string) {
return a.localeCompare(b);
}
/**
* Determines whether URL has a match in URL template list.
* @param url Site URL.
* @paramlist List to search into.
*/
export function isURLInList(url: string, list: string[]) {
for (let i = 0; i < list.length; i++) {
if (isURLMatched(url, list[i])) {
return true;
}
}
return false;
}
/**
* Determines whether URL matches the template.
* @param url URL.
* @param urlTemplate URL template ("google.*", "youtube.com" etc).
*/
export function isURLMatched(url: string, urlTemplate: string): boolean {
const isFirstIPV6 = isIPV6(url);
const isSecondIPV6 = isIPV6(urlTemplate);
if (isFirstIPV6 && isSecondIPV6) {
return compareIPV6(url, urlTemplate);
} else if (!isSecondIPV6 && !isSecondIPV6) {
const regex = createUrlRegex(urlTemplate);
return Boolean(url.match(regex));
} else {
return false;
}
}
function createUrlRegex(urlTemplate: string): RegExp {
urlTemplate = urlTemplate.trim();
const exactBeginning = (urlTemplate[0] === '^');
const exactEnding = (urlTemplate[urlTemplate.length - 1] === '$');
urlTemplate = (urlTemplate
.replace(/^\^/, '') // Remove ^ at start
.replace(/\$$/, '') // Remove $ at end
.replace(/^.*?\/{2,3}/, '') // Remove scheme
.replace(/\?.*$/, '') // Remove query
.replace(/\/$/, '') // Remove last slash
);
let slashIndex: number;
let beforeSlash: string;
let afterSlash: string;
if ((slashIndex = urlTemplate.indexOf('/')) >= 0) {
beforeSlash = urlTemplate.substring(0, slashIndex); // google.*
afterSlash = urlTemplate.replace('$', '').substring(slashIndex); // /login/abc
} else {
beforeSlash = urlTemplate.replace('$', '');
}
//
// SCHEME and SUBDOMAINS
let result = (exactBeginning ?
'^(.*?\\:\\/{2,3})?' // Scheme
: '^(.*?\\:\\/{2,3})?([^\/]*?\\.)?' // Scheme and subdomains
);
//
// HOST and PORT
const hostParts = beforeSlash.split('.');
result += '(';
for (let i = 0; i < hostParts.length; i++) {
if (hostParts[i] === '*') {
hostParts[i] = '[^\\.\\/]+?';
}
}
result += hostParts.join('\\.');
result += ')';
//
// PATH and QUERY
if (afterSlash) {
result += '(';
result += afterSlash.replace('/', '\\/');
result += ')';
}
result += (exactEnding ?
'(\\/?(\\?[^\/]*?)?)$' // All following queries
: '(\\/?.*?)$' // All following paths and queries
);
//
// Result
return new RegExp(result, 'i');
}
export function isPDF(url: string) {
if (url.includes('.pdf')) {
if (url.includes('?')) {
url = url.substring(0, url.lastIndexOf('?'));
}
if (url.includes('#')) {
url = url.substring(0, url.lastIndexOf('#'));
}
if (url.match(/(wikipedia|wikimedia).org/i) && url.match(/(wikipedia|wikimedia)\.org\/.*\/[a-z]+\:[^\:\/]+\.pdf/i)) {
return false;
}
if (url.endsWith('.pdf')) {
for (let i = url.length; 0 < i; i--) {
if (url[i] === '=') {
return false;
} else if (url[i] === '/') {
return true;
}
}
} else {
return false;
}
}
return false;
}
export function isURLEnabled(url: string, userSettings: UserSettings, {isProtected, isInDarkList}) {
if (isProtected) {
return false;
}
if (isPDF(url) && userSettings.enableForPDF) {
return userSettings.enableForPDF;
}
const isURLInUserList = isURLInList(url, userSettings.siteList);
if (userSettings.applyToListedOnly) {
return isURLInUserList;
}
// TODO: Use `siteListEnabled`, `siteListDisabled`, `enabledByDefault` options.
// Delete `siteList` and `applyToListedOnly` options, transfer user's values.
const isURLInEnabledList = isURLInList(url, userSettings.siteListEnabled);
if (isURLInEnabledList && isInDarkList) {
return true;
}
return (!isInDarkList && !isURLInUserList);
}