diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index cd59db4399582..a106ce26c9502 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -1088,18 +1088,14 @@ 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) { @@ -1107,13 +1103,14 @@ 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 }, { @@ -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); diff --git a/platform/safari/vapi-client.js b/platform/safari/vapi-client.js index d972852d3594d..4dd9b474602f1 100644 --- a/platform/safari/vapi-client.js +++ b/platform/safari/vapi-client.js @@ -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", @@ -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) { @@ -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(); } }; @@ -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) {\