mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-11 01:28:00 +01:00
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:
parent
146e2ff186
commit
917f3620e0
8 changed files with 46 additions and 42 deletions
|
@ -174,16 +174,17 @@ const µBlock = (function() { // jshint ignore:line
|
||||||
|
|
||||||
apiErrorCount: 0,
|
apiErrorCount: 0,
|
||||||
|
|
||||||
mouseEventRegister: {
|
maybeGoodPopup: {
|
||||||
tabId: '',
|
tabId: 0,
|
||||||
x: -1,
|
url: '',
|
||||||
y: -1,
|
|
||||||
url: ''
|
|
||||||
},
|
},
|
||||||
|
|
||||||
epickerTarget: '',
|
epickerArgs: {
|
||||||
epickerZap: false,
|
eprom: null,
|
||||||
epickerEprom: null,
|
mouse: false,
|
||||||
|
target: '',
|
||||||
|
zap: false,
|
||||||
|
},
|
||||||
|
|
||||||
scriptlets: {},
|
scriptlets: {},
|
||||||
|
|
||||||
|
|
|
@ -136,7 +136,7 @@ vAPI.commands.onCommand.addListener(async command => {
|
||||||
case 'launch-element-zapper': {
|
case 'launch-element-zapper': {
|
||||||
const tab = await vAPI.tabs.getCurrent();
|
const tab = await vAPI.tabs.getCurrent();
|
||||||
if ( tab instanceof Object === false ) { return; }
|
if ( tab instanceof Object === false ) { return; }
|
||||||
µb.mouseEventRegister.x = µb.mouseEventRegister.y = -1;
|
µb.epickerArgs.mouse = false;
|
||||||
µb.elementPickerExec(
|
µb.elementPickerExec(
|
||||||
tab.id,
|
tab.id,
|
||||||
undefined,
|
undefined,
|
||||||
|
|
|
@ -1417,23 +1417,25 @@ vAPI.bootstrap = (function() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// To send mouse coordinates to main process, as the chrome API fails
|
// To be used by element picker/zapper.
|
||||||
// to provide the mouse position to context menu listeners.
|
vAPI.mouseClick = { x: -1, y: -1 };
|
||||||
// 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
|
|
||||||
|
|
||||||
const onMouseClick = function(ev) {
|
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;
|
let elem = ev.target;
|
||||||
while ( elem !== null && elem.localName !== 'a' ) {
|
while ( elem !== null && elem.localName !== 'a' ) {
|
||||||
elem = elem.parentElement;
|
elem = elem.parentElement;
|
||||||
}
|
}
|
||||||
|
if ( elem === null ) { return; }
|
||||||
vAPI.messaging.send('contentscript', {
|
vAPI.messaging.send('contentscript', {
|
||||||
what: 'mouseClick',
|
what: 'maybeGoodPopup',
|
||||||
x: ev.clientX,
|
url: elem.href || '',
|
||||||
y: ev.clientY,
|
|
||||||
url: elem !== null && ev.isTrusted !== false ? elem.href : '',
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,7 @@ const onBlockElement = function(details, tab) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
µBlock.epickerArgs.mouse = true;
|
||||||
µBlock.elementPickerExec(tab.id, tagName + '\t' + src);
|
µBlock.elementPickerExec(tab.id, tagName + '\t' + src);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -129,7 +129,7 @@ const onMessage = function(request, sender, callback) {
|
||||||
|
|
||||||
case 'launchElementPicker':
|
case 'launchElementPicker':
|
||||||
// Launched from some auxiliary pages, clear context menu coords.
|
// 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);
|
µb.elementPickerExec(request.tabId, request.targetURL, request.zap);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -539,11 +539,9 @@ const onMessage = function(request, sender, callback) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'mouseClick':
|
case 'maybeGoodPopup':
|
||||||
µb.mouseEventRegister.tabId = tabId;
|
µb.maybeGoodPopup.tabId = tabId;
|
||||||
µb.mouseEventRegister.x = request.x;
|
µb.maybeGoodPopup.url = request.url;
|
||||||
µb.mouseEventRegister.y = request.y;
|
|
||||||
µb.mouseEventRegister.url = request.url;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'shouldRenderNoscriptTags':
|
case 'shouldRenderNoscriptTags':
|
||||||
|
@ -669,15 +667,13 @@ const onMessage = function(request, sender, callback) {
|
||||||
|
|
||||||
callback({
|
callback({
|
||||||
frameContent: this.responseText.replace(reStrings, replacer),
|
frameContent: this.responseText.replace(reStrings, replacer),
|
||||||
target: µb.epickerTarget,
|
target: µb.epickerArgs.target,
|
||||||
clientX: µb.mouseEventRegister.x,
|
mouse: µb.epickerArgs.mouse,
|
||||||
clientY: µb.mouseEventRegister.y,
|
zap: µb.epickerArgs.zap,
|
||||||
zap: µb.epickerZap,
|
eprom: µb.epickerArgs.eprom,
|
||||||
eprom: µb.epickerEprom
|
|
||||||
});
|
});
|
||||||
|
|
||||||
µb.epickerTarget = '';
|
µb.epickerArgs.target = '';
|
||||||
µb.mouseEventRegister.x = µb.mouseEventRegister.y = -1;
|
|
||||||
};
|
};
|
||||||
xhr.send();
|
xhr.send();
|
||||||
return;
|
return;
|
||||||
|
@ -703,7 +699,7 @@ const onMessage = function(request, sender, callback) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'elementPickerEprom':
|
case 'elementPickerEprom':
|
||||||
µb.epickerEprom = request;
|
µb.epickerArgs.eprom = request;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -1636,8 +1636,12 @@ const startPicker = function(details) {
|
||||||
highlightElements([], true);
|
highlightElements([], true);
|
||||||
|
|
||||||
// Try using mouse position
|
// Try using mouse position
|
||||||
if ( details.clientX !== -1 ) {
|
if (
|
||||||
if ( filtersFrom(details.clientX, details.clientY) !== 0 ) {
|
details.mouse &&
|
||||||
|
typeof vAPI.mouseClick.x === 'number' &&
|
||||||
|
vAPI.mouseClick.x > 0
|
||||||
|
) {
|
||||||
|
if ( filtersFrom(vAPI.mouseClick.x, vAPI.mouseClick.y) !== 0 ) {
|
||||||
showDialog();
|
showDialog();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -437,8 +437,8 @@ housekeep itself.
|
||||||
this.opener = {
|
this.opener = {
|
||||||
tabId: openerTabId,
|
tabId: openerTabId,
|
||||||
popunder: false,
|
popunder: false,
|
||||||
trustedURL: openerTabId === µb.mouseEventRegister.tabId
|
trustedURL: openerTabId === µb.maybeGoodPopup.tabId
|
||||||
? µb.mouseEventRegister.url
|
? µb.maybeGoodPopup.url
|
||||||
: ''
|
: ''
|
||||||
};
|
};
|
||||||
this.selfDestructionTimer = null;
|
this.selfDestructionTimer = null;
|
||||||
|
|
|
@ -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; }
|
if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
|
||||||
|
|
||||||
this.epickerTarget = targetElement || '';
|
this.epickerArgs.target = targetElement || '';
|
||||||
this.epickerZap = zap || false;
|
this.epickerArgs.zap = zap;
|
||||||
|
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/40
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/40
|
||||||
// The element picker needs this library
|
// The element picker needs this library
|
||||||
vAPI.tabs.executeScript(tabId, {
|
vAPI.tabs.executeScript(tabId, {
|
||||||
file: '/lib/diff/swatinem_diff.js',
|
file: '/lib/diff/swatinem_diff.js',
|
||||||
runAt: 'document_end'
|
runAt: 'document_end',
|
||||||
});
|
});
|
||||||
|
|
||||||
await vAPI.tabs.executeScript(tabId, {
|
await vAPI.tabs.executeScript(tabId, {
|
||||||
file: '/js/scriptlets/element-picker.js',
|
file: '/js/scriptlets/element-picker.js',
|
||||||
runAt: 'document_end'
|
runAt: 'document_end',
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/168
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/168
|
||||||
|
|
Loading…
Reference in a new issue