我想附加这个字符串:
&sp=CAASAhAB
每次执行 YouTube 搜索查询时都会删除那个愚蠢的“人们还观看了”部分。
我创建了一个 Tampermonkey 脚本,它运行得很好,但在通过网站自己的搜索框执行搜索时不起作用。
有人能帮忙吗?解决这个问题对后人大有裨益。
这是 TamperMonkey 脚本:
// ==UserScript==
// @name Youtube - Remove "People Also Watched" section from search results.
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Add "&sp=CAASAhAB" to YouTube search results URL if not already present. Recommended to also use https://github.com/Vulpelo/hide-youtube-shorts
// @author MattFor | @mattfor | https://github.com/MattFor
// @match www.youtube.com/results?search_query=*
// @grant none
// ==/UserScript==
(function () {
"use strict";
function containsString(mainString, subString) {
return mainString.indexOf(subString) !== -1;
}
function updateURL() {
var currentURL = window.location.href;
var queryString = "&sp=CAASAhAB";
if (!containsString(currentURL, queryString)) {
window.location.href += queryString;
}
}
updateURL();
window.addEventListener("beforeunload", updateURL);
window.addEventListener("DOMContentLoaded", updateURL);
})();
编辑#1:根据问题的评论更新了我的代码,但尚无重大升级,看起来 YouTube POST 请求仅发送一次根据网络标签来判断。
// ==UserScript==
// @name Youtube - Modify search request
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Add "&sp=CAASAhAB" to YouTube search results URL if not already present.
// @author MattFor | @mattfor | https://github.com/MattFor
// @match www.youtube.com/results?search_query=*
// @grant none
// ==/UserScript==
(function () {
"use strict";
function containsString(mainString, subString) {
return mainString.indexOf(subString) !== -1;
}
function updateURL() {
var currentURL = window.location.href;
var queryString = "&sp=CAASAhAB";
if (!containsString(currentURL, queryString)) {
window.location.href += queryString;
}
}
function modifySearchForm() {
var searchForm = document.getElementById("search-form");
if (searchForm) {
searchForm.addEventListener("submit", function (event) {
event.preventDefault();
var formAction = searchForm.action;
var modifiedAction = formAction + "&sp=CAASAhAB";
searchForm.action = modifiedAction;
searchForm.submit();
});
}
}
updateURL();
modifySearchForm();
window.addEventListener("beforeunload", updateURL);
window.addEventListener("DOMContentLoaded", function () {
updateURL();
modifySearchForm();
});
})();
答案1
我以前从未使用过 Tampermonkey,但我会尝试修改你的脚本来处理这个问题。
在我看来,问题在于搜索功能由<form action=""></form>
标签控制。了解这一点后,目标应该是修改action
URL 中的该参数,以便附加sp=CAASAhAB
URL 参数。
这应该做吧:
// ==UserScript==
// @name Youtube - Remove "People Also Watched" section from search results.
// @namespace http://tampermonkey.net/
// @version 1.1
// @description Add "&sp=CAASAhAB" to YouTube search results URL if not already present. Recommended to also use https://github.com/Vulpelo/hide-youtube-shorts
// @author MattFor | @mattfor | https://github.com/MattFor
// @match www.youtube.com/results?search_query=*
// @grant none
// ==/UserScript==
(function () {
"use strict";
function containsString(mainString, subString) {
return mainString.indexOf(subString) !== -1;
}
function updateURL() {
////////////////////////////////////////////////
// Append the URL parameter to the URL.
var currentURL = window.location.href;
var queryString = "&sp=CAASAhAB";
if (!containsString(currentURL, queryString)) {
window.location.href += queryString;
}
////////////////////////////////////////////////
// Append the URL parameter to the form action.
var formAction = document.getElementById("search-form").action;
var queryParam = "?sp=CAASAhAB";
if (!containsString(formAction, queryParam)) {
document.getElementById("search-form").action += queryParam;
}
}
updateURL();
window.addEventListener("beforeunload", updateURL);
window.addEventListener("DOMContentLoaded", updateURL);
})();
如果它不起作用,您可能需要像这样拦截表单提交;基于此的伪代码Stack Overflow 答案:
document.querySelector("#search-form").addEventListener("submit", function(e) {
e.preventDefault()
e.submitter.formAction += "&sp=CAASAhAB";
this.submit();
})
或者基于这个其他的稍微变体Stack Overflow 答案
document.querySelector("#search-form").addEventListener("submit", function(e) {
e.preventDefault();
button.setAttribute("action", this.getAttribute("action") += "&sp=CAASAhAB");
// Or try this instead of the above.
// this.action += "&sp=CAASAhAB";
this.submit();
});