symfony profiler toolbar enhancement

symfony debug bar enhancement

目前为 2020-04-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name symfony profiler toolbar enhancement
  3. // @namespace micoli.symfony.profiler
  4. // @version 0.1
  5. // @description symfony debug bar enhancement
  6. // @match http*://*/*
  7. // @author micoli
  8. // @match https://stackoverflow.com/questions/52040308/css-selector-anchor-text-of-href-contains
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. if (document.location.href.match(/_profiler/)) {
  16. document.getElementById('header').style.display='none';
  17. return;
  18. }
  19. if(!document.querySelector('.sf-toolbar')){
  20. return;
  21. }
  22.  
  23. let windowSrc = `
  24. <style>
  25. #sfProfilerWindow-title {
  26. font-size: 20px;
  27. left: 26px;
  28. top: 9px;
  29. position: fixed;
  30. font-weight: bold;
  31. color: white;
  32. }
  33. #sfProfilerWindow-title-container {
  34. top: 5px;
  35. height: 25px;
  36. left: 10px;
  37. right: 10px;
  38. border: 1px solid #A3A3A3;
  39. position: fixed;
  40. overflow: hidden;
  41. z-index: 99;
  42. background-color: #4F805D;
  43. display: none;
  44. }
  45. #sfProfilerWindow {
  46. top: 30px;
  47. bottom: 45px;
  48. left: 10px;
  49. right: 10px;
  50. border: 0px solid #A3A3A3;
  51. position: fixed;
  52. overflow: hidden;
  53. z-index: 99;
  54. display: none;
  55. }
  56. #sfProfilerWindow-close{
  57. width: 20px;
  58. cursor: pointer;
  59. float: right;
  60. font-size: 20px;
  61. margin-right: 7px;
  62. font-weight: bold;
  63. color: white;
  64. }
  65. #sfProfilerWindow-iframe {
  66. width: 100%;
  67. height: 100vh;
  68. border-width: thin;
  69. }
  70. </style>
  71. <div id="sfProfilerWindow-title-container">
  72. <span id="sfProfilerWindow-title">title</span>
  73. <span id="sfProfilerWindow-close">[X]</span>
  74. </div>
  75. <div id="sfProfilerWindow">
  76. <iframe
  77. id="sfProfilerWindow-iframe"
  78. src="http://dinghy:22080/_profiler/052e26?panel=request"
  79. scrolling="yes"
  80. ></iframe>
  81. </div>
  82. </div>
  83. `;
  84.  
  85.  
  86. const isWindowOpened = function () {
  87. return document.getElementById('sfProfilerWindow').style.display === 'block';
  88. };
  89.  
  90. const showWindow = function () {
  91. document.getElementById('sfProfilerWindow-title-container').style.display = 'block';
  92. document.getElementById('sfProfilerWindow').style.display = 'block';
  93. };
  94.  
  95. const hideWindow = function () {
  96. document.getElementById('sfProfilerWindow-title-container').style.display = 'none';
  97. document.getElementById('sfProfilerWindow').style.display = 'none';
  98. };
  99. const capitalize = function (s) {
  100. if (typeof s !== 'string') return ''
  101. return s.charAt(0).toUpperCase() + s.slice(1)
  102. };
  103. const openSymfonyWindow = function (event, button) {
  104. event.preventDefault();
  105. let link = button.querySelector('a');
  106. if (isWindowOpened() && link.href === document.getElementById('sfProfilerWindow-iframe').src) {
  107. hideWindow();
  108. return false;
  109. }
  110. showWindow();
  111. document.getElementById('sfProfilerWindow-iframe').src = link.href;
  112. const linkMatches = link.href.match(/panel=(.*)/);
  113. document.getElementById('sfProfilerWindow-title').innerHTML = capitalize(linkMatches[1]);
  114. return false;
  115. };
  116.  
  117. const setLinks = function () {
  118. //a[href*="/_profiler/"]:not(.rgx-add-profiler)
  119. document.querySelectorAll(
  120. '.sf-toolbarreset>.sf-toolbar-block:not(.rgx-add-profiler'
  121. ).forEach(function (button) {
  122. button.className = button.className + ' rgx-add-profiler';
  123. button.target = '_blank';
  124. button.onclick = function (event) {
  125. openSymfonyWindow(event, button);
  126. }
  127. });
  128. };
  129.  
  130. setLinks();
  131. setInterval(setLinks, 500);
  132.  
  133. document.body.insertAdjacentHTML('beforeEnd', windowSrc);
  134. document.getElementById('sfProfilerWindow-close').onclick = hideWindow;
  135. hideWindow();
  136. })();
  137.  
  138.