Discussions » Demandes de création de scripts
Youtube searches auto filtered by upload date?
cobble? I guess you meant code hah..
You will need a code like this
https://greasyfork.org/en/scripts/412015-force-https/code
So basically this should work
if (location.href.match('&sp=CAI%253D') === null)
{
location.href = location.href += '&sp=CAI%253D';
}
No, cobble: https://www.merriam-webster.com/dictionary/cobble
I have zero coding background but sometimes headbutt my way by copying bits of different scripts and frankensteining something together.
I feel I may now be closer thanks to your help but something is still missing. Tampermonkey doesn't even recognize the script when I'm in Youtube.
// ==UserScript==
// @name Force HTTPS
// @namespace ghostrider47
// @version 0.2
// @description Force HTTP to HTTPS
// @author ghostrider7
// @match *:www.youtube.com/results?search_query*
// @grant none
// ==/UserScript==
// @run-at document-start
// @run-at document-end
// @run-at document-idle
document.location = document.URL.replace('https://www.youtube.com/results?search_query','https://');
if (location.href.match('&sp=CAI%253D') === null)
{
location.href = location.href += '&sp=CAI%253D';
}
Better use something like this: https://chrome.google.com/webstore/detail/redirect-url-modify-heade/mdnleldcmiljblolnjhpnblkcekpdkpa
// ==UserScript==
// @name Add '&sp=CAI%253D' to the end of YT url
// @namespace hacker09
// @version 1
// @description Adds '&sp=CAI%253D' to the end of YT url
// @author hacker09
// @include https://www.youtube.com/results?search_query*
// @run-at document-start
// @grant none
// ==/UserScript==
if (location.href.match('&sp=CAI%253D') === null)
{
location.href = location.href += '&sp=CAI%253D';
}
You have to reload the page after searching for a term
Better use something like this: https://chrome.google.com/webstore/detail/redirect-url-modify-heade/mdnleldcmiljblolnjhpnblkcekpdkpa
That looks pretty close to what I wanted, but I had to get a Firefox version 'Redirector'. I'm not sure how to respect my search terms while still redirecting. For example 'https://www.youtube.com/results?search_query=test' and then keep the 'test' term but add the filter term '&sp=CAI%253D' just to the end 'https://www.youtube.com/results?search_query=test&sp=CAI%253D'.
After headbutting it for a while I eventually figured it simply was not working at all when I did a simple test with https://greasyfork.org/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBejlYQVE9PSIsImV4cCI6bnVsbCwicHVyIjoiYmxvYl9pZCJ9fQ==--608ceffa87951de8e49a85bd87f924a5206ce3a9/image.png?locale=en and it did nothing, even refreshing the page.
// ==UserScript==
// @name Add '&sp=CAI%253D' to the end of YT url
// @namespace hacker09
// @version 1
// @description Adds '&sp=CAI%253D' to the end of YT url
// @author hacker09
// @include https://www.youtube.com/results?search_query*
// @run-at document-start
// @grant none
// ==/UserScript==
if (location.href.match('&sp=CAI%253D') === null)
{
location.href = location.href += '&sp=CAI%253D';
}
I'm not sure what I am doing wrong, but creating a new script in Tamper Monkey, pasting the script you've provided, and then refreshing, shows no script active in Youtube.
Am I missing something obvious?
That extension will unlikely work...
Open www.youtube.com
Search for something
Reload the page
Now you will be able to see that the script is active.
You need to search for key up or key down event listeners to detect when you press enter to search for something. Or search for onclick event listener to work when you click on the search button.
Mutation observers would also work, so that you don't have to reload the page after searching for something...
Youtube. Search for something.
https://i.imgur.com/TxAO6S7.png
https://i.imgur.com/BQjx8jC.png
Hitting F5.
https://i.imgur.com/cROcWOA.png
https://i.imgur.com/pqEps0B.png
Weird, it worked for me
You could try changing
// @include https://www.youtube.com/results?search_query*
To
// @include https://www.youtube.com/results?search_query=*
That worked, w00t! Now all I need is to automate the refresh. Could it be tied to pressing enter while inside Youtube? After googling and running around I've found window.location.reload(); but it only works once I have refreshed the page.
I am really close now but it's been,,, ::checks time:: ...three hours, holy crap.
Modified slightly your script since it wanted a @match (apparently):
// ==UserScript==
// @name Add '&sp=CAI%253D' to the end of YT url
// @namespace hacker09
// @version 1
// @description Adds '&sp=CAI%253D' to the end of YT url
// @author hacker09
// @include https://www.youtube.com/results?search_query=*
// @match https://www.youtube.com/results?search_query=*
// @run-at document-start
// @grant none
// ==/UserScript==
if (location.href.match('&sp=CAI%253D') === null)
{
location.href = location.href += '&sp=CAI%253D';
}
And then I got a second script I've frankensteined from other scripts: // ==UserScript==
// @name Reload test
// @namespace hacker09
// @version 1
// @description Reload test
// @author hacker09
// @include https://www.youtube.com/results?search_query=*
// @match https://www.youtube.com/
// @run-at document-start
// @grant none
// ==/UserScript==
(function() {
'use strict';
function onReload() {window.location.reload();
}
function onKeydown(evt) {
// Use https://keycode.info/ to get keys
const ke = new KeyboardEvent("keydown", {
bubbles: true, cancelable: true, keyCode: 13
});
if (evt.keyCode == 13) {
onReload();
}
}
document.addEventListener('keydown', onKeydown, true);
})();
It's -almost- there but requires me to search twice in a row for the scripts to activate. I haven't been able to cobble them together but after three hours I'm this close to just having autohotkey refresh youtube on enter press so your script activates.
Could it be tied to pressing enter while inside Youtube?
yes,search for key up or key down event listeners to detect when you press enter to search for something. Or search for onclick event listener to work when you click on the search button.
Since match worked, remove // @include https://www.youtube.com/results?search_query=*
My new code works but just one time, so only a mutation observer would probably be able to solve that
// ==UserScript==
// @name Add '&sp=CAI%253D' to the end of YT url
// @namespace hacker09
// @version 1
// @description Adds '&sp=CAI%253D' to the end of YT url
// @author hacker09
// @match https://www.youtube.com*
// @run-at document-start
// @grant none
// ==/UserScript==
document.querySelectorAll("#search")[2].addEventListener("keydown", function(e) { //When the user press Enter on the search box
//document.querySelector("#search-icon-legacy").addEventListener("click", function(e) {
if (e.keyCode === 13) { //If the enter key was pressed
location.href = 'https://www.youtube.com/results?search_query=' + document.querySelectorAll("#search")[2].value + '&sp=CAI%253D';
} //Finishes the if condition
}) //Finishes the keyup event listener
I had to get a Firefox version 'Redirector'
But why?
Not on my end unfortunately.
I enter Youtube: https://i.imgur.com/eprMnVy.png
I search something: https://i.imgur.com/JV1RrQC.png but no appending.
The good news is that the @match makes the script be recognized without a refresh which was required with the original script: https://i.imgur.com/fijfsWU.png
Hit refresh: https://i.imgur.com/YBV3Og1.png No appending unlike the original script which did do that part.
The original and the cobbled version did work. All it needed was to send the keypress (so that the search happened and not just reload before the search happened) and then do a page refresh to force the appending.
I had to get a Firefox version 'Redirector'
But why?
Because the one linked was for Chrome.
From what I figure the only thing I needed was putting the function
onReload() {window.location.reload();
under
const ke = new KeyboardEvent("keydown", {bubbles: true, cancelable: true, keyCode: 13
so that the script sends the keypress (activating the youtube search) before loading the rest of the script (reloading the page). No matter the combination of symbols each time I put the window.location.reload under the keydown bit there is always an error.
Bump.
Maybe instead of redirecting the url, using .click() to click on the button that filters by upload date would be a better solution that works better?
Maybe instead of redirecting the url, using .click() to click on the button that filters by upload date would be a better solution that works better?
I've tried to explain I have zero background in coding. At most I can find other scripts and then copy bits and try to make it work.
Anyway, someone helped over Reddit and I will post what they did so it can help others who want a fix like I did: https://www.reddit.com/r/GreaseMonkey/comments/wv305n/a_way_to_auto_sort_youtube_searches_by_date/
I mean that maybe instead of playing with the url we could just click on the upload by date button under the filter settings
document.querySelector("ytd-search-filter-group-renderer:nth-child(5) > ytd-search-filter-renderer:nth-child(4)").click();
Your solution is an mutation observer, not an event listener, event listeners will work once, then not again requiring you to reload each time you search 2 or more times.
Searching on YT does not reload the page, what does not run the script again, that's why the script only runs and works once, but a mutation observer should fix that...
This is a good example of a mutation observer usage https://greasyfork.org/en/scripts/418312-pre%C3%A7o-total-mercado-livre/code
// ==UserScript==
// @name Add '&sp=CAI%253D' to the end of YT url (Final working version)
// @namespace hacker09
// @version 2
// @description Adds '&sp=CAI%253D' to the end of YT url
// @author hacker09
// @include https://www.youtube.com*
// @run-at document-end
// @grant none
// ==/UserScript==
new MutationObserver(async function() { //Starts the MutationObserver async function
document.querySelectorAll("#search")[2].addEventListener("keydown", function(e) { //When the user press Enter on the search box
if (e.keyCode === 13) { //If the enter key was pressed
location.href = 'https://www.youtube.com/results?search_query=' + document.querySelectorAll("#search")[2].value + '&sp=CAI%253D';
} //Finishes the if condition
}) //Finishes the keyup event listener
}).observe(document.body, { //Defines the element and the characteristics to be observed
attributes: true,
attributeOldValue: true,
characterData: true,
characterDataOldValue: true,
childList: true,
subtree: true
}); //Finishes the definicions to be observed
I think it will be helpful for me. Because I want to try to develop my channel. And now there are more questions than answers. But, I will learn more about American roulette here https://casinohex.org/roulette/american-roulette-rtg/ and I will use it.
I've been trying to cobble something together but my lack of any sort of coding background is holding me back.
The only thing that changing the filter in the Youtube search does is adding '&sp=CAI%253D' at the end of the URL and then refreshing the page