Improve suggested candidate filters in element picker

Additionally, fix eslint warnings.
This commit is contained in:
Raymond Hill 2024-04-10 10:28:11 -04:00
parent 89fa666bc4
commit c0b56cffab
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 97 additions and 97 deletions

View File

@ -16,6 +16,7 @@ rules:
- Program > ExpressionStatement > CallExpression > ArrowFunctionExpression > BlockStatement - Program > ExpressionStatement > CallExpression > ArrowFunctionExpression > BlockStatement
- CallExpression > MemberExpression - CallExpression > MemberExpression
- ArrayExpression > * - ArrayExpression > *
- ObjectExpression > *
no-control-regex: off no-control-regex: off
no-empty: off no-empty: off
sort-imports: warn sort-imports: warn

View File

@ -21,14 +21,13 @@
/* global CodeMirror */ /* global CodeMirror */
'use strict';
import './codemirror/ubo-static-filtering.js'; import './codemirror/ubo-static-filtering.js';
import * as sfp from './static-filtering-parser.js';
import { dom } from './dom.js';
import { hostnameFromURI } from './uri-utils.js'; import { hostnameFromURI } from './uri-utils.js';
import punycode from '../lib/punycode.js'; import punycode from '../lib/punycode.js';
import * as sfp from './static-filtering-parser.js';
import { dom } from './dom.js';
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
@ -63,13 +62,10 @@ const reCosmeticAnchor = /^#(\$|\?|\$\?)?#/;
const docURL = new URL(vAPI.getURL('')); const docURL = new URL(vAPI.getURL(''));
let resultsetOpt;
let netFilterCandidates = [];
let cosmeticFilterCandidates = [];
let computedCandidateSlot = 0;
let computedCandidate = '';
const computedSpecificityCandidates = new Map(); const computedSpecificityCandidates = new Map();
let resultsetOpt;
let cosmeticFilterCandidates = [];
let computedCandidate = '';
let needBody = false; let needBody = false;
/******************************************************************************/ /******************************************************************************/
@ -183,7 +179,6 @@ const candidateFromFilterChoice = function(filterChoice) {
elem.classList.remove('active'); elem.classList.remove('active');
} }
computedCandidateSlot = slot;
computedCandidate = ''; computedCandidate = '';
if ( filter === undefined ) { return ''; } if ( filter === undefined ) { return ''; }
@ -726,7 +721,6 @@ const svgListening = (( ) => {
// current mode is narrow or broad. // current mode is narrow or broad.
const populateCandidates = function(candidates, selector) { const populateCandidates = function(candidates, selector) {
const root = dialog.querySelector(selector); const root = dialog.querySelector(selector);
const ul = root.querySelector('ul'); const ul = root.querySelector('ul');
while ( ul.firstChild !== null ) { while ( ul.firstChild !== null ) {
@ -751,8 +745,6 @@ const showDialog = function(details) {
const { netFilters, cosmeticFilters, filter } = details; const { netFilters, cosmeticFilters, filter } = details;
netFilterCandidates = netFilters;
needBody = needBody =
cosmeticFilters.length !== 0 && cosmeticFilters.length !== 0 &&
cosmeticFilters[cosmeticFilters.length - 1] === '##body'; cosmeticFilters[cosmeticFilters.length - 1] === '##body';
@ -869,31 +861,31 @@ const quitPicker = function() {
const onPickerMessage = function(msg) { const onPickerMessage = function(msg) {
switch ( msg.what ) { switch ( msg.what ) {
case 'candidatesOptimized': case 'candidatesOptimized':
onCandidatesOptimized(msg); onCandidatesOptimized(msg);
break; break;
case 'showDialog': case 'showDialog':
showDialog(msg); showDialog(msg);
break; break;
case 'resultsetDetails': { case 'resultsetDetails': {
resultsetOpt = msg.opt; resultsetOpt = msg.opt;
$id('resultsetCount').textContent = msg.count; $id('resultsetCount').textContent = msg.count;
if ( msg.count !== 0 ) { if ( msg.count !== 0 ) {
$id('create').removeAttribute('disabled'); $id('create').removeAttribute('disabled');
} else { } else {
$id('create').setAttribute('disabled', ''); $id('create').setAttribute('disabled', '');
}
break;
} }
case 'svgPaths': { break;
let { ocean, islands } = msg; }
ocean += islands; case 'svgPaths': {
svgOcean.setAttribute('d', ocean); let { ocean, islands } = msg;
svgIslands.setAttribute('d', islands || NoPaths); ocean += islands;
break; svgOcean.setAttribute('d', ocean);
} svgIslands.setAttribute('d', islands || NoPaths);
default: break;
break; }
default:
break;
} }
}; };

View File

@ -19,19 +19,12 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* global CSS */
'use strict';
/******************************************************************************/
/******************************************************************************/
(async ( ) => { (async ( ) => {
/******************************************************************************/ /******************************************************************************/
if ( typeof vAPI !== 'object' ) { return; } if ( typeof vAPI !== 'object' ) { return; }
if ( typeof vAPI === null ) { return; } if ( vAPI === null ) { return; }
if ( vAPI.pickerFrame ) { return; } if ( vAPI.pickerFrame ) { return; }
vAPI.pickerFrame = true; vAPI.pickerFrame = true;
@ -902,6 +895,20 @@ const onOptimizeCandidates = function(details) {
if ( r !== 0 ) { return r; } if ( r !== 0 ) { return r; }
return a.selector.length - b.selector.length; return a.selector.length - b.selector.length;
}); });
// If two candidates have the same count of matching elements, replace
// the less specific cosmetic filters with the more specific one if the
// less specific one has an id and is simpler
for ( let i = 0, n = results.length-1; i < n; i++ ) {
const a = results[i+0];
const b = results[i+1];
if ( b.count !== a.count ) { continue; }
if ( b.selector.length <= a.selector.length ) { continue; }
if ( a.selector.startsWith('#') === false ) { continue; }
if ( b.selector.length < a.selector.length ) { continue; }
b.selector = a.selector;
}
pickerFramePort.postMessage({ pickerFramePort.postMessage({
what: 'candidatesOptimized', what: 'candidatesOptimized',
candidates: results.map(a => a.selector), candidates: results.map(a => a.selector),
@ -1163,59 +1170,59 @@ vAPI.shutdown.add(quitPicker);
const onDialogMessage = function(msg) { const onDialogMessage = function(msg) {
switch ( msg.what ) { switch ( msg.what ) {
case 'start': case 'start':
startPicker(); startPicker();
if ( pickerFramePort === null ) { break; } if ( pickerFramePort === null ) { break; }
if ( targetElements.length === 0 ) { if ( targetElements.length === 0 ) {
highlightElements([], true); highlightElements([], true);
}
break;
case 'optimizeCandidates':
onOptimizeCandidates(msg);
break;
case 'dialogCreate':
filterToDOMInterface.queryAll(msg);
filterToDOMInterface.preview(true, true);
quitPicker();
break;
case 'dialogSetFilter': {
const resultset = filterToDOMInterface.queryAll(msg) || [];
highlightElements(resultset.map(a => a.elem), true);
if ( msg.filter === '!' ) { break; }
pickerFramePort.postMessage({
what: 'resultsetDetails',
count: resultset.length,
opt: resultset.length !== 0 ? resultset[0].opt : undefined,
});
break;
} }
case 'quitPicker': break;
filterToDOMInterface.preview(false); case 'optimizeCandidates':
onOptimizeCandidates(msg);
break;
case 'dialogCreate':
filterToDOMInterface.queryAll(msg);
filterToDOMInterface.preview(true, true);
quitPicker();
break;
case 'dialogSetFilter': {
const resultset = filterToDOMInterface.queryAll(msg) || [];
highlightElements(resultset.map(a => a.elem), true);
if ( msg.filter === '!' ) { break; }
pickerFramePort.postMessage({
what: 'resultsetDetails',
count: resultset.length,
opt: resultset.length !== 0 ? resultset[0].opt : undefined,
});
break;
}
case 'quitPicker':
filterToDOMInterface.preview(false);
quitPicker();
break;
case 'highlightElementAtPoint':
highlightElementAtPoint(msg.mx, msg.my);
break;
case 'unhighlight':
highlightElements([]);
break;
case 'filterElementAtPoint':
filterElementAtPoint(msg.mx, msg.my, msg.broad);
break;
case 'zapElementAtPoint':
zapElementAtPoint(msg.mx, msg.my, msg.options);
if ( msg.options.highlight !== true && msg.options.stay !== true ) {
quitPicker(); quitPicker();
break; }
case 'highlightElementAtPoint': break;
highlightElementAtPoint(msg.mx, msg.my); case 'togglePreview':
break; filterToDOMInterface.preview(msg.state);
case 'unhighlight': if ( msg.state === false ) {
highlightElements([]); highlightElements(targetElements, true);
break; }
case 'filterElementAtPoint': break;
filterElementAtPoint(msg.mx, msg.my, msg.broad); default:
break; break;
case 'zapElementAtPoint':
zapElementAtPoint(msg.mx, msg.my, msg.options);
if ( msg.options.highlight !== true && msg.options.stay !== true ) {
quitPicker();
}
break;
case 'togglePreview':
filterToDOMInterface.preview(msg.state);
if ( msg.state === false ) {
highlightElements(targetElements, true);
}
break;
default:
break;
} }
}; };