0

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.

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
dsdsddsds
  • 3
  • 2

1 Answers1

0

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.

enter image description here

// ==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.

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • i used the code above and ran into a very odd problem. It seems it doesn't work if i click on a video and will have to refresh the video for the code to take effect. Is there a way to make this code work without having to refresh? – dsdsddsds Jul 24 '20 at 13:02
  • @dsdsddsds The script will only be executed at the start of page loading restricted by `@run-at document-start`. You can open the link in a new tab by clicking it with middle mouse. Or you can follow the answer at https://stackoverflow.com/a/18997637/10315163. – Ynjxsjmh Jul 25 '20 at 01:14