From ba0b11aabe810c6d0d29b9daf9741ec758272385 Mon Sep 17 00:00:00 2001 From: Deathamns Date: Sun, 2 Nov 2014 19:02:00 +0100 Subject: [PATCH] Use different page navigation detection for Safari The beforeNavigate event wasn't reliable (sometimes didn't fire, sometimes fired unnecessary when opening a link with the middle click - which had a workaround previously, but that's also removed by this commit). When the event didn't fire, the bindTabToPageStats method didn't run, and the requests related to the tab weren't blocked. --- src/js/vapi-background.js | 41 +++------------------------------------ src/js/vapi-client.js | 27 ++++++++++++-------------- 2 files changed, 15 insertions(+), 53 deletions(-) diff --git a/src/js/vapi-background.js b/src/js/vapi-background.js index c56d806cb..aedf8f0eb 100644 --- a/src/js/vapi-background.js +++ b/src/js/vapi-background.js @@ -390,7 +390,8 @@ if (self.chrome) { if (typeof onNavigation === 'function') { this.onNavigation = function(e) { - // e.url is not present for local files or data URIs + // e.url is not present for local files or data URIs, + // or probably for those URLs which we don't have access to if (!e.target || !e.target.url) { return; } @@ -740,11 +741,6 @@ if (self.chrome) { return e.message; } - if (e.message.middleClickURL) { - vAPI.lastMiddleClick = e.message; - return; - } - // blocking unwanted pop-ups if (e.message.type === 'popup') { if (typeof vAPI.tabs.onPopup === 'function') { @@ -790,39 +786,8 @@ if (self.chrome) { return e.message; }; + safari.application.addEventListener('message', this.onBeforeRequest.callback, true); - - // 'main_frame' simulation, since this isn't available in beforeload - safari.application.addEventListener('beforeNavigate', function(e) { - // e.url is not present for local files or data URIs - if (e.url) { - // beforeNavigate fires twice when opening a link with - // middle click. Once with the same tab, second time with - // the new tab. This hack tries to ignore the first one. - if (vAPI.lastMiddleClick) { - if (e.target === safari.application.activeBrowserWindow.activeTab - && e.timeStamp - vAPI.lastMiddleClick.timeStamp <= 500 - && e.url === vAPI.lastMiddleClick.middleClickURL) { - vAPI.lastMiddleClick = null; - return; - } - - vAPI.lastMiddleClick = null; - } - - vAPI.net.onBeforeRequest.callback({ - name: 'canLoad', - target: e.target, - message: { - url: e.url, - type: 'main_frame', - frameId: 0, - parentFrameId: -1, - timeStamp: e.timeStamp - } - }) || e.preventDefault(); - } - }, true); } } }; diff --git a/src/js/vapi-client.js b/src/js/vapi-client.js index 135a95df6..071f813d8 100644 --- a/src/js/vapi-client.js +++ b/src/js/vapi-client.js @@ -256,7 +256,13 @@ if (self.chrome) { var response = safari.self.tab.canLoad(e, details); if (!response) { - e.preventDefault(); + if (details.type === 'main_frame') { + window.stop(); + throw Error; + } + else { + e.preventDefault(); + } return false; } // local mirroring, response is a data: URL here @@ -350,20 +356,11 @@ if (self.chrome) { self.addEventListener('contextmenu', onContextMenu, true); - self.addEventListener('mouseup', function(e) { - if (e.button !== 1) { - return; - } - - e = document.evaluate('ancestor-or-self::a[@href]', e.target, null, 9, null).singleNodeValue; - - if (e && /^https?:$/.test(e.protocol)) { - safari.self.tab.canLoad(beforeLoadEvent, { - middleClickURL: e.href, - timeStamp: Date.now() - }); - } - }, true); + // 'main_frame' simulation + onBeforeLoad(beforeLoadEvent, { + url: window.location.href, + type: 'main_frame' + }); } })();