1. (function SpyOn() {
    2. const _id = 'spyon-container',
    3. _posBuffer = 3;
    4. function init() {
    5. document.body.addEventListener('mousemove', glide);
    6. document.body.addEventListener('mouseover', show);
    7. document.body.addEventListener('mouseleave', hide);
    8. }
    9. function hide(e) {
    10. document.getElementById(_id).style.display = 'none';
    11. }
    12. function show(e) {
    13. const spyContainer = document.getElementById(_id);
    14. if (!spyContainer) {
    15. create();
    16. return;
    17. }
    18. if (spyContainer.style.display !== 'block') {
    19. spyContainer.style.display = 'block';
    20. }
    21. }
    22. function glide(e) {
    23. const spyContainer = document.getElementById(_id);
    24. if (!spyContainer) {
    25. create();
    26. return;
    27. }
    28. const left = e.clientX + getScrollPos().left + _posBuffer;
    29. const top = e.clientY + getScrollPos().top + _posBuffer;
    30. spyContainer.innerHTML = showAttributes(e.target);
    31. if (left + spyContainer.offsetWidth > window.innerWidth) {
    32. spyContainer.style.left = left - spyContainer.offsetWidth + 'px';
    33. } else {
    34. spyContainer.style.left = left + 'px';
    35. }
    36. spyContainer.style.top = top + 'px';
    37. }
    38. function getScrollPos() {
    39. const ieEdge = document.all ? false : true;
    40. if (!ieEdge) {
    41. return {
    42. left : document.body.scrollLeft,
    43. top : document.body.scrollTop
    44. };
    45. } else {
    46. return {
    47. left : document.documentElement.scrollLeft,
    48. top : document.documentElement.scrollTop
    49. };
    50. }
    51. }
    52. function showAttributes(el) {
    53. const nodeName = `<span style="font-weight:bold;">${el.nodeName.toLowerCase()}</span><br/>`;
    54. const attrArr = Array.from(el.attributes);
    55. const attributes = attrArr.reduce((attrs, attr) => {
    56. attrs += `<span style="color:#ffffcc;">${attr.nodeName}</span>="${attr.nodeValue}"<br/>`;
    57. return attrs;
    58. }, '');
    59. return nodeName + attributes;
    60. }
    61. function create() {
    62. const div = document.createElement('div');
    63. div.id = _id;
    64. div.setAttribute('style', `
    65. position: absolute;
    66. left: 0;
    67. top: 0;
    68. width: auto;
    69. height: auto;
    70. padding: 10px;
    71. box-sizing: border-box;
    72. color: #fff;
    73. background-color: #444;
    74. z-index: 100000;
    75. font-size: 12px;
    76. border-radius: 5px;
    77. line-height: 20px;
    78. max-width: 45%;
    79. `
    80. );
    81. document.body.appendChild(div);
    82. }
    83. init();
    84. })();