Why doesn't this script work with successive page clicks?

纵饮孤独 提交于 2019-12-04 18:12:27
Brock Adams

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]);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!