From 11bb0e963895c0c6f219979e6bb32e175f271731 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 19 Jan 2015 01:50:10 -0700 Subject: [PATCH 1/3] Massive overhaul/fixes to core Safari event capturing --- platform/safari/vapi-background.js | 52 ++++++++++++------------------ platform/safari/vapi-client.js | 42 +++++++----------------- 2 files changed, 33 insertions(+), 61 deletions(-) diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index 6769855fa..26b865923 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -190,21 +190,32 @@ vAPI.tabs = { vAPI.tabs.registerListeners = function() { var onNavigation = this.onNavigation; - this.onNavigation = function(e) { - // 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 ) { + safari.application.addEventListener('beforeNavigate', function(e) { + if ( !e.target || !e.target.url || e.target.url === 'about:blank' ) { return; } - + var url = e.target.url, tabId = vAPI.tabs.getTabId(e.target); + if ( vAPI.tabs.popupCandidate ) { + var details = { + url: url, + tabId: tabId, + sourceTabId: vAPI.tabs.popupCandidate + }; + vAPI.tabs.popupCandidate = false; + if ( vAPI.tabs.onPopup(details) ) { + e.preventDefault(); + if ( vAPI.tabs.stack[details.sourceTabId] ) { + vAPI.tabs.stack[details.sourceTabId].activate(); + } + return; + } + } onNavigation({ + url: url, frameId: 0, - tabId: vAPI.tabs.getTabId(e.target), - url: e.target.url + tabId: tabId }); - }; - - safari.application.addEventListener('navigate', this.onNavigation, true); + }, true); // onClosed handled in the main tab-close event // onUpdated handled via monitoring the history.pushState on web-pages @@ -577,27 +588,6 @@ vAPI.messaging.broadcast = function(message) { /******************************************************************************/ -safari.application.addEventListener('beforeNavigate', function(e) { - if ( !vAPI.tabs.popupCandidate || e.url === 'about:blank' ) { - return; - } - - var details = { - url: e.url, - tabId: vAPI.tabs.getTabId(e.target), - sourceTabId: vAPI.tabs.popupCandidate - }; - - vAPI.tabs.popupCandidate = null; - - if ( vAPI.tabs.onPopup(details) ) { - e.preventDefault(); - - if ( vAPI.tabs.stack[details.sourceTabId] ) { - vAPI.tabs.stack[details.sourceTabId].activate(); - } - } -}, true); /******************************************************************************/ diff --git a/platform/safari/vapi-client.js b/platform/safari/vapi-client.js index 9adde80fd..958e8dc64 100644 --- a/platform/safari/vapi-client.js +++ b/platform/safari/vapi-client.js @@ -191,47 +191,31 @@ if ( location.protocol === 'safari-extension:' ) { /******************************************************************************/ -window.MutationObserver = window.MutationObserver || window.WebKitMutationObserver; - -if ( !window.MutationObserver ) { - // Dummy, minimalistic shim for older versions (<6) - // only supports node insertions, but currently we don't use it for anything else - window.MutationObserver = function(handler) { - this.observe = function(target) { - target.addEventListener('DOMNodeInserted', function(e) { - handler([{ addedNodes: [e.target] }]); - }, true); - }; - }; -} - -/******************************************************************************/ - var beforeLoadEvent = document.createEvent('Event'); beforeLoadEvent.initEvent('beforeload'); /******************************************************************************/ var frameId = window === window.top ? 0 : Date.now() % 1E5; +var parentFrameId = frameId ? 0 : -1; var linkHelper = document.createElement('a'); var onBeforeLoad = function(e, details) { - if ( e.url && e.url.slice(0, 5) === 'data:' ) { + if ( e.url && e.url.lastIndexOf('data:', 0) == 0 ) { return; } linkHelper.href = details ? details.url : e.url; + var url = linkHelper.href; - if ( linkHelper.protocol !== 'http:' && linkHelper.protocol !== 'https:' ) { - if ( !(details && details.type === 'popup') ) { - return; - } + if ( url.lastIndexOf('http:', 0) === -1 && url.lastIndexOf('https:', 0) === -1) { + return; } if ( details ) { - details.url = linkHelper.href; + details.url = url; } else { details = { - url: linkHelper.href + url: url }; switch ( e.target.nodeName.toLowerCase() ) { @@ -274,7 +258,7 @@ var onBeforeLoad = function(e, details) { // tabId is determined in the background script // details.tabId = null; details.frameId = frameId; - details.parentFrameId = frameId ? 0 : -1; + details.parentFrameId = parentFrameId; details.timeStamp = Date.now(); var response = safari.self.tab.canLoad(e, details); @@ -316,11 +300,10 @@ var onBeforeLoad = function(e, details) { document.addEventListener('beforeload', onBeforeLoad, true); /******************************************************************************/ - // block pop-ups, intercept xhr requests, and apply site patches var firstMutation = function() { document.removeEventListener('DOMSubtreeModified', firstMutation, true); - firstMutation = null; + firstMutation = false; var randEventName = uniqueId(); @@ -337,11 +320,10 @@ var firstMutation = function() { var tmpJS = document.createElement('script'); var tmpScript = ['(function() {', 'var block = function(u, t) {', - 'var e = document.createEvent("CustomEvent"),', - 'd = {url: u, type: t};', - 'e.initCustomEvent("' + randEventName + '", false, false, d);', + 'var e = new CustomEvent("' + randEventName +'",', + '{detail: {url: u, type: t}, bubbles: false});', 'dispatchEvent(e);', - 'return d.url === false;', + 'return e.detail.url === false;', '}, wo = open, xo = XMLHttpRequest.prototype.open;', 'open = function(u) {', 'return block(u, "popup") ? null : wo.apply(this, arguments);', From 15488d7396d646f159c7305f8dbbd96427ce8746 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 19 Jan 2015 01:55:24 -0700 Subject: [PATCH 2/3] Update extension info with update manifest and info to match signing cert --- platform/safari/Info.plist | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/platform/safari/Info.plist b/platform/safari/Info.plist index 938976a51..62498d5ef 100644 --- a/platform/safari/Info.plist +++ b/platform/safari/Info.plist @@ -3,7 +3,7 @@ Author - {author} + Chris Aljoudi (core by gorhill) Builder Version 534.57.2 CFBundleDisplayName @@ -83,8 +83,8 @@ All - + Update Manifest URL + https://chrismatic.io/ublock/Update.plist Website https://github.com/gorhill/uBlock From a65b63ca85f58270fb3a65dca4f27e2a4e28c434 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 19 Jan 2015 10:47:37 -0700 Subject: [PATCH 3/3] Fix equality (change to strict) --- platform/safari/vapi-client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/safari/vapi-client.js b/platform/safari/vapi-client.js index 958e8dc64..46b9d385b 100644 --- a/platform/safari/vapi-client.js +++ b/platform/safari/vapi-client.js @@ -200,7 +200,7 @@ var frameId = window === window.top ? 0 : Date.now() % 1E5; var parentFrameId = frameId ? 0 : -1; var linkHelper = document.createElement('a'); var onBeforeLoad = function(e, details) { - if ( e.url && e.url.lastIndexOf('data:', 0) == 0 ) { + if ( e.url && e.url.lastIndexOf('data:', 0) === 0 ) { return; }