首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试通过篡改猴子将www.youtube.com重定向到www.nsfwyoutube.com

尝试通过篡改猴子将www.youtube.com重定向到www.nsfwyoutube.com
EN

Stack Overflow用户
提问于 2020-07-24 08:40:19
回答 1查看 1.1K关注 0票数 0

正如标题所说,

我一直在尝试用下面的代码重定向youtube url:

代码语言:javascript
运行
复制
// ==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。

EN

Stack Overflow用户

回答已采纳

发布于 2020-07-24 11:07:46

您的if条件是无用的,因为只有当它与@include@exclude规则匹配时,才会执行if脚本。

创建newURL时缺少window.location.pathname。您可以在浏览器控制台中获取window.location.xxx的结果。

代码语言:javascript
运行
复制
// ==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);

看起来你只想在打开视频时执行脚本,所以你可以将头部改为

代码语言:javascript
运行
复制
// ==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的文档。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63065137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档