-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbgp.js
More file actions
69 lines (57 loc) · 1.9 KB
/
bgp.js
File metadata and controls
69 lines (57 loc) · 1.9 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
/**
* Singleton object that has the posiblity to redirect http trafic
*
* note: some of the headers may not be passed on, check the documentationf or webRequest
* for further info.
**/
var infectorSingletonInstance = (function() {
var _formData = '';
var _lastRequestId = -1;
// tells us that the infector is listening or not
var _active = false;
/**
* Private method that contains the logic for http listening
*
* @param {Object} details contains information about the current request
* @private
**/
function _webRequestListner(request) {
console.log(request)
if(request.requestId !== _lastRequestId && (request.url.indexOf(_formData.urlsToTriggerOn)) > -1 ){
_lastRequestId = request.requestId;
return {
redirectUrl : request.url.replace(_formData.urlReplacePattern, _formData.urlReplaceContent)
};
}
}
// public methods
return {
/**
* Start to listen on the http trafic
*
* it will unregister any existing listener
**/
infectHttpTrafic: function (formData) {
// copy formData to private formData so we can access it in the webRequestListner
_formData = formData;
// make sure that we remove the old listner if possible
this.stopInfectingTrafic();
// add the listner with a named method, so that we can remove it later
chrome.webRequest.onBeforeRequest.addListener(_webRequestListner, {
urls: ["http://*/*"]
}, ["blocking"]); // Block intercepted requests until this handler has finished
_active = true;
},
/**
* Remove the http request listner
**/
stopInfectingTrafic: function () {
// unregister the listner , private method as key
chrome.webRequest.onBeforeRequest.removeListener(_webRequestListner);
_active = false;
},
isActive:function() {
return _active;
}
};
})();