-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
143 lines (133 loc) · 4.65 KB
/
popup.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
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
var ext_api = (typeof browser === 'object') ? browser : chrome;
var manifestData = ext_api.runtime.getManifest();
var navigator_ua = navigator.userAgent;
var navigator_ua_mobile = navigator_ua.toLowerCase().includes('mobile');
var custom_switch = ((manifestData.optional_permissions && manifestData.optional_permissions.length) || (manifestData.optional_host_permissions && manifestData.optional_host_permissions.length)) && !navigator_ua_mobile;
function popup_show_toggle(domain, enabled) {
if (domain) {
var site_switch_span = document.getElementById('site_switch_span');
let labelEl = document.createElement('label');
labelEl.setAttribute('class', 'switch');
let inputEl = document.createElement('input');
inputEl.setAttribute('id', 'site_switch');
inputEl.setAttribute('type', 'checkbox');
if (enabled)
inputEl.setAttribute('checked', true);
labelEl.appendChild(inputEl);
let spanEl = document.createElement('span');
spanEl.setAttribute('class', 'slider round');
spanEl.setAttribute('title', 'en/disable current site/group in BPC');
labelEl.appendChild(spanEl);
site_switch_span.appendChild(labelEl);
document.getElementById("site_switch").addEventListener('click', function () {
ext_api.runtime.sendMessage({
request: 'site_switch'
});
//open(location).close();
});
}
};
ext_api.runtime.sendMessage({
request: 'popup_show_toggle'
});
ext_api.runtime.onMessage.addListener(function (message, sender) {
if (message.msg === 'popup_show_toggle' && message.data) {
popup_show_toggle(message.data.domain, message.data.enabled)
}
});
var cookie_domain;
ext_api.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
if (tabs && tabs[0] && /^http/.test(tabs[0].url)) {
let hostname = new URL(tabs[0].url).hostname;
cookie_domain = getCookiePermDomain(hostname);
}
});
document.getElementById("clear_cookies").addEventListener('click', function () {
if (custom_switch)
ext_api.permissions.request({
origins: ["*://*." + cookie_domain + "/*"]
}, function (granted) {
if (granted) {
ext_api.runtime.sendMessage({
request: 'clear_cookies'
});
}
});
else
ext_api.permissions.contains({
origins: ["*://*." + cookie_domain + "/*"]
}, function (result) {
if (result) {
ext_api.runtime.sendMessage({
request: 'clear_cookies'
});
}
});
});
function showArchiveLinks() {
ext_api.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
if (tabs && tabs[0] && /^http/.test(tabs[0].url)) {
let url = tabs[0].url.split('?')[0];
let url_enc = encodeURIComponent(url);
let hostname = urlHost(url);
let archive_array = {
'Archive.today': 'https://archive.today?run=1&url=' + url_enc,
'Google webcache': 'https://webcache.googleusercontent.com/search?q=cache:' + url_enc,
'12ft.io': 'https://12ft.io/' + url,
'Google Search Tool\n(use online html-viewer - no fix)': 'https://search.google.com/test/rich-results?url=' + url_enc
};
let archive_id = document.querySelector('span#archive');
if (archive_id) {
archive_id.appendChild(document.createTextNode('Open tab in:'));
for (let key in archive_array) {
let elem_div = document.createElement('div');
let elem = document.createElement('a');
elem.innerText = key;
if (!(matchDomain(['12ft.io', 'google.com', 'googleusercontent.com'], hostname) || hostname.match(/^archive\.\w{2}$/))) {
elem.href = archive_array[key];
elem.target = '_blank';
elem_div.appendChild(elem);
archive_id.appendChild(elem_div);
}
}
}
}
});
}
showArchiveLinks();
function matchDomain(domains, hostname = window.location.hostname) {
let matched_domain = false;
if (typeof domains === 'string')
domains = [domains];
domains.some(domain => (hostname === domain || hostname.endsWith('.' + domain)) && (matched_domain = domain));
return matched_domain;
}
function urlHost(url) {
if (/^http/.test(url)) {
try {
return new URL(url).hostname;
} catch (e) {
console.log(`url not valid: ${url} error: ${e}`);
}
}
return url;
}
function closeButton() {
window.close();
}
function getCookiePermDomain(hostname) {
let domain = hostname.replace(/^(www|amp(html)?|m|wap)(\d)?\./, '');
let domain_split = domain.split('.');
let num = 2;
if (domain_split.length > 2 && domain.match(/(\w){2,4}\.(\w){2}$/))
num = 3;
domain = domain_split.slice(-num).join('.');
return domain;
}
document.getElementById("button-close").addEventListener('click', closeButton);