diff --git a/changedetectionio/static/js/browser-steps.js b/changedetectionio/static/js/browser-steps.js index c189ce74..e167b81d 100644 --- a/changedetectionio/static/js/browser-steps.js +++ b/changedetectionio/static/js/browser-steps.js @@ -114,11 +114,11 @@ $(document).ready(function () { e.preventDefault() }); + // When the mouse moves we know which element it should be above + // mousedown will link that to the UI (select the right action, highlight etc) $('#browsersteps-selector-canvas').bind('mousedown', function (e) { // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent e.preventDefault() - console.log(e); - console.log("current xpath in index is " + current_selected_i); last_click_xy = {'x': parseInt((1 / x_scale) * e.offsetX), 'y': parseInt((1 / y_scale) * e.offsetY)} process_selected(current_selected_i); current_selected_i = false; @@ -132,6 +132,7 @@ $(document).ready(function () { } }); + // Debounce and find the current most 'interesting' element we are hovering above $('#browsersteps-selector-canvas').bind('mousemove', function (e) { if (!xpath_data) { return; @@ -151,41 +152,38 @@ $(document).ready(function () { current_selected_i = false; // Reverse order - the most specific one should be deeper/"laster" // Basically, find the most 'deepest' - //$('#browsersteps-selector-canvas').css('cursor', 'pointer'); - for (var i = xpath_data['size_pos'].length; i !== 0; i--) { - // draw all of them? let them choose somehow? - var sel = xpath_data['size_pos'][i - 1]; + var possible_elements = []; + xpath_data['size_pos'].forEach(function (item, index) { // If we are in a bounding-box - if (e.offsetY > sel.top * y_scale && e.offsetY < sel.top * y_scale + sel.height * y_scale + if (e.offsetY > item.top * y_scale && e.offsetY < item.top * y_scale + item.height * y_scale && - e.offsetX > sel.left * y_scale && e.offsetX < sel.left * y_scale + sel.width * y_scale + e.offsetX > item.left * y_scale && e.offsetX < item.left * y_scale + item.width * y_scale ) { - // Only highlight these interesting types - if (1) { - ctx.strokeRect(sel.left * x_scale, sel.top * y_scale, sel.width * x_scale, sel.height * y_scale); - ctx.fillRect(sel.left * x_scale, sel.top * y_scale, sel.width * x_scale, sel.height * y_scale); - current_selected_i = i - 1; - break; - - // find the smallest one at this x,y - // does it mean sort the xpath list by size (w*h) i think so! - } else { - - if (include_text_elements[0].checked === true) { - // blue one with background instead? - ctx.fillStyle = 'rgba(0,0,255, 0.1)'; - ctx.strokeStyle = 'rgba(0,0,200, 0.7)'; - $('#browsersteps-selector-canvas').css('cursor', 'grab'); - ctx.strokeRect(sel.left * x_scale, sel.top * y_scale, sel.width * x_scale, sel.height * y_scale); - ctx.fillRect(sel.left * x_scale, sel.top * y_scale, sel.width * x_scale, sel.height * y_scale); - current_selected_i = i - 1; - break; - } - } + // There could be many elements here, record them all and then we'll find out which is the most 'useful' + // (input, textarea, button, A etc) + possible_elements.push(item); } + }); + + // Find the best one + if (possible_elements.length) { + possible_elements.forEach(function (item, index) { + if (["a", "input", "textarea", "button"].includes(item['tagName'])) { + current_selected_i = item; + } + }); + + if (!current_selected_i) { + current_selected_i = possible_elements[0]; + } + + sel = xpath_data['size_pos'][current_selected_i]; + ctx.strokeRect(current_selected_i.left * x_scale, current_selected_i.top * y_scale, current_selected_i.width * x_scale, current_selected_i.height * y_scale); + ctx.fillRect(current_selected_i.left * x_scale, current_selected_i.top * y_scale, current_selected_i.width * x_scale, current_selected_i.height * y_scale); } + }.debounce(10)); }); @@ -195,16 +193,16 @@ $(document).ready(function () { // callback for clicking on an xpath on the canvas - function process_selected(xpath_data_index) { + function process_selected(selected_in_xpath_list) { found_something = false; var first_available = $("ul#browser_steps li.empty").first(); - if (xpath_data_index !== false) { + if (selected_in_xpath_list !== false) { // Nothing focused, so fill in a new one // if inpt type button or