我想附加這個字串:
&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);
})();
如果它不起作用,您可能會像這樣攔截表單提交;基於此的偽代碼堆疊溢位答案:
document.querySelector("#search-form").addEventListener("submit", function(e) {
e.preventDefault()
e.submitter.formAction += "&sp=CAASAhAB";
this.submit();
})
或基於此的一個輕微變體堆疊溢位答案
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();
});