Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

Commit

Permalink
"Rewrote detection"
Browse files Browse the repository at this point in the history
  • Loading branch information
AstraLuma committed Feb 28, 2012
1 parent 82938ab commit 6a9f022
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 52 deletions.
43 changes: 27 additions & 16 deletions background.html
Expand Up @@ -263,11 +263,20 @@
});
}

function beforeRequest(request) {
var requests = {};

function onBeforeRequest(request) {
if (!request) return {}; // on handlerBehaviorChanged()

var isDisabled = jsonStorage.get('is-disabled');
if (isDisabled) return {};
const rId = request.requestId;
if (!requests[rId]) {
requests[rId] = {};
} else if (requests[rId][request.url]) {
return {};
}
requests[rId][request.url] = true;

var po = new ProtoOrigin(request.url);
if (!po.hasSecure()) return {};
Expand All @@ -278,6 +287,10 @@
if (siteInList) {
if(!doNotRedirect.match(po)) {
var surl = po.makeSecure();
if (requests[rId][surl]) {
return {};
}
requests[rId][surl] = true;
console.log("Redirecting "+request.url+" to "+surl);
return {redirectUrl: surl}
} else {
Expand All @@ -291,28 +304,26 @@
}
return {};
}
chrome.webRequest.onBeforeRequest.addListener(beforeRequest, {urls: ['http://*/*'], types: ['main_frame']}, ["blocking"]);
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ['http://*/*'], types: ['main_frame']}, ["blocking"]);

function isSubDomain(sub, dom) {
if (sub == dom) return true;
if (dom != sub.substr(-dom.length)) return false;
return sub[dom.length] == '.';
function onBeforeRedirect(details) {
if (!details) return; // on handlerBehaviorChanged()
if (requests[details.requestId]) {
requests[details.requestId][details.url] = true;
requests[details.requestId][details.redirectUrl] = true;
}
}
chrome.webRequest.onBeforeRedirect.addListener(onBeforeRedirect, {urls: ['https://*/*']});

function beforeRedirect(details) {
function onCompleted(details) {
if (!details) return; // on handlerBehaviorChanged()
var frompo = new ProtoOrigin(details.url),
topo = new ProtoOrigin(details.redirectUrl);
if (isSubDomain(topo.o, frompo.o) && frompo.isSecure() && !topo.isSecure()) {
console.log("Blacklist: "+frompo.o+" ("+frompo.url+" -> "+topo.url+")");
API['blacklist.add']({site:frompo.o});
}
delete requests[details.requestId];
}
chrome.webRequest.onBeforeRedirect.addListener(beforeRedirect, {urls: ['https://*/*']});
chrome.webRequest.onCompleted.addListener(onBeforeRedirect, {urls: ['<all_urls>']});

/// Call this whenever our data changes
function redirectChanged() {
chrome.webRequest.handlerBehaviorChanged(beforeRequest);
// chrome.webRequest.handlerBehaviorChanged(beforeRedirect);
chrome.webRequest.handlerBehaviorChanged(onBeforeRequest);
}

function init() {
Expand Down
41 changes: 5 additions & 36 deletions utils.js
@@ -1,36 +1,5 @@
function addMethod(obj, func) {
if (!obj.__proto__[func.name]) {
obj.__proto__[func.name] = func;
}
}

addMethod(String, function startsWith(str) {
return !this.indexOf(str);
});

addMethod(String, function contains(it) {
return this.indexOf(it) != -1;
});

/*addMethod(Array, function uniquify() {
this.sort();
for (var i = 1; i < this.length; i++) {
if (this[i-1] == this[i]) {
delete this[i--];
}
}
return this;
});
addMethod(Array, function contains(item) {
return this.indexOf(item) != -1;
});
addMethod(Array, function removeByValue(arr, val) {
for(var i=0; i<arr.length; i++) {
if(arr[i] == val) {
arr.splice(i, 1);
break;
}
}
});*/
function isSubDomain(sub, dom) {
if (sub == dom) return true;
if (dom != sub.substr(-dom.length)) return false;
return sub[dom.length] == '.';
}

0 comments on commit 6a9f022

Please sign in to comment.