gpt4 book ai didi

javascript - 为什么此脚本不适用于连续的页面点击?

转载 作者:行者123 更新时间:2023-11-30 17:51:19 26 4
gpt4 key购买 nike

我目前在 Google Chrome 的 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 完美配合,但在 Google Chrome 中,它似乎只在第一次点击 YouTube 视频时有效。

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

但是,如果我点击相关视频垂直栏中的视频,它似乎不会进行任何进一步的重定向。

最佳答案

唉,在 Chrome 中仍然没有真正“巧妙”的方法来做到这一点。 (Firefox 有更多选项。)

最好的选择就是投票 location.search ;见下文。

Chrome 中的其他选项,目前,不被推荐——但在这里它们仅供引用:

  • > Hack into the history.pushState function .这可以更快地通知页面更改,但会在您运行代码之前触发,因此它仍然需要一个计时器。此外,它还会在用户脚本环境中带来跨范围问题。
  • 使用 Mutation Observers 来监控 <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]);
}
}
}

关于javascript - 为什么此脚本不适用于连续的页面点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18949888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com