Bypass Paste Restriction

Allows pasting using Ctrl+V on sites that block it and triggers formatting

  1. // ==UserScript==
  2. // @name Bypass Paste Restriction
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description Allows pasting using Ctrl+V on sites that block it and triggers formatting
  6. // @author OpenAI (gpt-4o-mini-2024-07-18)
  7. // @author Yi-01.AI (Yi-01.AI) (v0.4)
  8. // @license MIT
  9. // @match *://*/*
  10. // @grant clipboardRead
  11. // @grant clipboardWrite
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. document.addEventListener('keydown', async function(event) {
  18. if (event.ctrlKey && event.key === 'v') {
  19. event.preventDefault(); // Prevent the default paste behavior
  20.  
  21. try {
  22. // Use the Clipboard API to read the clipboard content
  23. const text = await navigator.clipboard.readText();
  24. const activeElement = document.activeElement;
  25.  
  26. if (activeElement && (
  27. activeElement.tagName === 'INPUT' ||
  28. activeElement.tagName === 'TEXTAREA' ||
  29. activeElement.getAttribute('contenteditable') === 'true'
  30. )) {
  31. if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') {
  32. // For standard input and textarea fields
  33. const start = activeElement.selectionStart;
  34. const end = activeElement.selectionEnd;
  35. const newValue = activeElement.value.substring(0, start) + text + activeElement.value.substring(end);
  36. activeElement.value = newValue;
  37. activeElement.setSelectionRange(start + text.length, start + text.length);
  38. } else if (activeElement.getAttribute('contenteditable') === 'true') {
  39. // For contenteditable divs
  40. const selection = window.getSelection();
  41. if (selection.rangeCount > 0) {
  42. const range = selection.getRangeAt(0);
  43. range.deleteContents();
  44. const textNode = document.createTextNode(text);
  45. range.insertNode(textNode);
  46. range.collapse(false); // Collapse the selection to the end of the pasted text
  47. selection.removeAllRanges();
  48. selection.addRange(range);
  49. }
  50. }
  51.  
  52. // Create and dispatch input event to trigger formatting
  53. const inputEvent = new Event('input', { bubbles: true });
  54. activeElement.dispatchEvent(inputEvent);
  55.  
  56. // Create and dispatch change event to ensure any additional formatting logic runs
  57. const changeEvent = new Event('change', { bubbles: true });
  58. activeElement.dispatchEvent(changeEvent);
  59. }
  60. } catch (err) {
  61. console.error('Failed to read clipboard contents: ', err);
  62. }
  63. }
  64. });
  65. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址