Okay, here is a Tampermonkey script designed specifically for crackap.com domains. It incorporates the plain text copying functionality and adds e.stopImmediatePropagation() within the copy event handler. This method prevents any other event listeners attached to the same element (document in this case) for the same event type (copy) from being executed after this script's handler runs.
This should effectively block CrackAP's own copy event listeners (if they have any attached directly to document or an ancestor that bubbles up) from modifying the clipboard during the copy event itself, right after you've set it to plain text.
Important Considerations:
- Event Capture Phase: The script now uses
true as the third argument for addEventListener. This makes the listener run during the "capture" phase (as the event travels down the DOM tree) instead of the "bubble" phase (as it travels up). This increases the chance that this script's listener runs before any listeners the page might have attached in the bubble phase or even later in the capture phase on descendant elements.
- Input Fields: The script now checks if the copy originates from an
<input> or <textarea>. If so, it allows the default copy behavior to proceed, as interfering here is usually undesirable.
- Asynchronous Changes: This script primarily prevents interference during the synchronous execution of the
copy event handlers. It cannot magically prevent any JavaScript on the page from ever modifying the clipboard using navigator.clipboard.writeText() or write() at some later point (e.g., triggered by a setTimeout or another event). However, interfering during the copy event is the most common way sites manipulate copied content.
@run-at: Setting @run-at document-end tries to ensure the script runs after the main DOM is parsed but potentially before all of the site's own scripts have finished executing or attaching their final event listeners. This, combined with the capture phase, gives it a good chance of running first.
// ==UserScript==
// @name CrackAP Secure Plain Text Copy
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Copies selected text as plain text on CrackAP (Ctrl/Cmd+C) and prevents other scripts from immediately overriding the clipboard during the copy event.
// @author Your Name / AI Assistant
// @match *://*.crackap.com/*
// @match *://crackap.com/*
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
console.log("CrackAP Secure Plain Text Copy script active.");
let isCopyShortcutPressed = false;
let allowDefaultCopy = false; // Flag to allow default copy for inputs/textareas
// --- Keydown Listener (Capture Phase) ---
// Detects Ctrl/Cmd + C and checks if it's in an input/textarea
document.addEventListener('keydown', function(e) {
// Check if the key press is relevant (Ctrl/Cmd + C)
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'c') {
const targetTagName = e.target.tagName.toLowerCase();
const targetIsEditable = e.target.isContentEditable;
// Allow default copy behavior for form inputs, textareas, or contenteditable elements
if (targetTagName === 'input' || targetTagName === 'textarea' || targetIsEditable) {
console.log("Copy shortcut in editable field, allowing default behavior.");
isCopyShortcutPressed = false; // Ensure our copy handler doesn't run
allowDefaultCopy = true; // Set flag to explicitly allow default in copy handler if needed
} else {
// It's a general text selection outside editable fields
console.log("Copy shortcut detected for general text selection.");
isCopyShortcutPressed = true;
allowDefaultCopy = false;
// Reset the flag shortly after, just in case the copy event doesn't fire
setTimeout(() => {
isCopyShortcutPressed = false;
}, 150);
}
} else {
// Reset flags if other keys are pressed
isCopyShortcutPressed = false;
allowDefaultCopy = false;
}
}, true); // Use CAPTURE phase to run earlier
// --- Copy Event Listener (Capture Phase) ---
// Handles the actual copy action
document.addEventListener('copy', function(e) {
console.log("Copy event triggered. isCopyShortcutPressed:", isCopyShortcutPressed, "allowDefaultCopy:", allowDefaultCopy);
// If it was triggered in an input/textarea, or not by our shortcut, let it proceed naturally
if (allowDefaultCopy || !isCopyShortcutPressed) {
console.log("Allowing default copy or not triggered by our shortcut.");
// Reset flags just in case
isCopyShortcutPressed = false;
allowDefaultCopy = false;
return;
}
// If we reached here, it's our custom copy for selected text
// --- This is the core logic ---
const selection = window.getSelection();
const selectedText = selection.toString();
if (selectedText) {
console.log("Handling custom plain text copy. Selection:", selectedText.substring(0, 70) + "...");
try {
// 1. Prevent the browser's default copy action (e.g., copying HTML)
e.preventDefault();
// 2. Stop any other 'copy' event listeners on 'document' from running AFTER this one.
// This is the key part to prevent interference during this event.
e.stopImmediatePropagation();
// 3. Set the clipboard data to plain text
if (e.clipboardData && e.clipboardData.setData) {
e.clipboardData.setData('text/plain', selectedText);
console.log("Plain text successfully set to clipboard.");
} else {
console.error("Error: e.clipboardData or setData is not available.");
// As a less ideal fallback, you *could* try navigator.clipboard.writeText
// but it's better practice to use e.clipboardData inside the 'copy' event
// navigator.clipboard.writeText(selectedText).catch(err => console.error('Clipboard API fallback failed:', err));
}
} catch (error) {
console.error("Error during custom copy handling:", error);
// Attempt to allow default behavior if an error occurs
allowDefaultCopy = true;
}
} else {
console.log("Copy triggered, but no text selected.");
// If nothing is selected, we might still want to stop propagation
// to prevent other handlers from doing something unexpected.
try {
e.preventDefault();
e.stopImmediatePropagation();
} catch(error){}
}
// --- End of core logic ---
// Reset flags after handling
isCopyShortcutPressed = false;
allowDefaultCopy = false; // Ensure reset
}, true); // Use CAPTURE phase to run earlier
})();