From 917f3620e0c08b722bbd4d400bca2735d9f6975f Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 18 Sep 2019 12:17:45 -0400 Subject: [PATCH] Revisit element picker arguments code No need to store mouse coordinates in background page, thus no need to post mouse coordinates information for every click. Rename/group element picker arguments and popup arguments separately. --- src/js/background.js | 17 +++++++++-------- src/js/commands.js | 2 +- src/js/contentscript.js | 22 ++++++++++++---------- src/js/contextmenu.js | 1 + src/js/messaging.js | 24 ++++++++++-------------- src/js/scriptlets/element-picker.js | 8 ++++++-- src/js/tab.js | 4 ++-- src/js/ublock.js | 10 +++++----- 8 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index 47fa99ab7..dfe5a716c 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -174,16 +174,17 @@ const µBlock = (function() { // jshint ignore:line apiErrorCount: 0, - mouseEventRegister: { - tabId: '', - x: -1, - y: -1, - url: '' + maybeGoodPopup: { + tabId: 0, + url: '', }, - epickerTarget: '', - epickerZap: false, - epickerEprom: null, + epickerArgs: { + eprom: null, + mouse: false, + target: '', + zap: false, + }, scriptlets: {}, diff --git a/src/js/commands.js b/src/js/commands.js index 4a5c61521..e72036a4f 100644 --- a/src/js/commands.js +++ b/src/js/commands.js @@ -136,7 +136,7 @@ vAPI.commands.onCommand.addListener(async command => { case 'launch-element-zapper': { const tab = await vAPI.tabs.getCurrent(); if ( tab instanceof Object === false ) { return; } - µb.mouseEventRegister.x = µb.mouseEventRegister.y = -1; + µb.epickerArgs.mouse = false; µb.elementPickerExec( tab.id, undefined, diff --git a/src/js/contentscript.js b/src/js/contentscript.js index 8aa6294c5..f0246bce9 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -1417,23 +1417,25 @@ vAPI.bootstrap = (function() { return; } - // To send mouse coordinates to main process, as the chrome API fails - // to provide the mouse position to context menu listeners. - // https://github.com/chrisaljoudi/uBlock/issues/1143 - // Also, find a link under the mouse, to try to avoid confusing new tabs - // as nuisance popups. - // Ref.: https://developer.mozilla.org/en-US/docs/Web/Events/contextmenu + // To be used by element picker/zapper. + vAPI.mouseClick = { x: -1, y: -1 }; const onMouseClick = function(ev) { + if ( ev.isTrusted === false ) { return; } + vAPI.mouseClick.x = ev.clientX; + vAPI.mouseClick.y = ev.clientY; + + // https://github.com/chrisaljoudi/uBlock/issues/1143 + // Find a link under the mouse, to try to avoid confusing new tabs + // as nuisance popups. let elem = ev.target; while ( elem !== null && elem.localName !== 'a' ) { elem = elem.parentElement; } + if ( elem === null ) { return; } vAPI.messaging.send('contentscript', { - what: 'mouseClick', - x: ev.clientX, - y: ev.clientY, - url: elem !== null && ev.isTrusted !== false ? elem.href : '', + what: 'maybeGoodPopup', + url: elem.href || '', }); }; diff --git a/src/js/contextmenu.js b/src/js/contextmenu.js index 4c5ff4c9e..7ac5b2d4b 100644 --- a/src/js/contextmenu.js +++ b/src/js/contextmenu.js @@ -57,6 +57,7 @@ const onBlockElement = function(details, tab) { } } + µBlock.epickerArgs.mouse = true; µBlock.elementPickerExec(tab.id, tagName + '\t' + src); }; diff --git a/src/js/messaging.js b/src/js/messaging.js index 4eed67bbc..15e553b92 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -129,7 +129,7 @@ const onMessage = function(request, sender, callback) { case 'launchElementPicker': // Launched from some auxiliary pages, clear context menu coords. - µb.mouseEventRegister.x = µb.mouseEventRegister.y = -1; + µb.epickerArgs.mouse = false; µb.elementPickerExec(request.tabId, request.targetURL, request.zap); break; @@ -539,11 +539,9 @@ const onMessage = function(request, sender, callback) { } break; - case 'mouseClick': - µb.mouseEventRegister.tabId = tabId; - µb.mouseEventRegister.x = request.x; - µb.mouseEventRegister.y = request.y; - µb.mouseEventRegister.url = request.url; + case 'maybeGoodPopup': + µb.maybeGoodPopup.tabId = tabId; + µb.maybeGoodPopup.url = request.url; break; case 'shouldRenderNoscriptTags': @@ -669,15 +667,13 @@ const onMessage = function(request, sender, callback) { callback({ frameContent: this.responseText.replace(reStrings, replacer), - target: µb.epickerTarget, - clientX: µb.mouseEventRegister.x, - clientY: µb.mouseEventRegister.y, - zap: µb.epickerZap, - eprom: µb.epickerEprom + target: µb.epickerArgs.target, + mouse: µb.epickerArgs.mouse, + zap: µb.epickerArgs.zap, + eprom: µb.epickerArgs.eprom, }); - µb.epickerTarget = ''; - µb.mouseEventRegister.x = µb.mouseEventRegister.y = -1; + µb.epickerArgs.target = ''; }; xhr.send(); return; @@ -703,7 +699,7 @@ const onMessage = function(request, sender, callback) { break; case 'elementPickerEprom': - µb.epickerEprom = request; + µb.epickerArgs.eprom = request; break; default: diff --git a/src/js/scriptlets/element-picker.js b/src/js/scriptlets/element-picker.js index 1da2e8ab4..5f3e6ee84 100644 --- a/src/js/scriptlets/element-picker.js +++ b/src/js/scriptlets/element-picker.js @@ -1636,8 +1636,12 @@ const startPicker = function(details) { highlightElements([], true); // Try using mouse position - if ( details.clientX !== -1 ) { - if ( filtersFrom(details.clientX, details.clientY) !== 0 ) { + if ( + details.mouse && + typeof vAPI.mouseClick.x === 'number' && + vAPI.mouseClick.x > 0 + ) { + if ( filtersFrom(vAPI.mouseClick.x, vAPI.mouseClick.y) !== 0 ) { showDialog(); return; } diff --git a/src/js/tab.js b/src/js/tab.js index 62c5ad9f8..d0e64bcb3 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -437,8 +437,8 @@ housekeep itself. this.opener = { tabId: openerTabId, popunder: false, - trustedURL: openerTabId === µb.mouseEventRegister.tabId - ? µb.mouseEventRegister.url + trustedURL: openerTabId === µb.maybeGoodPopup.tabId + ? µb.maybeGoodPopup.url : '' }; this.selfDestructionTimer = null; diff --git a/src/js/ublock.js b/src/js/ublock.js index e031aaed0..6dd2a792c 100644 --- a/src/js/ublock.js +++ b/src/js/ublock.js @@ -414,22 +414,22 @@ const matchBucket = function(url, hostname, bucket, start) { /******************************************************************************/ -µBlock.elementPickerExec = async function(tabId, targetElement, zap) { +µBlock.elementPickerExec = async function(tabId, targetElement, zap = false) { if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; } - this.epickerTarget = targetElement || ''; - this.epickerZap = zap || false; + this.epickerArgs.target = targetElement || ''; + this.epickerArgs.zap = zap; // https://github.com/uBlockOrigin/uBlock-issues/issues/40 // The element picker needs this library vAPI.tabs.executeScript(tabId, { file: '/lib/diff/swatinem_diff.js', - runAt: 'document_end' + runAt: 'document_end', }); await vAPI.tabs.executeScript(tabId, { file: '/js/scriptlets/element-picker.js', - runAt: 'document_end' + runAt: 'document_end', }); // https://github.com/uBlockOrigin/uBlock-issues/issues/168