/
blocked.js
116 lines (104 loc) · 3.72 KB
/
blocked.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
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
const urlParams = new URLSearchParams(window.location.search);
const originalLink = urlParams.get('u');
var targetLink = originalLink;
function hasBrowserObject() {
// see: https://github.com/mozilla/webextension-polyfill/blob/master/src/browser-polyfill.js
return !(typeof globalThis.browser === 'undefined' ||
Object.getPrototypeOf(globalThis.browser) !== Object.prototype);
}
function setLink(link) {
targetLink = link;
document.querySelector('span').textContent = targetLink;
document.querySelector('#visit').setAttribute('href', targetLink);
window.history.replaceState(null, '', '?u=' + encodeURIComponent(targetLink));
setRootLink();
}
function editLink(navigateToEnd) {
const linkElement = document.querySelector('span');
linkElement.contentEditable = true;
if (navigateToEnd && linkElement.textContent.length > 0) {
const selection = window.getSelection();
const range = document.createRange();
range.setStart(linkElement, 1);
range.setEnd(linkElement, 1);
selection.removeAllRanges();
selection.addRange(range);
}
linkElement.focus();
}
function openIncognito() {
browser = hasBrowserObject() ? browser : chrome;
if (typeof browser.extension.isAllowedIncognitoAccess === 'function') {
browser.extension.isAllowedIncognitoAccess(function(isAllowed) {
if (isAllowed) {
browser.windows.create({incognito: true, url: targetLink});
} else {
window.alert('The ATP link cleaner extension has not been granted permission to run in private mode. ' +
'Please grant this permission and try again.');
}
});
} else {
browser.windows.create({incognito: true, url: targetLink});
}
}
function copyLink() {
const copySource = document.createElement('textarea');
copySource.value = targetLink;
copySource.setAttribute('readonly', '');
copySource.style.position = 'absolute';
copySource.style.left = '-9999px';
document.body.appendChild(copySource);
copySource.select();
document.execCommand('copy');
document.body.removeChild(copySource);
document.querySelector('icon.copy').classList.remove('hidden');
}
function setRootLink() {
const rootLink = document.querySelector('#root');
const rootLinkParts = targetLink.split(/[/?]/);
if (rootLinkParts.length >= 3) {
rootLink.setAttribute('href', rootLinkParts[0] + '//' + rootLinkParts[2] + '/');
} else {
rootLink.setAttribute('href', '#');
}
}
function stripLink() {
setLink(targetLink.split('?')[0]);
}
function resetLink() {
setLink(originalLink);
}
document.addEventListener('DOMContentLoaded', function(){
if (targetLink) {
setLink(targetLink);
const linkElement = document.querySelector('span');
linkElement.addEventListener('keydown', function(event) {
if (event.keyCode == 13) {
event.preventDefault();
linkElement.contentEditable = false;
if (event.metaKey) {
document.querySelector('#visit').click();
}
}
}, false);
linkElement.addEventListener('input', function() {
setLink(this.textContent);
}, false);
linkElement.addEventListener('click', function() { editLink(false); });
document.querySelector('#edit').addEventListener('click', function() { editLink(true); });
document.querySelector('#copy').addEventListener('click', copyLink);
const incognito = document.querySelector('#incognito');
if (!hasBrowserObject()) {
incognito.textContent = incognito.textContent.replace('incognito', 'private');
}
incognito.addEventListener('click', openIncognito);
document.querySelector('#strip').addEventListener('click', stripLink);
document.querySelector('#reset').addEventListener('click', resetLink);
} else {
document.querySelector('#edit').classList.add('hidden');
const options = document.querySelectorAll('.options');
options.forEach(option => {
option.classList.add('hidden');
});
}
});