diff --git a/src/js/vapi-background.js b/src/js/vapi-background.js index a4918859a..43445308a 100644 --- a/src/js/vapi-background.js +++ b/src/js/vapi-background.js @@ -718,6 +718,11 @@ if (window.chrome) { e.stopPropagation(); } + if (e.message.middleClickURL) { + vAPI.lastMiddleClick = e.message; + return; + } + // blocking unwanted pop-ups if (e.message.type === 'popup') { if (typeof vAPI.tabs.onPopup === 'function') { @@ -769,6 +774,20 @@ if (window.chrome) { 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, diff --git a/src/js/vapi-client.js b/src/js/vapi-client.js index e9bfd63d1..a58ed1419 100644 --- a/src/js/vapi-client.js +++ b/src/js/vapi-client.js @@ -187,6 +187,9 @@ if (window.chrome) { return; } + var beforeLoadEvent = document.createEvent('Event'); + beforeLoadEvent.initEvent('beforeload'); + var linkHelper = document.createElement('a'); var onBeforeLoad = function(e, details) { if (e.url && e.url.slice(0, 5) === 'data:') { @@ -274,8 +277,6 @@ if (window.chrome) { document.removeEventListener('DOMSubtreeModified', firstMutation, true); firstMutation = null; var randomEventName = parseInt(Math.random() * 1e15, 10).toString(36); - var beforeLoadEvent = document.createEvent('Event'); - beforeLoadEvent.initEvent('beforeload'); window.addEventListener(randomEventName, function(e) { var result = onBeforeLoad(beforeLoadEvent, e.detail); @@ -342,6 +343,21 @@ if (window.chrome) { }; window.addEventListener('contextmenu', onContextMenu, true); + + window.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); } })();