正如标题所说,
我一直在尝试用下面的代码重定向youtube url:
// ==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);
}当我开始看视频时,它似乎不起作用,我对代码不是很擅长。
我正在使用firefox。
发布于 2020-07-24 11:07:46
您的if条件是无用的,因为只有当它与@include和@exclude规则匹配时,才会执行if脚本。
创建newURL时缺少window.location.pathname。您可以在浏览器控制台中获取window.location.xxx的结果。

// ==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);看起来你只想在打开视频时执行脚本,所以你可以将头部改为
// ==UserScript==
// @name youtube to nsfwyoutube
// @match *://*youtube.com/watch*
// @run-at document-start
// @version 1
// @grant none
// ==/UserScript==可以在https://www.tampermonkey.net/documentation.php上找到tampermonkey的文档。
https://stackoverflow.com/questions/63065137
复制相似问题