forked from sblask-webextensions/webextension-skip-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psl.js
41 lines (30 loc) · 959 Bytes
/
psl.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
/* global pslrules */
const psl = (() => {
if (typeof pslrules === 'undefined') {
try {
pslrules = require('./pslrules');
} catch (e) {
console.error('Error importing pslrules module');
throw e;
}
}
const getDomain = (hostname, previousHead = undefined) => {
if (!hostname) return undefined;
const [exception] = [...pslrules.EXCEPTION_ENTRIES].filter(([entry]) => entry === hostname);
if (exception) return hostname;
const dotIndex = hostname.indexOf('.');
if (dotIndex === -1) return undefined;
const [head, rest] = [hostname.slice(0, dotIndex), hostname.slice(dotIndex + 1)];
if (pslrules.WILDCARD_ENTRIES.has(rest) && previousHead) {
return [previousHead, head, rest].join('.');
}
if (pslrules.NORMAL_ENTRIES.has(rest)) {
return [head, rest].join('.');
}
return getDomain(rest, head);
};
return {
getDomain,
};
})(this);
module.exports = psl;