Easy Input

一键粘贴文本到当前页面的输入框.

  1. // ==UserScript==
  2. // @name Easy Input
  3. // @namespace zarttic
  4. // @description 一键粘贴文本到当前页面的输入框.
  5. // @author zarttic
  6. // @match *
  7. // @grant none
  8. // @license MIT
  9. // @version 1.03
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 添加样式
  16. const style = document.createElement('style');
  17. style.textContent = `
  18. body {
  19. font-family: Arial, sans-serif;
  20. padding: 10px;
  21. width: 300px;
  22. }
  23.  
  24. .container {
  25. text-align: center;
  26. }
  27.  
  28. textarea {
  29. width: 100%;
  30. height: 100px;
  31. margin-bottom: 10px;
  32. }
  33.  
  34. button {
  35. width: 100%;
  36. padding: 10px;
  37. background-color: #0078d7;
  38. color: white;
  39. border: none;
  40. cursor: pointer;
  41. }
  42.  
  43. button:hover {
  44. background-color: #005a9e;
  45. }
  46. `;
  47. document.head.appendChild(style);
  48.  
  49. // 创建一个容器
  50. const container = document.createElement('div');
  51. container.className = 'container';
  52.  
  53. // 创建标题
  54. const title = document.createElement('h1');
  55. title.innerText = '😎Easy Input✍️';
  56. container.appendChild(title);
  57.  
  58. // 创建文本区域
  59. const textarea = document.createElement('textarea');
  60. textarea.id = 'textInput';
  61. textarea.placeholder = '✍️粘贴到这里~~';
  62. container.appendChild(textarea);
  63.  
  64. // 创建按钮
  65. const button = document.createElement('button');
  66. button.id = 'pasteButton';
  67. button.innerText = '👉一键粘贴👈';
  68. container.appendChild(button);
  69.  
  70. // 添加容器到页面
  71. document.body.appendChild(container);
  72.  
  73. // 监听按钮点击事件
  74. button.addEventListener('click', async () => {
  75. const text = textarea.value;
  76. if (text) {
  77. simulateInput(text);
  78. }
  79. });
  80.  
  81. // 模拟输入函数
  82. function simulateInput(text) {
  83. const inputField = document.activeElement;
  84. if (inputField && (inputField.tagName === 'INPUT' || inputField.tagName === 'TEXTAREA')) {
  85. inputField.value = text;
  86. const event = new Event('input', { bubbles: true });
  87. inputField.dispatchEvent(event);
  88. }
  89. }
  90. })();