1

I am currently using the following script in Tampermonkey in Google Chrome:

// ==UserScript==
// @name        Youtube opt in Ads per channel
// @namespace   schippi
// @include     http://www.youtube.com/watch*
// @version     1
// ==/UserScript==

var u = window.location.href;
if (u.search("user=") == -1) {
   var cont = document.getElementById("watch7-user-header").innerHTML;
   var user=cont.replace(/.+\/user\//i,'').replace(/\?(?:.|\s)*/m,'');
   window.location.href = u+"&user="+user;
}

It seems to work perfectly in Firefox with Greasemonkey but in Google Chrome, it seems that it only works on the first click on a YouTube video.

More specifically, if I click on a YouTube video:
   youtube.com/watch?v=MijmeoH9LT4,
it redirects me to:
   youtube.com/watch?v=MijmeoH9LT4&user=Computerphile

However, if I click on a video from the related videos vertical bar, it doesn't seem to do any further redirection.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • @BrockAdams: Hmm.. it still seems to not work. New script: http://pastie.org/pastes/8347656/text – user2805335 Sep 23 '13 at 01:55
  • Yeah, it's the same *kind* of problem, but because YouTube no longer fires the `hashchange` event, the solution is not exactly the same. I'll post an answer in a bit. – Brock Adams Sep 23 '13 at 01:57

1 Answers1

2

Alas, there is still no really "neat" way to do this in Chrome. (Firefox has more options.)

Your best bet is just to poll location.search; see below.

Your other options in Chrome, currently, are not recommended -- but here they are for reference:

  • Hack into the history.pushState function. This gives faster notice of page changes, BUT fires before you can run your code, so it will still need a timer. Plus, it brings in cross-scope problems, in a userscript environment.
  • Use Mutation Observers to monitor changes to the <title> tag. this may work okay, but may fire after you want it to, causing delays and pronounced "flicker". Also may not work on poorly designed pages (YouTube is okay).


Also note that the replace() statements, from the question, will blow up the URL and 404 the script in several cases. Use DOM methods to get the user (see below).


Polling code (Simple, robust, cross-browser):

// ==UserScript==
// @name        Youtube opt in Ads per channel
// @namespace   schippi
// @include     http://www.youtube.com/watch*
// @version     1
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
var elemCheckTimer      = null;
var pageURLCheckTimer   = setInterval (
    function () {
        if (this.lastQueryStr !== location.search) {
            this.lastQueryStr = location.search;
            gmMain ();
        }
    }
    , 111   //-- Nine times a second. Plenty fast w/o bogging page
);

function gmMain () {
    if ( ! /user=/.test (window.location.href) ) {
       elemCheckTimer = setInterval (checkUserAndRelocate, 24);
    }
}

function checkUserAndRelocate () {
    var elem        = document.querySelector (
        "#watch7-user-header a[href*='/user/']"
    );
    if (elem) {
        clearInterval (elemCheckTimer);
        var user    = elem.href.match (/\/user\/(\w+)\W?/);
        if (user  &&  user.length > 1) {
            location.replace (location.href + "&user=" + user[1]);
        }
    }
}
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • The script might be outdated. It puts the user on home page if you click on it. And it does not put it on videos without a reload. – Gopoi Jan 31 '15 at 23:01
  • @Gopoi, Yeah, YouTube changes rapidly. I'll put this in the queue to recheck, but the issue seems to be of low interest - so it's low priority. – Brock Adams Jan 31 '15 at 23:34
  • I am myself checking it see if I can come with a fix. It is just weird the script does not seem to start when on /watch page, but it does after a reload. Might it be a tampermonkey issue? Plus when quitting a page (pressing the youtube home button) the user= gets stuck in the address leading to error. I tried the first script and changed the second replace for /\"(?:.|\s)*/m matching the terminating ", but the script does not execute. In debug window it shows that the script is queued but it never executes it. – Gopoi Jan 31 '15 at 23:40
  • @Gopoi, It's AJAX-related behavior. And, Google-owned sites are a particular pain to script for. (The "YouTube" you see, may be quite a bit different than the "YouTube" I see, for starters.) – Brock Adams Jan 31 '15 at 23:45