As the title says,
I have been trying to redirect the youtube url with this code:
// ==UserScript==
// @run-at document-start
// @name youtube to nsfwyoutube
// @include https://www.youtube.com/*
// @exclude https://www.youtube.com
// @exclude https://www.youtube.com/feed*
// @exclude https://www.youtube.com/channel*
// @exclude https://www.youtube.com/results*
// @exclude https://www.youtube.com/c*
// @version 1
// @grant none
// ==/UserScript==
var oldUrlPath = window.location.host + "/" + window.location.pathname;
/*--- Test that ".compact" is at end of URL, excepting any "hashes"
or searches.
*/
if ( ("www.nsfwyoutube.com/watch") != oldUrlPath) {
var newURL = window.location.protocol + "//"
+ "www.nsfwyoutube.com"
+ "/watch"
+ window.location.search
+ window.location.hash
;
/*-- replace() puts the good page in the history instead of the
bad page.
*/
window.location.replace (newURL);
}
It doesn't seem to work when I start watching a video, i am not very good with code.
I am using firefox.
Your if
condition is useless since the tampermonkey script will only execute when it matches @include
and @exclude
rules.
window.location.pathname
is missing when your create newURL
. You can get the result of window.location.xxx
in your browser console.
// ==UserScript==
// @name youtube to nsfwyoutube
// @include https://www.youtube.com/*
// @exclude https://www.youtube.com
// @exclude https://www.youtube.com/feed*
// @exclude https://www.youtube.com/channel*
// @exclude https://www.youtube.com/results*
// @exclude https://www.youtube.com/c*
// @run-at document-start
// @version 1
// @grant none
// ==/UserScript==
var newHost = window.location.host.replace("youtube", "nsfwyoutube");
var newURL = window.location.protocol + "//" +
newHost +
window.location.pathname +
window.location.search +
window.location.hash;
window.location.replace (newURL);
It seems that you only want the script to be executed when you open a video, so you can change the header to
// ==UserScript==
// @name youtube to nsfwyoutube
// @match *://*youtube.com/watch*
// @run-at document-start
// @version 1
// @grant none
// ==/UserScript==
The documentation of tampermonkey can be found at https://www.tampermonkey.net/documentation.php.