// ==UserScript==
// @name Replace X.com URLs with Twitter.com URLs
// @namespace http://tampermonkey.net/
// @description Replaces URLs containing "https://x.com" or "https://www.x.com" with "https://twitter.com".
// @author Author Name
// @version 1.3
// @match https://www.thecoli.com/threads/*
// @match https://thecoli.com/threads/*
// @match https://xenforo.com/community/forums/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to get the current cursor position in a text input field
function getCursorPosition(input) {
let position = 0;
if ('selectionStart' in input) {
position = input.selectionStart;
} else if (document.selection) {
input.focus();
let selection = document.selection.createRange();
selection.moveStart('character', -input.value.length);
position = selection.text.length;
}
return position;
}
// Function to set the cursor position in a text input field
function setCursorPosition(input, position) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(position, position);
} else if (input.createTextRange) {
let range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', position);
range.moveStart('character', position);
range.select();
}
}
// Function to replace URLs in text input fields and editable areas
function replaceURLs(event) {
// Get all editable fields on the page
let editableFields = document.querySelectorAll('[contenteditable="true"], .fr-element.fr-view.fr-element-scroll-visible, textarea.input');
editableFields.forEach(function(field) {
// Save the current cursor position
let cursorPosition = getCursorPosition(field);
// Replace URLs in the value or innerHTML of the field
let value = field.value || field.innerHTML;
let newValue = value.replace(/https:\/\/(www\.)?x\.com/g, 'https://twitter.com');
if (value !== newValue) {
if (field.tagName.toLowerCase() === 'textarea' || field.tagName.toLowerCase() === 'input') {
field.value = newValue;
} else {
field.innerHTML = newValue;
}
// Restore the cursor position
let newCursorPosition = getCursorPosition(field);
if (newCursorPosition > cursorPosition) {
setCursorPosition(field, newCursorPosition - (value.length - newValue.length));
} else {
setCursorPosition(field, newCursorPosition);
}
}
// Check if the user pressed Enter
if (event.type === 'keydown' && event.key === 'Enter') {
// Save the current cursor position
cursorPosition = getCursorPosition(field);
// Replace URLs in the new line
let lines = (field.value || field.innerHTML).split('\n');
lines[cursorPosition.line] = lines[cursorPosition.line].replace(/https:\/\/(www\.)?x\.com/g, 'https://twitter.com');
if (field.tagName.toLowerCase() === 'textarea' || field.tagName.toLowerCase() === 'input') {
field.value = lines.join('\n');
} else {
field.innerHTML = lines.join('\n');
}
// Restore the cursor position
let newCursorPosition = getCursorPosition(field);
if (newCursorPosition > cursorPosition) {
setCursorPosition(field, newCursorPosition - (value.length - lines.join('\n').length));
} else {
setCursorPosition(field, newCursorPosition);
}
}
});
}
// Add event listeners for input and keydown events on the document
document.addEventListener('input', replaceURLs);
document.addEventListener('keydown', replaceURLs);
})();