-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventPage.js
More file actions
167 lines (152 loc) · 4.63 KB
/
eventPage.js
File metadata and controls
167 lines (152 loc) · 4.63 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
/**
Copyright (C) 2014 Robotic-Brain <chrome-ext@roboticbrain.de>
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* TODO: This is not actually an event page since chrome does not support it
* switch to declarativeWebRequest when available
*/
/**
* This array contains all the needed parameters
*/
var wantedParameters = {
"tag": "sempervideo-21"
};
/**
* This function turns the paramter part of an url (after "?")
* into a key/value map
*
* @param string params the url after "?"
* @return object key/value map of GET parameters
*/
function strToMap(params) {
if ((typeof params) !== "string") {
return {};
}
var pairs = params.split("&");
var result = {};
for (var i=0; i < pairs.length; ++i) {
var kv = pairs[i].split("=");
result[kv[0]] = kv[1];
}
return result;
}
/**
* This function does the reverse of strToMap
* and rebulds a string out of the given key/value map
*
* @param object params the key/value map of all parameters
* @return string the string of GET parameters
*/
function mapToStr(params) {
var result = "";
var first = true;
for (var key in params) {
if (!first) {
result += "&";
}
result += key + "=" + params[key];
first = false;
}
return result;
}
/**
* This function takes the parameter part of an url
* and checks if params contains what we want
*
* @param object params parameter part of url in key/value pairs
* @param object wanted wanted url parameters
* @return bool true if url looks good
*/
function checkIfDone(params, wanted) {
for (var key in wanted) {
if (params[key] !== wanted[key]) {
return false;
}
}
return true;
}
/**
* This function takes a url and returns either false if the url is good
* or the new url where the browser should be redirected to
*
* @param string url current url
* @return false | string new url or false
*/
function checkRedirection(url) {
var urlParts = url.split("?", 2);
var params = strToMap(urlParts[1]);
if (!checkIfDone(params, wantedParameters)) {
for (var key in wantedParameters) {
params[key] = wantedParameters[key];
}
var newUrl = urlParts[0] + "?" + mapToStr(params);
return newUrl;
}
return false;
}
/*******************************************
*
* Event Handlers
*
*******************************************/
/**
* Handler function for onBeforeRequest event
*
* this function does the actual work of the
* extension and causes a redirect if necessary
*/
function onBeforeRequestHandler(args) {
var result = checkRedirection(args.url);
if (result !== false) {
return {redirectUrl: result};
}
}
/**
* Handler function for onBeforeRedirect event
*
* this is only a convenience function to detect extension conflicts
* if a redirect occured but the new target does not contain our parameters
* some other mechanism must have overwriten our redirect
*/
function onBeforeRedirectHandler(args) {
if (args.statusCode < 0) { // ignore redirects from the server
if (!checkIfDone(strToMap(args.redirectUrl.split("?", 2)[1]), wantedParameters)) {
chrome.notifications.create(
"semperVideoError",
{
"type": "basic",
"priority": 2,
"title": chrome.i18n.getMessage("notificationTitle"),
"message": chrome.i18n.getMessage("notificationMessage"),
"iconUrl": "icons/klappe128-warn.png"
},
function(id){}
);
}
}
}
/**
* Register the event handlers
*/
var filter = {urls: ["*://*/*"]}; /* urls already filtered by manifest.json */
chrome.webRequest.onBeforeRequest.addListener(
onBeforeRequestHandler,
filter,
["blocking"]
);
/**
* Notify the user on extension conflicts
*/
chrome.webRequest.onBeforeRedirect.addListener(
onBeforeRedirectHandler,
filter
);