Reverse every selected line (Full) or (Smart). Alt+R or Tampermonkey menu. Debug mode copies result to clipboard for doing it manually if a certain site doesn't work (string looks normal but bypasses some filters)
当前为
// ==UserScript==
// @name Universal Text Reversal bypass
// @namespace github.com/annaroblox
// @version 1.6
// @license MIT
// @description Reverse every selected line (Full) or (Smart). Alt+R or Tampermonkey menu. Debug mode copies result to clipboard for doing it manually if a certain site doesn't work (string looks normal but bypasses some filters)
// @author AnnaRoblox
// @match *://*/*
// @grant GM_registerMenuCommand
// @grant GM_setClipboard
// ==/UserScript==
(function () {
'use strict';
/* ---------- CONFIG ---------- */
const STORAGE_KEY = 'utr_mode';
const RTL_MAGIC = "\u202E";
const PDF = "\u202C";
const RLO = "\u202E";
const RLI = "\u2067";
const PDI = "\u2069";
let isDebugMode = false;
let mode = localStorage.getItem(STORAGE_KEY) || 'smart';
/* ---------- CORE TEXT TRANSFORM ---------- */
const reverseLines = text =>
text.split('\n')
.map(l => RTL_MAGIC + [...l].reverse().join(''))
.join('\n');
const smartReverse = text =>
text
.split('\n')
.map(line => {
// Split into words keeping trailing spaces attached to each word
const words = line.match(/\S+\s*/g) || [];
// Collect all pairs from all words
const allPairs = [];
for (const word of words) {
// Split each word into 2-char pairs
for (let i = 0; i < word.length; i += 2) {
allPairs.push(word.slice(i, i + 2));
}
}
if (allPairs.length === 0) return line;
let result = '';
// First pair: RLO + reversed + PDF
const firstPair = allPairs[0];
const firstReversed = [...firstPair].reverse().join('');
result += RLO + firstReversed + PDF;
// Remaining pairs in REVERSE order, each with RLO + reversed (no PDF)
if (allPairs.length > 1) {
result += RLI;
for (let i = allPairs.length - 1; i >= 1; i--) {
const pair = allPairs[i];
const reversed = [...pair].reverse().join('');
result += RLO + reversed;
}
result += PDI;
}
return result;
})
.join('\n');
const transform = txt => mode === 'full' ? reverseLines(txt) : smartReverse(txt);
/* ---------- UI / MENU ---------- */
function buildMenu() {
GM_registerMenuCommand('Reverse selected lines', processSelection);
GM_registerMenuCommand(
`Mode: ${mode.toUpperCase()} (click to toggle)`,
() => {
mode = mode === 'full' ? 'smart' : 'full';
localStorage.setItem(STORAGE_KEY, mode);
buildMenu();
}
);
GM_registerMenuCommand(
`DEBUG: ${isDebugMode ? 'ON' : 'OFF'} (click to toggle)`,
() => { isDebugMode = !isDebugMode; buildMenu(); }
);
}
/* ---------- CLIPBOARD HELPERS ---------- */
function copyToClipboard(textToCopy) {
if (typeof GM_setClipboard !== 'undefined') {
GM_setClipboard(textToCopy);
} else if (navigator.clipboard) {
navigator.clipboard.writeText(textToCopy).catch(console.error);
} else {
const ta = document.createElement('textarea');
ta.value = textToCopy;
ta.style.position = 'fixed'; ta.style.left = '-9999px';
document.body.appendChild(ta);
ta.select();
try { document.execCommand('copy'); } catch (e) {}
document.body.removeChild(ta);
}
}
/* ---------- IMPROVED INPUT DETECTION ---------- */
function isEditableElement(el) {
if (!el || !el.nodeType || el.nodeType !== 1) return false;
const tag = el.tagName?.toLowerCase();
if (tag === 'input' || tag === 'textarea') return true;
if (el.contentEditable === 'true') return true;
if (el.isContentEditable) return true;
if (el.designMode === 'on') return true;
// Check ARIA attributes
const role = el.getAttribute('role');
if (role === 'textbox' || role === 'searchbox') return true;
return false;
}
function findEditableInShadowDOM(root) {
if (!root) return null;
if (root.activeElement && isEditableElement(root.activeElement)) {
return root.activeElement;
}
if (root.activeElement?.shadowRoot) {
const found = findEditableInShadowDOM(root.activeElement.shadowRoot);
if (found) return found;
}
const selectors = [
'input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"])',
'textarea',
'[contenteditable="true"]',
'[role="textbox"]',
'[role="searchbox"]'
];
for (const selector of selectors) {
const el = root.querySelector(selector);
if (el && isEditableElement(el)) return el;
}
return null;
}
function locateRealInput(node) {
let cur = node;
// Traverse up including shadow DOM boundaries
while (cur) {
if (cur.nodeType === 1 && isEditableElement(cur)) {
const tag = cur.tagName?.toLowerCase();
if (tag === 'input' || tag === 'textarea') {
return { element: cur, type: tag };
}
return { element: cur, type: 'contenteditable' };
}
if (cur.shadowRoot) {
const shadowEditable = findEditableInShadowDOM(cur.shadowRoot);
if (shadowEditable) {
const tag = shadowEditable.tagName?.toLowerCase();
if (tag === 'input' || tag === 'textarea') {
return { element: shadowEditable, type: tag };
}
return { element: shadowEditable, type: 'contenteditable' };
}
}
cur = cur.parentNode || cur.host;
// Handle shadow root boundaries
if (!cur && node.getRootNode) {
const root = node.getRootNode();
if (root?.host) cur = root.host;
}
}
// Check document.activeElement chain
let active = document.activeElement;
while (active) {
if (isEditableElement(active)) {
const tag = active.tagName?.toLowerCase();
if (tag === 'input' || tag === 'textarea') {
return { element: active, type: tag };
}
return { element: active, type: 'contenteditable' };
}
if (active.shadowRoot?.activeElement) {
active = active.shadowRoot.activeElement;
} else if (active.shadowRoot) {
const shadowEditable = findEditableInShadowDOM(active.shadowRoot);
if (shadowEditable) {
const tag = shadowEditable.tagName?.toLowerCase();
if (tag === 'input' || tag === 'textarea') {
return { element: shadowEditable, type: tag };
}
return { element: shadowEditable, type: 'contenteditable' };
}
break;
} else {
break;
}
}
// Check iframes
try {
for (const frame of document.querySelectorAll('iframe')) {
try {
const frameDoc = frame.contentDocument || frame.contentWindow?.document;
if (frameDoc?.activeElement && isEditableElement(frameDoc.activeElement)) {
const tag = frameDoc.activeElement.tagName?.toLowerCase();
if (tag === 'input' || tag === 'textarea') {
return { element: frameDoc.activeElement, type: tag };
}
return { element: frameDoc.activeElement, type: 'contenteditable' };
}
} catch (e) { /* Cross-origin */ }
}
} catch (e) {}
return null;
}
/* ---------- IMPROVED TEXT REPLACEMENT ---------- */
function replaceTextInInput(el, type, reversed, start, end) {
if (type === 'input' || type === 'textarea') {
const original = el.value;
const replacement = original.slice(0, start) + reversed + original.slice(end);
const scrollTop = el.scrollTop;
el.value = replacement;
el.setSelectionRange(start, start + reversed.length);
el.scrollTop = scrollTop;
// Trigger events for React/Vue/Angular
el.dispatchEvent(new Event('input', { bubbles: true }));
el.dispatchEvent(new Event('change', { bubbles: true }));
} else {
const sel = window.getSelection();
// Try execCommand first (better undo support)
if (document.queryCommandSupported('insertText')) {
try {
if (sel.rangeCount > 0) {
const range = sel.getRangeAt(0);
if (start !== end) range.deleteContents();
}
document.execCommand('insertText', false, reversed);
el.dispatchEvent(new Event('input', { bubbles: true }));
return;
} catch (e) {}
}
// Fallback: manual insertion
if (sel.rangeCount > 0) {
const range = sel.getRangeAt(0);
range.deleteContents();
const textNode = document.createTextNode(reversed);
range.insertNode(textNode);
range.setStartAfter(textNode);
range.setEndAfter(textNode);
sel.removeAllRanges();
sel.addRange(range);
el.dispatchEvent(new Event('input', { bubbles: true }));
}
}
}
function processSelection() {
const sel = window.getSelection();
const inputInfo = locateRealInput(sel.focusNode || document.activeElement);
if (!inputInfo) {
const selected = sel.toString();
if (selected) {
const reversed = transform(selected);
if (isDebugMode) copyToClipboard(reversed);
try {
document.execCommand('insertText', false, reversed);
} catch (e) {
copyToClipboard(reversed);
}
}
return;
}
const { element: el, type } = inputInfo;
let original, start, end;
if (type === 'input' || type === 'textarea') {
original = el.value;
start = el.selectionStart ?? 0;
end = el.selectionEnd ?? 0;
} else {
original = el.textContent || el.innerText || '';
const range = sel.rangeCount ? sel.getRangeAt(0) : null;
if (!range) {
start = end = 0;
} else {
const pre = range.cloneRange();
pre.selectNodeContents(el);
pre.setEnd(range.startContainer, range.startOffset);
start = pre.toString().length;
end = start + range.toString().length;
}
}
const chunk = (start === end) ? original : original.slice(start, end);
const reversed = transform(chunk);
if (isDebugMode) copyToClipboard(reversed);
replaceTextInInput(el, type, reversed, start, end);
}
/* ---------- KEYBOARD SHORTCUT ---------- */
document.addEventListener('keydown', e => {
if (e.altKey && e.key.toLowerCase() === 'r') {
e.preventDefault();
processSelection();
}
}, true); // Capture phase
buildMenu();
})();