Sunday, August 13, 2017

Copy Dom path To Clipboard

// ==UserScript==
// @name         Copy Dom path To Clipboard
// @namespace    DomPathToClipboard
// @version      0.1
// @include      *
// @require      http://code.jquery.com/jquery-1.12.4.min.js
// ==/UserScript==

$('head').append('<style>::-moz-selection {color:#fff;background: #3b5998}::selection {color:#fff;background: #3b5998}</style>');
$('body').append('<div id="boot" style="background:rgba(0,0,0,0.5);position:fixed;top:0;width:100%;text-align:center;display: block;padding:5px 0;font-size:15px;display:none;z-index:9999"></div>');
$(this).click(function(e){
   var url = document.location;
      if (e.altKey){
         var path  = $(e.target).getPath();
         $('#boot').html('<input id="temp" style="background:unset;border:0;min-width:100px;padding:0 10px;text-align: center;color:#fff;width:'+((path.length+1)*7)+'px"/>').fadeIn(500);
         $('#temp').val(path).select();
         document.execCommand("copy");
         $("#boot").append('<span class="copied" style="position:fixed;top:30px;left:0;right:0;color:orange;font-width:bold">Copied!</span>').fadeIn();
         $(".copied").fadeOut(1000, function(){$(this).remove();});
         setTimeout(function(){
   $("#boot").fadeOut(1000);
   },5000);
    }
});
jQuery.fn.extend({
    getPath: function () {
        var path, node = this;
        while (node.length) {
            var realNode = node[0], name = realNode.localName;
            if (!name) break;
            name = name.toLowerCase();
            var parent = node.parent();
            var sameTagSiblings = parent.children(name);
            if (sameTagSiblings.length > 1) {
                allSiblings = parent.children();
                var index = allSiblings.index(realNode) + 1;
                if (index > 1) {
                    name += ':nth-child(' + index + ')';
                }
            }
            path = name + (path ? ' > ' + path : '');
            node = parent;
        }
        return path;
    }
});

No comments:

Post a Comment