-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadblock-getadblock.js
195 lines (171 loc) · 6.26 KB
/
adblock-getadblock.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
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
195
/*
* This file is part of AdBlock <https://getadblock.com/>,
* Copyright (C) 2013-present Adblock, Inc.
*
* AdBlock is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* AdBlock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AdBlock. If not, see <http://www.gnu.org/licenses/>.
*/
/* For ESLint: List any global identifiers used in this file below */
/* global browser, onReady */
const gabHostnames = ['getadblock.com', 'dev.getadblock.com', 'dev1.getadblock.com', 'dev2.getadblock.com', 'getadblockpremium.com'];
const gabHostnamesWithProtocal = gabHostnames.map(host => `https://${host}`);
// listen to messages from the background page
function onMessage(request, sender, sendResponse) {
if (Object.prototype.hasOwnProperty.call(request, 'dataMigrationStatus')) {
browser.runtime.onMessage.removeListener(onMessage);
window.postMessage({ dataMigrationStatus: request.dataMigrationStatus }, '*');
sendResponse({});
}
}
async function receiveMessage(event) {
if (
event.data
&& gabHostnamesWithProtocal.includes(event.origin)
&& event.data.command === 'payment_success'
) {
window.removeEventListener('message', receiveMessage);
browser.runtime.onMessage.addListener(onMessage);
const response = await browser.runtime.sendMessage({ command: 'payment_success', version: 1, origin: event.origin });
window.postMessage(response, '*');
}
}
window.addEventListener('message', receiveMessage, false);
async function unsubscribeAcceptableAds(event) {
if (event.isTrusted === false) {
return;
}
event.preventDefault();
await browser.runtime.sendMessage({ command: 'unsubscribe', adblockId: 'acceptable_ads' });
await browser.runtime.sendMessage({ command: 'recordGeneralMessage', msg: 'disableacceptableads_clicked' });
await browser.runtime.sendMessage({ command: 'openTab', urlToOpen: browser.runtime.getURL('options.html?aadisabled=true#general') });
}
function handleOpenSettingsPageClick(event) {
if (event.isTrusted === false) {
return;
}
event.preventDefault();
void browser.runtime.sendMessage({
command: 'openTab',
urlToOpen: browser.runtime.getURL('options.html#general'),
});
}
async function getStartedWithMyAdBlock(event) {
if (event.isTrusted === false) {
return;
}
event.stopImmediatePropagation();
event.preventDefault();
await browser.runtime.sendMessage({ command: 'openTab', urlToOpen: browser.runtime.getURL('options.html#mab') });
}
onReady(async () => {
if (gabHostnames.includes(document.location.hostname) && window.top === window.self) {
window.addEventListener('message', receiveMessage, false);
let response = await browser.storage.local.get('userid');
if (response.userid) {
const elemDiv = document.createElement('div');
elemDiv.id = 'adblockUserId';
elemDiv.innerText = response.userid;
elemDiv.style.display = 'none';
document.body.appendChild(elemDiv);
}
response = await browser.runtime.sendMessage({ command: 'isActiveLicense' });
const elemDiv = document.createElement('div');
elemDiv.id = 'isAdblockLicenseActive';
elemDiv.innerText = response;
elemDiv.style.display = 'none';
elemDiv.dataset.isAdblockLicenseActive = response;
document.body.appendChild(elemDiv);
document.querySelectorAll('#disableacceptableads').forEach((node) => {
node.addEventListener('click', unsubscribeAcceptableAds);
});
// Listen to clicks on links to open the settings page
document.querySelectorAll('.open-settings-page').forEach((node) => {
node.addEventListener('click', handleOpenSettingsPageClick);
});
// Listen to clicks on 'Get Started With MyAdBlock' on v4 payment page
document.querySelectorAll('.get-started-with-myadblock').forEach((node) => {
node.addEventListener('click', getStartedWithMyAdBlock);
});
// add click handler for adblock subscribe clicks
// similiar to the code here:
// https://github.com/adblockplus/adblockpluschrome/blob/master/subscriptionLink.postload.js
// the link host check ('subscribe.getadblock.com') is specific to the getadblock.com domain
document.addEventListener('click', (event) => {
// Ignore right-clicks
if (event.button === 2) {
return;
}
// Ignore simulated clicks.
if (event.isTrusted === false) {
return;
}
// Search the link associated with the click
let link = event.target;
while (!(link instanceof HTMLAnchorElement)) {
link = link.parentNode;
if (!link) {
return;
}
}
let queryString = null;
if (link.protocol === 'http:' || link.protocol === 'https:') {
if (link.host === 'subscribe.getadblock.com' && link.pathname === '/') {
queryString = link.search.substr(1);
}
} else {
return;
}
if (!queryString) {
return;
}
// This is our link - make sure the browser doesn't handle it
event.preventDefault();
event.stopPropagation();
// Decode URL parameters
let title = null;
let url = null;
for (const param of queryString.split('&')) {
const parts = param.split('=', 2);
if (parts.length === 2) {
switch (parts[0]) {
case 'title':
title = decodeURIComponent(parts[1]);
break;
case 'location':
url = decodeURIComponent(parts[1]);
break;
default: // do nothing
}
}
}
if (!url) {
return;
}
// Default title to the URL
if (!title) {
title = url;
}
// Trim spaces in title and URL
title = title.trim();
url = url.trim();
if (!/^(https?|ftp):/.test(url)) {
return;
}
browser.runtime.sendMessage({
type: 'subscriptions.add',
title,
url,
confirm: true,
});
}, true);
}
});