Allows pasting using Ctrl+V on sites that block it and triggers formatting
当前为
// ==UserScript==
// @name Bypass Paste Restriction
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Allows pasting using Ctrl+V on sites that block it and triggers formatting
// @author OpenAI (gpt-4o-mini-2024-07-18)
// @license MIT
// @match *://*/*
// @grant clipboardRead
// @grant clipboardWrite
// ==/UserScript==
(function() {
'use strict';
document.addEventListener('keydown', async function(event) {
if (event.ctrlKey && event.key === 'v') {
event.preventDefault(); // Prevent the default paste behavior
try {
// Use the Clipboard API to read the clipboard content
const text = await navigator.clipboard.readText();
// Find the currently focused input or textarea
const activeElement = document.activeElement;
if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
// Insert the pasted text at the current cursor position
const start = activeElement.selectionStart;
const end = activeElement.selectionEnd;
const newValue = activeElement.value.substring(0, start) + text + activeElement.value.substring(end);
// Set the new value
activeElement.value = newValue;
activeElement.setSelectionRange(start + text.length, start + text.length); // Move cursor to the end of pasted text
// Create and dispatch input event to trigger formatting
const inputEvent = new Event('input', { bubbles: true });
activeElement.dispatchEvent(inputEvent);
// Create and dispatch change event to ensure any additional formatting logic runs
const changeEvent = new Event('change', { bubbles: true });
activeElement.dispatchEvent(changeEvent);
}
} catch (err) {
console.error('Failed to read clipboard contents: ', err);
}
}
});
})();