Repository navigation
Expand file tree
/
Copy pathcontent_script.js
More file actions
55 lines (49 loc) · 1.46 KB
/
Copy pathcontent_script.js
File metadata and controls
55 lines (49 loc) · 1.46 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
function normalize(url) {
try {
return new URL(url).hostname.replace(/^www\./, '').toLowerCase();
} catch {
return '';
}
}
function isBlocked(hostname, list) {
const h = hostname.replace(/^www\./, '').toLowerCase();
return list.some((site) => {
const s = site.replace(/^www\./, '').toLowerCase();
return s === h || h.endsWith('.' + s);
});
}
function isJustUnlocked(hostname) {
try {
return sessionStorage.getItem('sitelock_unlock_' + hostname) === '1';
} catch {
return false;
}
}
function check() {
const hostname = normalize(location.href);
if (!hostname) return;
if (isJustUnlocked(hostname)) return;
chrome.storage.local.get(['blockedSites', 'unlockedSites', 'lockPageStyle'], (res) => {
const list = res.blockedSites || [];
const unlocked = res.unlockedSites || [];
const style = res.lockPageStyle || 'default';
if (isBlocked(hostname, list) && !unlocked.includes(hostname)) {
location.replace(
chrome.runtime.getURL('blocked.html?style=' + encodeURIComponent(style) + '&target=' + encodeURIComponent(location.href))
);
}
});
}
function patchHistory(method) {
const original = history[method];
history[method] = function (...args) {
const result = original.apply(this, args);
setTimeout(check, 0);
return result;
};
}
patchHistory('pushState');
patchHistory('replaceState');
window.addEventListener('popstate', check);
window.addEventListener('hashchange', check);
check();