-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
457 lines (408 loc) · 16.8 KB
/
Copy pathbackground.js
File metadata and controls
457 lines (408 loc) · 16.8 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
Copyright 2016,2017 Taylor Raack.
This file is part of PageAccel.
PageAccel is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PageAccel 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 PageAccel. If not, see <http://www.gnu.org/licenses/>.
*/
var working = false;
var publicSuffixList = this.publicSuffixList;
var thestorage = window.localStorage;
function logpa(message, tabId) {
console.log(new Date().toISOString() + (tabId ? (" - tab " + tabId) : "") + " - " + message);
}
function getFromStorage(key, callback) {
var item = JSON.parse(thestorage.getItem(key));
callback(item != null ? item : {});
}
function getTabAndSiteStatus(callback) {
var tabstatus = thestorage.getItem('tabstatus');
if (tabstatus == null) tabstatus = '{}';
var sitestatus = thestorage.getItem('sitestatus');
if (sitestatus == null) sitestatus = '{}';
callback(JSON.parse(tabstatus), JSON.parse(sitestatus));
}
function getTabStatus(callback) {
getFromStorage('tabstatus', callback);
}
function getSiteStatus(callback) {
getFromStorage('sitestatus', callback);
}
function setToStorage(key, value, callback) {
thestorage.setItem(key, JSON.stringify(value));
callback();
}
function setTabStatus(tabStatus, callback) {
setToStorage('tabstatus', tabStatus, callback);
}
function setSiteStatus(siteStatus, callback) {
setToStorage('sitestatus', siteStatus, callback);
}
function primePublicSuffixList() {
publicSuffixList.parse("com", punycode.toASCII)
}
function updatePageActionIcon(tabId, senderUrl, status) {
logpa("updating page action icon; canonical: " + status.canonicalUrl + "; amp: " + status.ampUrl + "; on amp page: " + status.onAmpPage, tabId);
if (status.canonicalUrl != null && status.canonicalUrl != senderUrl && status.onAmpPage != null && status.onAmpPage) {
logpa("canonical url is " + status.canonicalUrl + " and we are on an amp page", tabId)
// we are currently viewing an amp page
chrome.pageAction.setIcon({ tabId : tabId, path : 'canonical.png' });
chrome.pageAction.setTitle({ tabId : tabId, title : 'Show the Standard version of this page' });
chrome.pageAction.show(tabId);
logpa("setting to is on amp page icon", tabId);
} else if (status.ampUrl != null && status.onAmpPage != null && !status.onAmpPage) {
logpa("amp url is " + status.ampUrl + " and we are NOT on an amp page", tabId)
// we are not currently viewing an amp page
chrome.pageAction.setIcon({ tabId : tabId, path : 'amplify.png' });
chrome.pageAction.setTitle({ tabId : tabId, title : 'Show the accelerated version of this page' });
chrome.pageAction.show(tabId);
logpa("setting to is on canonical page icon", tabId);
}
}
function checkAndWork(fcn) {
if (working) {
setTimeout(function() { checkAndWork(fcn) }, 5);
} else {
working = true;
fcn();
}
}
function isSimplifyEnabled(tabId, thisTabStatus, sitestatus) {
var domain = getMasterDomain(thisTabStatus);
var enabled = domain in sitestatus ? sitestatus[domain] : true;
logpa("simplify " + (enabled ? "enabled" : "disabled") + " for domain " + domain, tabId);
return enabled;
}
chrome.webNavigation.onCommitted.addListener(function(data) {
var thewindow = this;
if (data.transitionQualifiers != 'forward_back') {
// not a user click on forward or back; ignore
return;
}
checkAndWork(function() {
getTabStatus(function(tabStatus) {
var status = data.tabId in tabStatus ? tabStatus[data.tabId] : {};
var lastUrl = 'swithedurls' in status ? status['swithedurls'] : [];
if (lastUrl.length > 0 && lastUrl[lastUrl.length-1] == data.url) {
logpa("detected forward/backward change with previous url; assuming back button was clicked");
lastUrl.pop();
status['swithedurls'] = lastUrl;
status['goback'] = true;
status['pageaccelblock'] = true;
logpa("blocking future changes", data.tabId);
logpa("setting go back to true; lasturl was " + data.url, data.tabId);
tabStatus[data.tabId] = status;
setTabStatus(tabStatus, function() { working = false; });
} else {
working = false;
}
});
});
// do whatever else I need this specific tab in question to do
});
chrome.webNavigation.onCompleted.addListener(function(data) {
// Fired when a document, including the resources it refers to, is completely loaded and initialized.
// this means after all messages from html scanning that might send messages to alter pageaccel behavior
checkAndWork(function() {
getTabStatus(function(tabStatus) {
var status = data.tabId in tabStatus ? tabStatus[data.tabId] : {};
status['pageaccelblock'] = false;
tabStatus[data.tabId] = status;
setTabStatus(tabStatus, function() { working = false; });
});
});
});
function buildAmpUrl(status) {
if (status.ampUrl.startsWith('//')) {
return status['origin'].split("/")[0] + status.ampUrl;
} else if (status.ampUrl.startsWith('/')) {
return status['origin'] + status.ampUrl;
} else {
return status.ampUrl;
}
}
function recordAmpSwitched() {
getFromStorage('ampSwitched', function(item) {
var newCount = item['count'] + 1;
if (Number.isInteger(Math.log(newCount)/Math.log(5))) {
// display a notification and increase the counter once the user has closed the notification
displayNotification(
newCount == 1 ?
"You're viewing your first PageAccel simplified page!" :
"PageAccel is still simplifying your web browsing experience!",
newCount == 1 ?
"Enjoy!" :
"You've viewed " + newCount + " simplified pages with PageAccel.", function () {
setToStorage('ampSwitched', {'count':newCount}, function() {});
});
} else {
// don't need to show a notification right now, just increase counter
setToStorage('ampSwitched', {'count':newCount}, function() {});
}
});
}
function displayNotification(title, message, onClose) {
chrome.permissions.contains({permissions: ['notifications']}, function(result) {
if (result) {
chrome.notifications.create(
{ type: "basic", title: title, message: message, iconUrl: "amplifier128.png"},
function(notificationId) {
chrome.notifications.onClosed.addListener(onClose);
});
}
});
}
/*
* AMP document discovery (https://www.ampproject.org/docs/reference/spec#amp-document-discovery)
*
* If an AMP document exists which is an alternative representation of a canonical document, then the canonical document should point to the AMP document via a link tag with the relation "amphtml".
* for example: <link rel="amphtml" href="https://www.example.com/url/to/amp/document.html">
*
* The AMP document itself is expected to point back to its canonical document document via a link tag with the relation "canonical".
* for example: <link rel="canonical" href="https://www.example.com/url/to/canonical/document.html">
*
* If a single resource is simultaneously the AMP and the canonical document, the canonical relation should point to itself--no "amphtml" relation is required.
*/
function processTabState(tabId, senderUrl) {
logpa("processing tab state", tabId);
getTabAndSiteStatus(function(tabStatus, sitestatus) {
var status = tabId in tabStatus ? tabStatus[tabId] : {};
if (status.canonicalUrl != null && status.canonicalUrl != senderUrl && status.onAmpPage != null && status.onAmpPage && !isSimplifyEnabled(tabId, status, sitestatus)) {
logpa("switching to canonical url", tabId);
var lastUrl = 'swithedurls' in status ? status['swithedurls'] : [];
if(lastUrl.length == 0 || lastUrl[lastUrl.length - 1] != senderUrl) {
lastUrl.push(senderUrl);
}
status['swithedurls'] = lastUrl;
// block other events from triggering duplicate url change events until end of load, which confuse chrome (it might just decide not to switch urls)
status['pageaccelblock'] = true;
tabStatus[tabId] = status;
logpa("setting previous url to " + senderUrl, tabId);
setTabStatus(tabStatus, function() {
working = false;
chrome.tabs.update(tabId, { url : status.canonicalUrl });
});
} else if (status.ampUrl != null && status.onAmpPage != null && !status.onAmpPage && isSimplifyEnabled(tabId, status, sitestatus)) {
logpa("switching to amp url", tabId);
var lastUrl = 'swithedurls' in status ? status['swithedurls'] : [];
if(lastUrl.length == 0 || lastUrl[lastUrl.length - 1] != senderUrl) {
lastUrl.push(senderUrl);
}
status['swithedurls'] = lastUrl;
status['lastSwitchedUrl'] = senderUrl;
// block other events from triggering duplicate url change events until end of load, which confuse chrome (it might just decide not to switch urls)
status['pageaccelblock'] = true;
tabStatus[tabId] = status;
logpa("setting previous url to " + senderUrl, tabId);
setTabStatus(tabStatus, function() {
working = false;
recordAmpSwitched();
chrome.tabs.update(tabId, { url : buildAmpUrl(status) });
});
} else {
updatePageActionIcon(tabId, senderUrl, status);
working = false;
}
});
}
function handleUpdate(sender, key, value) {
if (value == null) throw key + " cannot be null";
logpa("got new " + key + ": " + value, sender.tab.id);
checkAndWork(function() {
getTabStatus(function(tabStatus) {
var status = sender.tab.id in tabStatus ? tabStatus[sender.tab.id] : {};
if (!('pageaccelblock' in status) || status['pageaccelblock'] == false) {
status[key] = value;
tabStatus[sender.tab.id] = status;
setTabStatus(tabStatus, function() { processTabState(sender.tab.id, sender.url); });
} else {
logpa("forbidden from handling update by pageaccelblock", sender.tab.id);
working = false;
}
});
});
}
function handleOnAmpPage(sender, onAmpPage) {
handleUpdate(sender, 'onAmpPage', onAmpPage);
}
function handleCanonicalUrl(sender, canonicalUrl) {
handleUpdate(sender, 'canonicalUrl', canonicalUrl);
}
function handleAmpUrl(sender, ampUrl) {
handleUpdate(sender, 'ampUrl', ampUrl);
}
function handleClear(sender) {
var tabId = sender.tab.id;
logpa("got new clear", tabId);
checkAndWork(function() {
getTabStatus(function(tabStatus) {
var status = tabId in tabStatus ? tabStatus[tabId] : {};
var lasturl = 'swithedurls' in status ? status['swithedurls'] : [];
var goback = 'goback' in status ? status['goback'] : false;
if ('lastSwitchedUrl' in status && status['lastSwitchedUrl'] == sender.url) {
// the last thing we did was switch from a canonical url, and now we've arrived back at that canonical url
// so there must have been a redirect that brought us back
// so let's not force another amp redirect
logpa("likely redirect detected; not loading pageaccel for this page load", tabId);
status['pageaccelblock'] = true;
}
var pageaccelblock = 'pageaccelblock' in status ? status['pageaccelblock'] : false;
status = {'swithedurls' : lasturl, 'goback' : goback, 'pageaccelblock' : pageaccelblock, 'lastTabLoadTime' : Date.now()};
tabStatus[tabId] = status;
setTabStatus(tabStatus, function() { working = false });
});
});
}
function handleOrigin(tabId, origin) {
checkAndWork(function() {
getTabStatus(function(tabStatus) {
var status = tabId in tabStatus ? tabStatus[tabId] : {};
status['origin'] = origin;
logpa('got new origin ' + origin, tabId);
tabStatus[tabId] = status;
setTabStatus(tabStatus, function() { working = false });
});
});
}
function handleGetBack(sender, callback) {
var theurl = sender.url;
checkAndWork(function() {
getTabStatus(function(tabStatus) {
var status = sender.tab.id in tabStatus ? tabStatus[sender.tab.id] : {};
var oldGoBack = 'goback' in status ? status['goback'] : false;
status['goback'] = false;
tabStatus[sender.tab.id] = status;
setTabStatus(tabStatus, function() {
logpa("going back " + oldGoBack + " from " + theurl, sender.tab.id);
callback(oldGoBack);
working = false;
});
});
});
// must return true to indicate that callback is going to be called later, asynchronously
return true;
}
function handleGetEnabled(sender, callback) {
getTabAndSiteStatus(function(tabstatus, sitestatus) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var domain = getMasterDomain(tabstatus[tabs[0].id]);
callback(domain in sitestatus ? sitestatus[domain] : true);
});
});
// must return true to indicate that callback is going to be called later, asynchronously
return true;
}
function getMasterDomain(thisTabStatus) {
return publicSuffixList.getDomain(new URL(thisTabStatus.onAmpPage == true ? thisTabStatus.canonicalUrl : thisTabStatus.origin).hostname)
}
function handleToggleEnabled(sender, inputhostname, callback) {
getTabAndSiteStatus(function(tabstatus, sitestatus) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var masterDomain = inputhostname != null ? publicSuffixList.getDomain(new URL(inputhostname).hostname) : getMasterDomain(tabstatus[tabs[0].id]);
// flip domain enabled, or set to false if it's never been set
sitestatus[masterDomain] = (masterDomain in sitestatus) ? !sitestatus[masterDomain] : false;
logpa("setting simplify enabled for " + masterDomain + " to " + sitestatus[masterDomain], tabs[0].id);
setSiteStatus(sitestatus, inputhostname != null ? callback : function() {
// reload the page, which will force the proper loading to occur again
logpa("reloading", tabs[0].id);
chrome.tabs.reload(tabs[0].id);
});
});
});
return inputhostname != null;
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.sentinel === undefined || message.sentinel != "__SIMPLIFYMESSAGE__") {
return;
}
var tabId = sender.tab != null ? sender.tab.id : null;
logpa("received action " + message.method + " with data " + message.data + " url " + sender.url, tabId);
var response = false;
switch (message.method) {
case "clear":
handleClear(sender);
break;
case "origin":
handleOrigin(sender.tab.id, message.data);
break;
case "onAmpPage":
handleOnAmpPage(sender, message.data);
break;
case "ampUrl":
handleAmpUrl(sender, message.data);
break;
case "canonicalUrl":
handleCanonicalUrl(sender, message.data);
break;
case "getEnabled":
response = handleGetEnabled(sender, sendResponse);
break;
case "getBack":
response = handleGetBack(sender, sendResponse);
break;
case "toggleEnabled":
response = handleToggleEnabled(sender, message.data, sendResponse);
default:
break;
}
logpa("completed action " + message.method, tabId);
return response;
});
var uninstallTimer = null;
var uninstallUrlBase = "http://pageaccel.raack.info/uninstall_survey"
function updateUninstallUrl() {
getFromStorage('installTime', function(item) {
logpa("updating install url");
chrome.runtime.setUninstallURL(uninstallUrlBase + "?installed_for=" + (Date.now() - ('time' in item ? item['time'] : Date.now())));
});
}
updateUninstallUrl();
function showSplashScreenOnFirstRun() {
getFromStorage('tutorialShown', function(item) {
if (!('shown' in item)) {
chrome.tabs.create({
url: chrome.extension.getURL("firstRun2.html")
});
setToStorage('tutorialShown', {'shown':true}, function() {});
}
});
}
function setUpInstallUninstallActions() {
getFromStorage('installTime', function(item) {
if (!('time' in item)) {
setToStorage('installTime', {'time':Date.now()}, function() {})
}
})
if(uninstallTimer == null) {
// update uninstall URL every 5 minutes
uninstallTimer = setInterval(updateUninstallUrl, 60 * 1000);
}
}
function primeAmpSwitchedCount() {
getFromStorage('ampSwitched', function(item) {
if (!('count' in item)) {
setToStorage('ampSwitched', {'count':0}, function() {});
}
});
}
function verifyNotificationPermissions() {
chrome.permissions.contains({permissions: ['notifications']}, function(result) {
if (!result) {
chrome.tabs.create({
url: chrome.extension.getURL("nonavigation.html")
});
}
});
}
primePublicSuffixList();
setUpInstallUninstallActions();
showSplashScreenOnFirstRun();
primeAmpSwitchedCount();
verifyNotificationPermissions();