Skip to content

Commit

Permalink
Support redirect filters (fixes gorhill#19, gorhill#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
el1t committed Jan 17, 2017
1 parent 490af8b commit 0717227
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
23 changes: 8 additions & 15 deletions platform/safari/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -1088,32 +1088,29 @@
vAPI.tabs.onNavigation({
url: e.message.url,
frameId: 0,
tabId: vAPI.tabs.getTabId(e.target)
tabId: vAPI.tabs.getTabId(e.target).toString()
});
e.message.hostname = µb.URI.hostnameFromURI(e.message.url);
e.message.tabId = vAPI.tabs.getTabId(e.target);
e.message.responseHeaders = [];
onBeforeRequestClient(e.message);
var blockVerdict = onHeadersReceivedClient(e.message);
if(blockVerdict && blockVerdict.responseHeaders) {
e.message = false;
} else {
e.message = true;
}
e.message.shouldBlock = blockVerdict && blockVerdict.responseHeaders;
return;
}
switch(e.message.type) {
case "popup":
var openerTabId = vAPI.tabs.getTabId(e.target).toString();
var shouldBlock = !!vAPI.tabs.onPopupUpdated("preempt", openerTabId, e.message.url);
if (shouldBlock) {
e.message = false;
e.message.shouldBlock = true;
} else {
vAPI.tabs.popupCandidate = openerTabId;
e.message = true;
e.message.shouldBlock = false;
}
break;
case "popstate":
// No return value/message
vAPI.tabs.onUpdated(vAPI.tabs.getTabId(e.target), {
url: e.message.url
}, {
Expand All @@ -1124,13 +1121,9 @@
e.message.hostname = µb.URI.hostnameFromURI(e.message.url);
e.message.tabId = vAPI.tabs.getTabId(e.target);
var blockVerdict = onBeforeRequestClient(e.message);
if(blockVerdict && blockVerdict.cancel) {
e.message = false;
return;
} else {
e.message = true;
return;
}
e.message.response = blockVerdict;
e.message.shouldBlock = blockVerdict && (blockVerdict.cancel === true || blockVerdict.redirectUrl !== undefined);
return;
}
};
safari.application.addEventListener("message", onBeforeRequestAdapter, true);
Expand Down
30 changes: 24 additions & 6 deletions platform/safari/vapi-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,10 @@
// Inform that we've navigated
var shouldBlockScript = false;
if(frameId === 0) {
shouldBlockScript = !safari.self.tab.canLoad(beforeLoadEvent, {
shouldBlockScript = safari.self.tab.canLoad(beforeLoadEvent, {
url: location.href,
type: "main_frame"
});
}).shouldBlock;
}
var nodeTypes = {
"frame": "sub_frame",
Expand All @@ -527,7 +527,10 @@
details.frameId = frameId;
details.parentFrameId = parentFrameId;
details.timeStamp = Date.now();
return !(safari.self.tab.canLoad(beforeLoadEvent, details));
return safari.self.tab.canLoad(beforeLoadEvent, details).shouldBlock;
};
var redirectSrc = function(element, url) {
element.src = url;
};
var onBeforeLoad = function(e) {
if(firstMutation !== false) {
Expand All @@ -537,16 +540,30 @@
if(linkHelper.protocol.charCodeAt(0) !== 104) { // h = 104
return;
}
var type = nodeTypes[e.target.nodeName.toLowerCase()] || "other";
var details = {
url: linkHelper.href,
type: nodeTypes[e.target.nodeName.toLowerCase()] || "other",
type: type,
// tabId is determined in the background script
frameId: frameId,
parentFrameId: parentFrameId,
timeStamp: Date.now()
};
var response = safari.self.tab.canLoad(e, details);
if(response === false) {
// sometimes response = details, so check for that
if (response.shouldBlock) {
response = response.response.redirectUrl;
if (response) {
switch (type) {
case 'style':
e.target.href = response;
break;
case 'script':
default:
setTimeout(redirectSrc.bind(undefined, e.target, response), 1);
break;
}
}
e.preventDefault();
}
};
Expand Down Expand Up @@ -599,7 +616,8 @@ set: function(val) {\
x.setAttribute('src', block(val, 'image') ? 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=' : val);\
}\
});\
}\catch(e){}\
}\
catch(e){}\
return x;\
};\
open = function(u) {\
Expand Down

0 comments on commit 0717227

Please sign in to comment.