Redirect X & Twitter to xcancel.com in address bar

Redirect x.com and twitter.com URLs to xcancel.com in address bar.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name 			Redirect X & Twitter to xcancel.com in address bar
// @namespace		x.com-xcancel.com-redirect
// @version			2.0.1
// @description	Redirect x.com and twitter.com URLs to xcancel.com in address bar.
// @author			github.com/localhorst
// @license MIT
// @match 			*://*.x.com/*
// @match 			*://*.twitter.com/*
// @exclude 		*://*.x.com/i/*
// @exclude 		*://*.twitter.com/i/*
// @run-at 			document-start
// @grant           none
// ==/UserScript==

// Enforce strict mode for better code quality
'use strict';

// Declare constant for current URL
const currentUrl = window.location.href;

// Declare constant for old reddit URL
const xCancelUrl = 'https://xcancel.com/';

// Check if the current URL does not include old.x.com
if ((currentUrl.includes("twitter.com") || currentUrl.includes("x.com")) && !currentUrl.includes("twitter.com/i/") && !currentUrl.includes("x.com/i/")) {
  // Use regex literal and constant for new URL
	const newUrl = currentUrl.replace(/^https?:\/\/(www\.)?(x|twitter)\.com\//, xCancelUrl);
  // Redirect to new URL without history entry
  window.location.replace(newUrl);
}