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

后端 未结 10 950
醉话见心
醉话见心 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:33

    What you need to do is to create 4 elements for the highlighting. They will form an empty square, and so your mouse events are free to fire. This is similar to this overlay example I've made.

    The difference is that you only need the four elements (no resize markers), and that the size and position of the 4 boxes are a bit different (to mimick the red border). Then you can use event.target in your event handler, because it gets the real topmost element by default.

    Another approach is to hide the exra element, get elementFromPoint, calculate then put it back.

    They're faster than light, I can tell you. Even Einstein would agree :)

    1.) elementFromPoint overlay/borders - [Demo1] FF needs v3.0+

    var box = $("
    ").css({ display: "none", position: "absolute", zIndex: 65000, background:"rgba(255, 0, 0, .3)" }).appendTo("body"); var mouseX, mouseY, target, lastTarget; // in case you need to support older browsers use a requestAnimationFrame polyfill // e.g: https://gist.github.com/paulirish/1579671 window.requestAnimationFrame(function frame() { window.requestAnimationFrame(frame); if (target && target.className === "outer") { box.hide(); target = document.elementFromPoint(mouseX, mouseY); } box.show(); if (target === lastTarget) return; lastTarget = target; var $target = $(target); var offset = $target.offset(); box.css({ width: $target.outerWidth() - 1, height: $target.outerHeight() - 1, left: offset.left, top: offset.top }); }); $("body").mousemove(function (e) { mouseX = e.clientX; mouseY = e.clientY; target = e.target; });

    2.) mouseover borders - [Demo2]

    var box = new Overlay();
    
    $("body").mouseover(function(e){
      var el = $(e.target);
      var offset = el.offset();
      box.render(el.outerWidth(), el.outerHeight(), offset.left, offset.top);
    });​
    
    /**
     * This object encapsulates the elements and actions of the overlay.
     */
    function Overlay(width, height, left, top) {
    
        this.width = this.height = this.left = this.top = 0;
    
        // outer parent
        var outer = $("
    ").appendTo("body"); // red lines (boxes) var topbox = $("
    ").css("height", 1).appendTo(outer); var bottombox = $("
    ").css("height", 1).appendTo(outer); var leftbox = $("
    ").css("width", 1).appendTo(outer); var rightbox = $("
    ").css("width", 1).appendTo(outer); // don't count it as a real element outer.mouseover(function(){ outer.hide(); }); /** * Public interface */ this.resize = function resize(width, height, left, top) { if (width != null) this.width = width; if (height != null) this.height = height; if (left != null) this.left = left; if (top != null) this.top = top; }; this.show = function show() { outer.show(); }; this.hide = function hide() { outer.hide(); }; this.render = function render(width, height, left, top) { this.resize(width, height, left, top); topbox.css({ top: this.top, left: this.left, width: this.width }); bottombox.css({ top: this.top + this.height - 1, left: this.left, width: this.width }); leftbox.css({ top: this.top, left: this.left, height: this.height }); rightbox.css({ top: this.top, left: this.left + this.width - 1, height: this.height }); this.show(); }; // initial rendering [optional] // this.render(width, height, left, top); }

提交回复
热议问题