I’m developping a Chrome Extension that adds new feeds content on Youtube.com
I would like to add new thumbnails links that will open new video but I can’t figure out how can I use their ajax navigation.
As everything is loaded in ajax its not easy to understand how it really works.
Here are the following tests I made
Test 1 : adding a simple a link with /watch?v=2SNThp1YiU4 as href attribute value will load the page without ajax …
Test 2 : in Devtools when I inspect a thumbnail link and change the href attribute, if I click the link the default page will be loaded, not the one I edited which means the video ID was certainly hardcoded in the event listener declaration ?!
So, how can I add custom link to Youtube.com DOM so it uses the proper Youtube ajax navigation ?
Thanks in advance
2
Answers
To add custom links to YouTube and use their AJAX navigation, you need to create links that fit their system. Here are some steps to do this:
Use the YouTube API: Instead of just using regular links, try to use YouTube’s internal functions to create your links. This can help you trigger the same AJAX loading.
Listen for Click Events: Instead of just setting the
href
attribute, you can add a click event listener to your custom thumbnails. Inside this listener, you can use the same methods YouTube uses to change the content without reloading the page.Use
window.history.pushState
: When a link is clicked, you might want to change the URL usingwindow.history.pushState()
for the new video link. This will help simulate the AJAX navigation.Trigger Navigation Functions: Look for existing YouTube functions in the DevTools (like the ones triggered when a thumbnail is clicked) and try to call those functions in your click event handler.
By following these steps, you should be able to create links that utilize YouTube’s AJAX navigation effectively.
I think YouTube uses a specific function to handle navigation generally called
yt.navigate
.Why not generate your custom thumbnail with the appropriate event listener to call the
yt.navigate
function and then trigger the navigation by using theyt.navigate
function to navigate to the new video page.Make sure that your script runs after YouTube’s dynamic content is fully loaded by using appropriate events like yt-page-data-updated.
To integrate custom links on YouTube that use the platform’s AJAX navigation:
Create the Link:
Add JavaScript to Handle the Click: