-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
102 lines (95 loc) · 2.75 KB
/
background.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
/**
* Namespace
* @type {Object}
*/
UofTLibApp = {};
/**
* Override the following links.
* @type {Object}
*/
UofTLibApp.LINKS = {
'dl.acm.org': 'dl.acm.org.myaccess.library.utoronto.ca',
'ieeexplore.ieee.org': 'ieeexplore.ieee.org.myaccess.library.utoronto.ca',
'www.engineeringvillage.com': 'www-engineeringvillage-com.myaccess.library.utoronto.ca',
'www.scopus.com': 'www-scopus-com.myaccess.library.utoronto.ca',
'apps.webofknowledge.com': 'apps.webofknowledge.com.myaccess.library.utoronto.ca',
'search.proquest.com': 'search.proquest.com.myaccess.library.utoronto.ca',
'www.sciencedirect.com': 'www.sciencedirect.com.myaccess.library.utoronto.ca'
};
/**
* Generates the callack handler function.
* @param {string} link
* @return {function} callback.
*/
UofTLibApp.redirectCallBack = function (link) {
return function (details) {
this.analyticTrack('Redirect', details.url, link);
return { redirectUrl: details.url.replace(link, this.LINKS[link]) };
}.bind(this);
};
/**
* Sets Up Redirect Listeners.
*/
UofTLibApp.setUpListeners = function () {
for (var link in this.LINKS) {
chrome.webRequest.onBeforeRequest.addListener(
this.redirectCallBack(link),
// Applies to following url patterns
{ urls: ['*://' + link + '/*'] },
// In request blocking mode
['blocking']
);
}
};
/**
* Loads Google Analytics source.
* The new universal tracking is painful to set up.
*/
UofTLibApp.LoadGoogleAnalytics = function () {
if (!window.ga) {
// Load ga code.
window.ga = function() {
(window.ga.q = window.ga.q || []).push(arguments);
}, window.ga.l = 1 * new Date();
var tag = 'script';
var a = document.createElement(tag);
var m = document.getElementsByTagName(tag)[0];
a.async = 1;
a.src = 'https://www.google-analytics.com/analytics.js';
m.parentNode.insertBefore(a, m);
// Setup account.
ga('create', 'UA-90932606-1', 'auto');
// The need this for some reason.
ga('set', 'checkProtocolTask', null);
// I don't even know wtf does this do.
ga('require', 'displayfeatures');
// I need to override this manually because GA will reject urls beginning
// with chrome://... wtf!
ga('send', 'pageview', '/background.html');
// For personal tracking.
this.analyticTrack('Load', 'Background', 'Active');
}
};
/**
* Reports event to GA.
* @param {String} category
* @param {String} action
* @param {String} label
*/
UofTLibApp.analyticTrack = function (category, action, label) {
ga('send', {
hitType: 'event',
eventCategory: category,
eventAction: action,
eventLabel: label
});
};
/**
* Init code.
*/
UofTLibApp.init = function () {
this.setUpListeners();
this.LoadGoogleAnalytics();
};
// Init
UofTLibApp.init();