url 的 Greasemonkey 脚本测试并添加参数

Greasemonkey script testfor url & add parameter

我想检查站点 youtube.com,如果它是视频站点(包括 watch?v=),则将 &rel=0 添加到 url。伪代码:

if url = youtube.com/watch?v=* |then newurl = url + &rel=0

更多示例:

诀窍是使用现代 javascript 替换 URL 而无需重定向。第二个技巧是知道 window.location 包含已解析的 URL 分为几个部分(主机名、路径、查询参数...)。

// ==UserScript==
// @name        window rel youtube
// @namespace   util
// @match       *://www.youtube.com/*
// @version     1
// @grant       none
// ==/UserScript==
const watchRegex = /watch\?v=/i
if(window.location.href.indexOf("watch?v=")!=-1 && window.location.search.indexOf("rel=0")==-1) {
    console.log("changing url");
    var baseURL = window.location.origin + window.location.pathname+window.location.search+"&rel=0"+window.location.hash;
    window.history.pushState({"pageTitle":document.title},"", baseURL);
}
else {
    console.log("No change in url");
}