Greasy Fork

Easy Input

模拟键盘输入实现文本粘贴。

目前为 2024-10-21 提交的版本。查看 最新版本

// ==UserScript==
// @name         Easy Input
// @namespace    zarttic
// @version      3.2
// @description  模拟键盘输入实现文本粘贴。
// @author       zarttic
// @run-at       document-start
// @license      ISC
// @grant        none
// @match       *://*.*.*/*
// ==/UserScript==

(function() {
  'use strict';

  // 创建一个按钮
  const button = document.createElement('button');
  button.innerText = '👉一键粘贴👈';
  button.style.position = 'fixed';
  button.style.bottom = '10px';
  button.style.right = '10px';
  button.style.zIndex = '1000';
  button.style.padding = '10px';
  button.style.backgroundColor = '#4CAF50';
  button.style.color = 'white';
  button.style.border = 'none';
  button.style.borderRadius = '5px';
  button.style.cursor = 'pointer';

  // 添加按钮到页面
  document.body.appendChild(button);

  // 监听按钮点击事件
  button.addEventListener('click', async () => {
    const text = prompt('请输入要粘贴的文本:');
    if (text) {
      simulateInput(text);
    }
  });

  // 模拟输入函数
  function simulateInput(text) {
    const inputField = document.activeElement;
    if (inputField && (inputField.tagName === 'INPUT' || inputField.tagName === 'TEXTAREA')) {
      inputField.value = text;
      const event = new Event('input', { bubbles: true });
      inputField.dispatchEvent(event);
    }
  }
})();