I want to append this string:
&sp=CAASAhAB
Every time a YouTube search query is performed to remove that stupid “People also watched” section.
I’ve created a Tampermonkey script that works pretty well but does not work when the search is performed by the website’s own search box.
Could anyone help? Solving this would be great for posterity’s sake.
Here’s the TamperMonkey script:
// ==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);
})();
Edit #1: Updated my code based on comments to the question yet there are no significant upgrades, looks like the YouTube POST request is only sent once judging by the network tab.
// ==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();
});
})();