2013-09-22 31 views
1

我目前使用Tampermonkey下面的脚本在谷歌浏览器:为什么这个脚本不能连续点击页面?

// ==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; 
} 

它似乎在Firefox中的Greasemonkey,但在谷歌浏览器很好地工作,它似乎只适用于第一个点击一个YouTube视频。

更具体地说,如果我点击一个YouTube视频:
youtube.com/watch?v=MijmeoH9LT4
它重定向我:
youtube.com/watch?v=MijmeoH9LT4&user=Computerphile

但是,如果我点击从相关视频竖线的视频,它不似乎没有做进一步的重定向。

+0

@BrockAdams:嗯..似乎仍然无法正常工作。新脚本:http://pastie.org/pastes/8347656/text – user2805335

+0

是的,这是相同的问题,但是因为YouTube不再激发'hashchange'事件,所以解决方案并不完全相同。我会稍微发表一个答案。 –

回答

2

唉,在Chrome中仍然没有真正“干净”的方式来做到这一点。 (Firefox有更多的选择。)

最好的办法就是轮询location.search;见下文。

其他选择在Chrome中,目前,不建议使用 - 但在这里,他们是参考:

  • Hack into the history.pushState function。这可以提供更快的页面更改通知,但在运行代码之前触发,所以它仍然需要定时器。另外,它在用户标记环境中引入了跨范围的问题。
  • 使用突变观察者来监视对<title>标记的更改。这可能工作正常,但可能会在您想要之后触发,导致延迟并发出“闪烁”。也可能不适用于设计不佳的页面(YouTube可以)。


还要注意的是replace()语句,从这个问题,将炸毁的URL和404脚本在几起案件。使用DOM方法获取用户(见下文)。


投票代码(简单,健壮,跨浏览器):

// ==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]); 
     } 
    } 
} 
+0

该脚本可能已过时。它将用户放在主页上,如果你点击它。它不会在没有重新加载的情况下将其放入视频中。 – Gopoi

+0

@Gopoi,是的,YouTube变化很快。我会把它放在队列中重新检查,但这个问题似乎是低利率的 - 所以它的优先级低。 –

+0

我自己检查一下,看看我能否修复。这只是奇怪的脚本似乎不开始时,在/观看页面,但它会在重新加载后。可能是一个篡改密钥问题?另外,当退出一个页面(按下YouTube的主页按钮)时,用户=卡在导致错误的地址中。我尝试了第一个脚本,并更改​​了第二个替换为/\"(?:.|\s)*/m匹配终止“,但脚本不执行。在调试窗口中,它显示脚本已排队,但从不执行它。 – Gopoi

相关问题