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.
This commit is contained in:
Raymond Hill 2019-09-18 12:17:45 -04:00
parent 146e2ff186
commit 917f3620e0
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
8 changed files with 46 additions and 42 deletions

View file

@ -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: {},

View file

@ -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,

View file

@ -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 || '',
});
};

View file

@ -57,6 +57,7 @@ const onBlockElement = function(details, tab) {
}
}
µBlock.epickerArgs.mouse = true;
µBlock.elementPickerExec(tab.id, tagName + '\t' + src);
};

View file

@ -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:

View file

@ -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;
}

View file

@ -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;

View file

@ -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