Skip to content

Instantly share code, notes, and snippets.

@aniket91
Last active October 3, 2016 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniket91/464ed8f61ecdeec7cf99ae65ea6514c9 to your computer and use it in GitHub Desktop.
Save aniket91/464ed8f61ecdeec7cf99ae65ea6514c9 to your computer and use it in GitHub Desktop.
GM script to redirect a URL to new URL
// ==UserScript==
// @name PageRedirectScript
// @include http://domain1/*
// @include https://domain1/*
// @run-at document-start
// @description Redirects page from one domain to another. Note if domain1 is down you need to add entry in /etc/hosts file as "ipOfDomain2 domain1 domain2". You can get the ipOfDomain2 as "ping domain2"
// @namespace http://opensourceforgeeks.blogspot.in/
// @version 1
// @grant none
// ==/UserScript==
console.log ("Executing PageRedirectScript");
reloadPage();
function reloadPage() {
var currentUrl = window.location.href;
console.log ("Current URL : " + currentUrl);
if(currentUrl.includes("domain1")) {
currentUrl = currentUrl.replace("domain1","domain2");
console.log ("New URL : " + currentUrl);
//replace() puts the good page in the history instead of the bad page
window.location.replace (currentUrl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment