-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpremium.preload.js
192 lines (174 loc) · 4.63 KB
/
premium.preload.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
/*
* 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, cloneInto */
/**
* List of events that are waiting to be processed
*/
const eventQueue = [];
/**
* Maximum number of failed requests after which events stop being handled
*/
const maxErrorThreshold = 30;
/**
* Maximum number of events that can be queued up
*/
const maxQueuedEvents = 20;
/**
* Interval period in milliseconds at which events are processed
*/
const processingDelay = 100;
/**
* Number of failed requests
*/
let errorCount = 0;
/**
* Interval identifier for processing events
*/
let processingIntervalId = null;
/**
* Retrieves requested payload from background page
*
* @param {Event} event - "flattr-request-payload" DOM event
*
* @returns {Promise<string|null>} payload - Encoded signed Premium license data
*/
async function getPayload(event) {
/* eslint-disable-next-line no-use-before-define */
if (!isTrustedEvent(event)) {
return null;
}
/* eslint-disable-next-line no-use-before-define */
if (!isAuthRequestEvent(event)) {
return null;
}
const payload = await browser.runtime.sendMessage({
command: 'users.isPaying',
timestamp: event.detail.timestamp,
signature: event.detail.signature,
});
return payload;
}
/**
* Queues up incoming requests
*
* @param {Event} event - "flattr-request-payload" DOM event
*/
function handleFlattrRequestPayloadEvent(event) {
if (eventQueue.length >= maxQueuedEvents) {
return;
}
eventQueue.push(event);
/* eslint-disable-next-line no-use-before-define */
startProcessingInterval();
}
/**
* Checks whether event contains authentication data
*
* @param {Event} event - Event
*
* @returns {boolean} whether event contains authentication data
*/
function isAuthRequestEvent(event) {
return (
event.detail
&& typeof event.detail.signature === 'string'
&& typeof event.detail.timestamp === 'number'
);
}
/**
* Check whether incoming event hasn't been tampered with
*
* @param {Event} event - DOM event
*
* @returns {boolean} whether the event can be trusted
*/
function isTrustedEvent(event) {
return Object.getPrototypeOf(event) === CustomEvent.prototype
&& !Object.hasOwnProperty.call(event, 'detail');
}
/**
* Processes incoming requests
*
* @returns {Promise}
*/
async function processNextEvent() {
const event = eventQueue.shift();
if (event) {
try {
const payload = await getPayload(event);
if (!payload) {
throw new Error('Premium request rejected');
}
let detail = { detail: { payload } };
if (typeof cloneInto === 'function') {
// Firefox requires content scripts to clone objects
// that are passed to the document
detail = cloneInto(detail, document.defaultView);
}
document.dispatchEvent(
new CustomEvent('flattr-payload', detail),
);
/* eslint-disable-next-line no-use-before-define */
stop();
} catch (e) {
errorCount += 1;
if (errorCount >= maxErrorThreshold) {
/* eslint-disable-next-line no-use-before-define */
stop();
}
}
}
if (!eventQueue.length) {
/* eslint-disable-next-line no-use-before-define */
stopProcessingInterval();
}
}
/**
* Starts interval for processing incoming requests
*/
function startProcessingInterval() {
if (processingIntervalId) {
return;
}
processNextEvent();
processingIntervalId = setInterval(processNextEvent, processingDelay);
}
/**
* Stops interval for processing incoming requests
*/
function stopProcessingInterval() {
clearInterval(processingIntervalId);
processingIntervalId = null;
}
/**
* Initializes module
*/
function start() {
document.addEventListener('flattr-request-payload',
handleFlattrRequestPayloadEvent, true);
}
/**
* Uninitializes module
*/
function stop() {
document.removeEventListener('flattr-request-payload',
handleFlattrRequestPayloadEvent, true);
eventQueue.length = 0;
stopProcessingInterval();
}
start();