[removed] Let user select an HTML element like Firebug?

后端 未结 10 942
醉话见心
醉话见心 2020-11-30 17:55

I want to write a browser (Chrome/FF) extension that needs to select an element on a web page. I would like it to behave like Firebug\'s element inspector does. You click

10条回答
  •  失恋的感觉
    2020-11-30 18:50

    One simple way to do it is to use an outline instead of a border:

    .highlight { outline: 4px solid #07C; }
    

    Just add and remove that class to any element you want to select/deselect (code below is not properly tested):

    document.body.addEventListener("mouseover", function(e) {
        e.stopPropagation();
        e.target.addEventListener("mouseout", function (e) {
            e.target.className = e.target.className.replace(new RegExp(" highlight\\b", "g"), "");
        });
        e.target.className += " highlight";
    });
    

    Since you are using an outline, (which is supported by Chrome) instead of a border, elements will not jump around. I'm using something similar in my EasyReader Extension.

提交回复
热议问题