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
194 lines (170 loc) · 5.38 KB
/
Copy pathurl.ts
File metadata and controls
194 lines (170 loc) · 5.38 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import type {UserSettings} from '../definitions';
import {isIPV6, compareIPV6} from './ipv6';
let anchor: HTMLAnchorElement;
export const parsedURLCache = new Map<string, URL>();
function fixBaseURL($url: string) {
if (!anchor) {
anchor = document.createElement('a');
}
anchor.href = $url;
return anchor.href;
}
export function parseURL($url: string, $base: string = null) {
const key = `${$url}${$base ? ';' + $base : ''}`;
if (parsedURLCache.has(key)) {
return parsedURLCache.get(key);
}
if ($base) {
const parsedURL = new URL($url, fixBaseURL($base));
parsedURLCache.set(key, parsedURL);
return parsedURL;
}
const parsedURL = new URL(fixBaseURL($url));
parsedURLCache.set($url, parsedURL);
return parsedURL;
}
export function getAbsoluteURL($base: string, $relative: string) {
if ($relative.match(/^data\:/)) {
return $relative;
}
const b = parseURL($base);
const a = parseURL($relative, b.href);
return a.href;
}
export function getURLHostOrProtocol($url: string) {
const url = new URL($url);
if (url.host) {
return url.host;
} else {
return url.protocol;
}
}
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 (!isFirstIPV6 && !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(/\$/g, '').substring(slashIndex); // /login/abc
} else {
beforeSlash = urlTemplate.replace(/\$/g, '');
}
//
// 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; i > 0; 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 && !userSettings.enableForProtectedPages) {
return false;
}
if (isPDF(url)) {
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);
}