Skip to content

Instantly share code, notes, and snippets.

@loilo
Last active October 12, 2023 21:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save loilo/de8c9a4f4ee0104ddb3becb665f2ff9d to your computer and use it in GitHub Desktop.
Save loilo/de8c9a4f4ee0104ddb3becb665f2ff9d to your computer and use it in GitHub Desktop.
Userscript: Redirect Website to its English Version
// This userscript redirects you to the English version of a website if it's denoted in the source code.
// Insert any URLs of websites below (after @match), for example https://developer.mozilla.org/* or https://www.php.net/*
// Use multiple @match clauses to enable the script on several domains.
// ==UserScript==
// @name Redirect to English
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Redirect websites to their English version
// @author Florian Reuschel <florian@loilo.de>
// @match [INSERT URL HERE]
// @grant none
// ==/UserScript==
if (document.documentElement.lang !== 'en' && !document.documentElement.lang.startsWith('en-')) {
const referrerHost = document.referrer
? new URL(document.referrer).hostname
: undefined
const currentHost = new URL(location.href).hostname
if (
// Don't redirect when directly calling the page
referrerHost !== undefined &&
// Don't redirect if coming from another page on the same domain
referrerHost !== currentHost &&
// Don't redirect if history state indicates that this page has previously been
// redirected away from (to maintain ability to go back to the original language)
!(history.state && history.state.redirectToEnglish)
) {
const englishLink = document.head.querySelector('link[rel="alternate"][hreflang="en"]')
if (englishLink) {
history.replaceState({ redirectToEnglish: true }, null)
location.assign(englishLink.href)
}
}
}
@bonnebulle
Copy link

bonnebulle commented Sep 17, 2022

En très très simplifié.. In french (FR)

// ==UserScript==
// @name        Redirect to my language - mozilla.org
// @namespace   Violentmonkey Scripts
// @match       https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties
// @grant       none
// @version     1.0
// @author      -
// @description 17/09/2022, 20:39:30
// ==/UserScript==

// WORKS FROM eng_US ONLY
  if (document.documentElement.lang !== 'fr') {
     const frlink = document.head.querySelector('link[rel="alternate"][hreflang="fr"]').getAttribute("href");
     if (frlink) {
       location.assign(frlink); 
    }
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment