17

We have a bookmarklet, and the user clicks a button and an inspect like highligther needs to kick in. We want to this to be cross browser.

For this we need to detect the DOM element during the mouse move, and once we have this element we need to highlight with CSS.

We have problems detecting the DOM element by mouse move, can you guide us how this is done?

Once we have this DOM element, on user click we need to extract XPath.

Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100

2 Answers2

26

You can hook mousemove on document or document.body, then use the target property of the event object to find out the topmost element the mouse is over. Then applying CSS to it is probably most easily done by adding a class to it.

But I wonder if the :hover psuedo-class might save you some trouble...

If not using :hover, here's an example:

(function() {
  var prev;

  if (document.body.addEventListener) {
    document.body.addEventListener('mouseover', handler, false);
  }
  else if (document.body.attachEvent) {
    document.body.attachEvent('mouseover', function(e) {
      return handler(e || window.event);
    });
  }
  else {
    document.body.onmouseover = handler;
  }

  function handler(event) {
    if (event.target === document.body ||
        (prev && prev === event.target)) {
      return;
    }
    if (prev) {
      prev.className = prev.className.replace(/\bhighlight\b/, '');
      prev = undefined;
    }
    if (event.target) {
      prev = event.target;
      prev.className += " highlight";
    }
  }

})();

Live copy | source

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • This :hover class does work for any HTML dom element? And does work in every browser? Do you know if it's supported by older version like FF 3.5 and IE7? – Ionut Flavius Pogacian Jun 13 '12 at 08:11
  • @IonutFlaviusPogacian: `:hover` does indeed work with any element in IE7 or higher (not in IE6, which only supported it on anchors). But I'd have to think carefully about how to structure the CSS. Just `body *:hover { ... }` probably wouldn't do it, if you want to highlight only the *topmost* element the mouse is over... – T.J. Crowder Jun 13 '12 at 08:16
  • 4
    Here's a jsfiddle that could show something similar http://jsfiddle.net/MBujm/ Ignore the jQuery, it's not needed. – Esailija Jun 13 '12 at 08:17
  • @Esailija and T.J.Crowder you please check out this related question [Mouseover event doesn't granulate on IE9 for sub elements, event doesn't start on IE8](http://stackoverflow.com/questions/11052988/mouseover-event-doesnt-granulate-on-ie9-for-sub-elements-event-doesnt-start-o) – Pentium10 Jun 15 '12 at 15:03
  • It's really helpful but i need iframe inside does not work..Kindly please anyone help. – SekDinesh Nov 13 '17 at 07:58
6

With the help of jquery you can do something like this

$('*').hover(
    function(e){
        $(this).css('border', '1px solid black');
        e.preventDefault();
        e.stopPropagation();
        return false;
    },function(e){
        $(this).css('border', 'none');
        e.preventDefault();
        e.stopPropagation();
        return false;
    }
);

With this code in your bookmarklet, you can load what ever code

javascript:function loadScript(src){f=document.createElement('script');if(f){f.setAttribute("type","text/javascript");f.setAttribute("src",src);document.getElementsByTagName("head")[0].appendChild(f);}};loadScript("yourscripturl");
James
  • 13,571
  • 6
  • 61
  • 83
  • 1
    Do you happen to know how this is done with plain javascript? It's a bit problematic to load JQuery in a bookmarklet. – Ionut Flavius Pogacian Jun 13 '12 at 08:12
  • I just edit my answer, you can use a bookmarklet code to load a biget code which can contain jquery and do whatever you want – James Jun 13 '12 at 08:19
  • change `yourscripturl` at the end – James Jun 13 '12 at 08:19
  • Wow! This is awesome as you can now do so much more with bookmarklets! However, I would also suspect that it's also kinda dangerous as it would open more security holes, wouldn't it? – Adrian Nov 10 '21 at 21:51
  • @Adrian unless you intentionally load bad script yourself or someone hijack your url and add their bad script there then yes it a security bug – James Nov 10 '21 at 22:24
  • 1
    @James : Small addition . Better you keep if any existing border to a variable and set it instead of setting the border to 'none'. Otherwise it will keep removing existing borders of elements :-) – Tharaka Deshan Dec 02 '22 at 12:19