From a287e5a86cfce54fc627d81f69650bd829226b15 Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 19 Dec 2022 12:33:31 +0100 Subject: [PATCH] Visual Selector - Select smallest/most precise element first, better filtering of zero size elements --- changedetectionio/res/xpath_element_scraper.js | 12 +++++++++--- changedetectionio/static/js/visual-selector.js | 10 ++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/changedetectionio/res/xpath_element_scraper.js b/changedetectionio/res/xpath_element_scraper.js index 0bbc035d..effdd58b 100644 --- a/changedetectionio/res/xpath_element_scraper.js +++ b/changedetectionio/res/xpath_element_scraper.js @@ -1,3 +1,6 @@ +// Copyright (C) 2021 Leigh Morresi (dgtlmoon@gmail.com) +// All rights reserved. + // @file Scrape the page looking for elements of concern (%ELEMENTS%) // http://matatk.agrip.org.uk/tests/position-and-width/ // https://stackoverflow.com/questions/26813480/when-is-element-getboundingclientrect-guaranteed-to-be-updated-accurate @@ -89,8 +92,8 @@ for (var i = 0; i < elements.length; i++) { continue } - // Forget really small ones - if (bbox['width'] < 10 && bbox['height'] < 10) { + // Skip really small ones, and where width or height ==0 + if (bbox['width'] * bbox['height'] < 100) { continue; } @@ -146,7 +149,6 @@ for (var i = 0; i < elements.length; i++) { } - // Inject the current one set in the include_filters, which may be a CSS rule // used for displaying the current one in VisualSelector, where its not one we generated. if (include_filters.length) { @@ -205,5 +207,9 @@ if (include_filters.length) { } } +// Sort the elements so we find the smallest one first, in other words, we find the smallest one matching in that area +// so that we dont select the wrapping element by mistake and be unable to select what we want +size_pos.sort((a, b) => (a.width*a.height > b.width*b.height) ? 1 : -1) + // Window.width required for proper scaling in the frontend return {'size_pos': size_pos, 'browser_width': window.innerWidth}; diff --git a/changedetectionio/static/js/visual-selector.js b/changedetectionio/static/js/visual-selector.js index a61bb3c7..2416696e 100644 --- a/changedetectionio/static/js/visual-selector.js +++ b/changedetectionio/static/js/visual-selector.js @@ -1,4 +1,5 @@ -// Horrible proof of concept code :) +// Copyright (C) 2021 Leigh Morresi (dgtlmoon@gmail.com) +// All rights reserved. // yes - this is really a hack, if you are a front-ender and want to help, please get in touch! $(document).ready(function () { @@ -177,9 +178,10 @@ $(document).ready(function () { // Basically, find the most 'deepest' var found = 0; ctx.fillStyle = 'rgba(205,0,0,0.35)'; - for (var i = selector_data['size_pos'].length; i !== 0; i--) { + // Will be sorted by smallest width*height first + for (var i = 0; i <= selector_data['size_pos'].length; i++) { // draw all of them? let them choose somehow? - var sel = selector_data['size_pos'][i - 1]; + var sel = selector_data['size_pos'][i]; // If we are in a bounding-box if (e.offsetY > sel.top * y_scale && e.offsetY < sel.top * y_scale + sel.height * y_scale && @@ -195,7 +197,7 @@ $(document).ready(function () { // no need to keep digging // @todo or, O to go out/up, I to go in // or double click to go up/out the selector? - current_selected_i = i - 1; + current_selected_i = i; found += 1; break; }