MyConsole

记录所有 window._console.log 即使控制台被清除也能查看

  1. // ==UserScript==
  2. // @name MyConsole
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 记录所有 window._console.log 即使控制台被清除也能查看
  6. // @author Your Name
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var renderLog = undefined;
  16. var clearLog = undefined;
  17.  
  18. window._console = {};
  19.  
  20. window._console.log = function(...args) {
  21. if(!renderLog) return;
  22. renderLog({
  23. type: 'log',
  24. args: args,
  25. timestamp: new Date()
  26. });
  27. };
  28.  
  29. window._console.warn = function(...args) {
  30. if(!renderLog) return;
  31. renderLog({
  32. type: 'warn',
  33. args: args,
  34. timestamp: new Date()
  35. });
  36. };
  37.  
  38. window._console.error = function(...args) {
  39. if(!renderLog) return;
  40. renderLog({
  41. type: 'error',
  42. args: args,
  43. timestamp: new Date()
  44. });
  45. };
  46.  
  47. window._console.clear = function() {
  48. if(!clearLog) return;
  49. clearLog();
  50. };
  51.  
  52. // 创建一个界面按钮来查看存储的日志
  53. const createLogViewer = () => {
  54. // 创建按钮
  55. const button = document.createElement('button');
  56. button.innerText = '查看日志';
  57. button.style.position = 'fixed';
  58. button.style.bottom = '20px';
  59. button.style.right = '20px';
  60. button.style.padding = '10px 20px';
  61. button.style.zIndex = 10000;
  62. button.style.backgroundColor = '#4CAF50';
  63. button.style.color = 'white';
  64. button.style.border = 'none';
  65. button.style.borderRadius = '5px';
  66. button.style.cursor = 'pointer';
  67. button.style.boxShadow = '0 4px 6px rgba(0, 0, 0, 0.1)';
  68. button.title = '点击查看的 window._console.log 日志';
  69.  
  70. // 创建日志查看窗口
  71. const logWindow = document.createElement('div');
  72. logWindow.style.position = 'fixed';
  73. logWindow.style.top = '50px';
  74. logWindow.style.right = '20px';
  75. logWindow.style.width = '400px';
  76. logWindow.style.height = '300px';
  77. logWindow.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
  78. logWindow.style.color = 'white';
  79. logWindow.style.padding = '10px';
  80. logWindow.style.borderRadius = '5px';
  81. logWindow.style.display = 'none';
  82. logWindow.style.zIndex = 10000;
  83. logWindow.style.fontFamily = 'monospace';
  84. logWindow.style.fontSize = '12px';
  85.  
  86. // 创建关闭按钮
  87. const closeButton = document.createElement('button');
  88. closeButton.innerText = '关闭';
  89. closeButton.style.position = 'absolute';
  90. closeButton.style.top = '10px';
  91. closeButton.style.right = '10px';
  92. closeButton.style.padding = '5px 10px';
  93. closeButton.style.backgroundColor = '#f44336';
  94. closeButton.style.color = 'white';
  95. closeButton.style.border = 'none';
  96. closeButton.style.borderRadius = '3px';
  97. closeButton.style.cursor = 'pointer';
  98.  
  99. // 创建清空按钮
  100. const clearButton = document.createElement('button');
  101. clearButton.innerText = '清空';
  102. clearButton.style.position = 'absolute';
  103. clearButton.style.top = '10px';
  104. clearButton.style.right = '60px';
  105. clearButton.style.padding = '5px 10px';
  106. clearButton.style.backgroundColor = '#4CAF50';
  107. clearButton.style.color = 'white';
  108. clearButton.style.border = 'none';
  109. clearButton.style.borderRadius = '3px';
  110. clearButton.style.cursor = 'pointer';
  111.  
  112.  
  113. const logContent = document.createElement('div');
  114. logContent.style.marginTop = '40px';
  115. logContent.style.overflowY = 'scroll';
  116. logContent.style.height = '230px';
  117. logContent.style.userSelect = 'text';
  118.  
  119. // 添加事件监听器
  120. button.addEventListener('click', () => {
  121. logWindow.style.display = 'block';
  122. });
  123.  
  124. closeButton.addEventListener('click', () => {
  125. logWindow.style.display = 'none';
  126. });
  127.  
  128. clearButton.addEventListener('click', () => {
  129. logContent.innerHTML = '';
  130. });
  131.  
  132. // 添加内容到 logWindow
  133. logWindow.appendChild(closeButton);
  134. logWindow.appendChild(clearButton);
  135. // 创建日志内容容器
  136. logWindow.appendChild(logContent);
  137.  
  138. // 渲染日志内容
  139. renderLog = (entry) => {
  140. const logEntry = document.createElement('div');
  141. logEntry.style.marginBottom = '5px';
  142. logEntry.style.padding = '5px';
  143. logEntry.style.borderBottom = '1px solid #555';
  144. logEntry.style.wordBreak = 'break-word';
  145.  
  146. const time = entry.timestamp.toLocaleTimeString();
  147. const type = entry.type.toUpperCase();
  148. const args = entry.args.map(arg => {
  149. if (typeof arg === 'object') {
  150. try {
  151. return JSON.stringify(arg);
  152. } catch (e) {
  153. return '[Object]';
  154. }
  155. }
  156. return arg.toString();
  157. }).join(' ');
  158.  
  159. logEntry.innerHTML = `<strong>[${time}] ${type}:</strong> ${args}`;
  160. logContent.appendChild(logEntry);
  161. };
  162.  
  163. clearLog = () => {
  164. logContent.innerHTML = '';
  165. }
  166. // 将按钮和日志窗口添加到页面
  167. document.body.appendChild(button);
  168. document.body.appendChild(logWindow);
  169. };
  170.  
  171. // 延迟创建界面,确保页面加载完成
  172. window.addEventListener('load', createLogViewer);
  173. })();