Greasy Fork 支持简体中文。

Console-in-Pages

Creates a floating console in browser

  1. // ==UserScript==
  2. // @name Console-in-Pages
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.0
  5. // @description Creates a floating console in browser
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Create the shell container
  16. const shellContainer = document.createElement('div');
  17. shellContainer.style.position = 'fixed';
  18. shellContainer.style.bottom = '100px';
  19. shellContainer.style.left = '20px';
  20. shellContainer.style.width = '500px';
  21. shellContainer.style.height = '300px';
  22. shellContainer.style.backgroundColor = '#000';
  23. shellContainer.style.color = '#0f0';
  24. shellContainer.style.fontFamily = 'monospace';
  25. shellContainer.style.zIndex = '99999';
  26. shellContainer.style.overflowY = 'auto';
  27. shellContainer.style.padding = '10px';
  28. shellContainer.style.display = 'none'; // Initially hidden
  29. document.body.appendChild(shellContainer);
  30.  
  31. // Create the header (for dragging)
  32. const header = document.createElement('div');
  33. header.style.backgroundColor = '#333';
  34. header.style.padding = '5px';
  35. header.style.cursor = 'move';
  36. header.innerText = 'Tampermonkey generated Console';
  37. shellContainer.appendChild(header);
  38.  
  39. // Create input field for commands
  40. const inputField = document.createElement('input');
  41. inputField.style.width = '100%';
  42. inputField.style.backgroundColor = '#000';
  43. inputField.style.color = '#0f0';
  44. inputField.style.border = 'none';
  45. inputField.style.outline = 'none';
  46. inputField.style.fontSize = '16px';
  47. inputField.placeholder = 'Type your command here...';
  48. shellContainer.appendChild(inputField);
  49.  
  50. // Function to handle user input
  51. inputField.addEventListener('keydown', function(e) {
  52. if (e.key === 'Enter') {
  53. const command = inputField.value.trim();
  54. if (command) {
  55. executeCommand(command);
  56. }
  57. inputField.value = ''; // Clear input after execution
  58. }
  59. });
  60.  
  61. // Function to execute commands
  62. function executeCommand(command) {
  63. const output = document.createElement('div');
  64. output.textContent = `$ ${command}`;
  65. shellContainer.appendChild(output);
  66.  
  67. // Handle predefined commands
  68. if (command === 'hello') {
  69. showOutput('Hello world!');
  70. } else if (command === 'date') {
  71. showOutput(new Date().toLocaleString());
  72. } else if (command === 'clear') {
  73. clearShell();
  74. } else {
  75. showOutput('Command not recognized');
  76. }
  77.  
  78. // Scroll to bottom
  79. shellContainer.scrollTop = shellContainer.scrollHeight;
  80. }
  81.  
  82. // Function to display command output
  83. function showOutput(outputText) {
  84. const output = document.createElement('div');
  85. output.textContent = outputText;
  86. shellContainer.appendChild(output);
  87. }
  88.  
  89. // Function to clear the shell
  90. function clearShell() {
  91. shellContainer.innerHTML = '';
  92. shellContainer.appendChild(header); // Keep the header
  93. shellContainer.appendChild(inputField); // Keep the input field
  94. }
  95.  
  96. // Create the resize handle
  97. const resizeHandle = document.createElement('div');
  98. resizeHandle.style.position = 'absolute';
  99. resizeHandle.style.bottom = '0';
  100. resizeHandle.style.right = '0';
  101. resizeHandle.style.width = '20px';
  102. resizeHandle.style.height = '20px';
  103. resizeHandle.style.backgroundColor = '#0f0';
  104. resizeHandle.style.cursor = 'se-resize';
  105. shellContainer.appendChild(resizeHandle);
  106.  
  107. // Drag functionality for the terminal window
  108. let isDragging = false;
  109. let offsetX, offsetY;
  110.  
  111. header.addEventListener('mousedown', function(e) {
  112. isDragging = true;
  113. offsetX = e.clientX - shellContainer.getBoundingClientRect().left;
  114. offsetY = e.clientY - shellContainer.getBoundingClientRect().top;
  115. document.addEventListener('mousemove', dragShell);
  116. document.addEventListener('mouseup', stopDrag);
  117. });
  118.  
  119. function dragShell(e) {
  120. if (isDragging) {
  121. shellContainer.style.left = `${e.clientX - offsetX}px`;
  122. shellContainer.style.top = `${e.clientY - offsetY}px`;
  123. }
  124. }
  125.  
  126. function stopDrag() {
  127. isDragging = false;
  128. document.removeEventListener('mousemove', dragShell);
  129. document.removeEventListener('mouseup', stopDrag);
  130. }
  131.  
  132. // Resize functionality for the terminal window
  133. let isResizing = false;
  134. let initialWidth, initialHeight, initialMouseX, initialMouseY;
  135.  
  136. resizeHandle.addEventListener('mousedown', function(e) {
  137. isResizing = true;
  138. initialWidth = shellContainer.offsetWidth;
  139. initialHeight = shellContainer.offsetHeight;
  140. initialMouseX = e.clientX;
  141. initialMouseY = e.clientY;
  142. document.addEventListener('mousemove', resizeShell);
  143. document.addEventListener('mouseup', stopResize);
  144. });
  145.  
  146. function resizeShell(e) {
  147. if (isResizing) {
  148. const width = initialWidth + (e.clientX - initialMouseX);
  149. const height = initialHeight + (e.clientY - initialMouseY);
  150. shellContainer.style.width = `${width}px`;
  151. shellContainer.style.height = `${height}px`;
  152. }
  153. }
  154.  
  155. function stopResize() {
  156. isResizing = false;
  157. document.removeEventListener('mousemove', resizeShell);
  158. document.removeEventListener('mouseup', stopResize);
  159. }
  160.  
  161. // Create the toggle icon
  162. const toggleIcon = document.createElement('div');
  163. toggleIcon.style.position = 'fixed';
  164. toggleIcon.style.bottom = '60px';
  165. toggleIcon.style.left = '10px';
  166. toggleIcon.style.width = '40px';
  167. toggleIcon.style.height = '40px';
  168. toggleIcon.style.backgroundColor = '#0f0';
  169. toggleIcon.style.color = '#000';
  170. toggleIcon.style.fontSize = '24px';
  171. toggleIcon.style.borderRadius = '50%';
  172. toggleIcon.style.textAlign = 'center';
  173. toggleIcon.style.lineHeight = '40px';
  174. toggleIcon.style.cursor = 'pointer';
  175. toggleIcon.style.zIndex = '100000';
  176. toggleIcon.innerHTML = '⌨'; // Unicode for a refresh icon (can be replaced with other symbols)
  177. document.body.appendChild(toggleIcon);
  178.  
  179. // Add dragging functionality to the toggle icon
  180. let iconIsDragging = false;
  181. let iconOffsetX, iconOffsetY;
  182. let iconDragStartTime;
  183.  
  184. toggleIcon.addEventListener('mousedown', function(e) {
  185. iconIsDragging = false;
  186. iconDragStartTime = Date.now(); // Record when the drag starts
  187. iconOffsetX = e.clientX - toggleIcon.getBoundingClientRect().left;
  188. iconOffsetY = e.clientY - toggleIcon.getBoundingClientRect().top;
  189. document.addEventListener('mousemove', dragIcon);
  190. document.addEventListener('mouseup', stopIconDrag);
  191. });
  192.  
  193. function dragIcon(e) {
  194. iconIsDragging = true;
  195. toggleIcon.style.left = `${e.clientX - iconOffsetX}px`;
  196. toggleIcon.style.top = `${e.clientY - iconOffsetY}px`;
  197. }
  198.  
  199. function stopIconDrag(e) {
  200. document.removeEventListener('mousemove', dragIcon);
  201. document.removeEventListener('mouseup', stopIconDrag);
  202.  
  203. // If the mouse was held down for less than 300ms and moved less than 5px, consider it a click
  204. if (iconIsDragging && (Date.now() - iconDragStartTime > 300 || Math.abs(e.clientX - toggleIcon.offsetLeft) > 5 || Math.abs(e.clientY - toggleIcon.offsetTop) > 5)) {
  205. iconIsDragging = false; // It was a drag, not a click
  206. } else {
  207. // Toggle the terminal visibility only on click
  208. if (shellContainer.style.display === 'none') {
  209. shellContainer.style.display = 'block';
  210. } else {
  211. shellContainer.style.display = 'none';
  212. }
  213. }
  214. }
  215.  
  216. })();