Could you make me a Java script bookmark to automatically add site:YouTube.com to google search
Assistant
Certainly! A JavaScript bookmarklet can be created to modify the current Google search query to add the site:youtube.com restriction to it.
Here's a step-by-step guide to creating the bookmarklet:
Open your web browser.
Right-click on your bookmarks bar and select "Add Page", "New Bookmark", or a similar option, depending on the browser you're using. If the bookmarks bar is not visible, you might need to enable it first.
In the Name field, give your bookmarklet a descriptive name, such as "Search YouTube".
In the URL or Location field, you'll input the JavaScript code for the bookmarklet. Copy and paste the following code:
javascript:(function(){
var query = document.querySelector('input[name=q]').value;
var youtubeSearch = query + ' site:youtube.com';
if (!/site:youtube.com/.test(query)) {
document.querySelector('input[name=q]').value = youtubeSearch;
document.forms['tsf'].submit();
}
})();
User
Didn’t do anything
Assistant
I apologize for the inconvenience. Let's troubleshoot the situation. The original code assumes that you're already on a Google search results page with a search box visible and using the form 'tsf', which might not always be the case.
Here's an updated version of the JavaScript bookmarklet that should work more consistently:
javascript:(function(){
var location = window.location.href;
if(location.includes("google.com/search?")){
var query = new URLSearchParams(window.location.search);
var searchTerms = query.get('q');
if(searchTerms && !searchTerms.includes("site:youtube.com")){
query.set('q', searchTerms + " site:youtube.com");
window.location.search = query.toString();
}
} else {
alert('Please use this bookmark on a Google search results page.');
}
})();
User
It’s for safari
Assistant
Safari supports JavaScript bookmarklets, but you may need to ensure that the JavaScript is enabled in your preferences and that your bookmark contains exactly the right code.
Here's a step-by-step guide to adding a JavaScript bookmarklet to Safari on a Mac:
If you prefer a bookmarklet that changes the current URL to a Google search for the same query but specifically on site:youtube.com, here is the adjusted JavaScript code:
javascript:(function()%7B
var url = new URL(window.location.href);
var queryParam = url.searchParams.get('q');
if(queryParam && !queryParam.includes('site:youtube.com')) {
window.location.href = 'https://www.google.com/search?q=' + encodeURIComponent(queryParam + ' site:youtube.com');
} else if (!queryParam) {
alert('No search query found. Please search something on Google first.');
}
%7D)();
To add this as a bookmarklet in Safari:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Save the bookmarklet by clicking "Save" or "Add", depending on the browser.
Now, when you're on google.com and performing a search, you can click the "Search YouTube" bookmarklet in your bookmarks bar, and it will automatically append site:youtube.com to your search query and re-run the search.
Please be advised that this bookmarklet is designed to work when you're already on the Google search results page. If Google updates their page structure or if you use a different language/version of Google, the selectors used in the script (input[name=q] and document.forms['tsf']) may need to be updated.
To use this bookmarklet:
Create a new bookmark in your browser.
Give it a name, for example, "Search YouTube".
Copy and paste the updated code above into the URL or Location field.
Save the bookmark.
Now, when you perform a Google search and want to restrict results to YouTube, click the bookmarklet. This will modify the current URL to include site:youtube.com in the search parameters and reload the page with the filtered results.
It's possible that security settings or updates in browsers may block the execution of JavaScript bookmarklets. If it's still not working, make sure that JavaScript is enabled in your browser and that your browser settings or extensions are not preventing bookmarklet usage. Some browsers like Chrome may also strip out the javascript: prefix when you paste the code, so you might need to type it in manually.
First, make sure your Favorites Bar or Bookmarks Bar is visible in Safari. You can enable it by clicking on View in the menu bar and then selecting Show Favorites Bar.
Control-click (or right-click) on the Favorites Bar and choose Add Bookmark... to add a new bookmark.
Name your bookmarklet something easy to recognize, like "Search YouTube".
In the address field, carefully paste the following JavaScript code:
javascript:(function(){
var location = window.location.href;
if(location.includes("google.com/search?")){
var query = new URLSearchParams(window.location.search);
var searchTerms = query.get('q');
if(searchTerms && !searchTerms.includes("site:youtube.com")){
query.set('q', searchTerms + " site:youtube.com");
window.location.search = query.toString();
}
} else {
alert('Please use this bookmark on a Google search results page.');
}
})();
Ensure you manually type javascript: before pasting the code, as Safari will likely remove it when you paste.
Click Add to save the bookmark to your Favorites Bar.
If these steps don't work, or if you are on iOS, the process will be slightly different. Here's how to do it on an iOS device:
Create a regular bookmark for any page. To do this, navigate to any webpage in Safari, tap the share icon, and tap "Add Bookmark".
Save the bookmark to your favorites and label it "Search YouTube".
Now, tap the bookmarks button (the open book icon), then "Edit".
Choose the "Search YouTube" bookmark you just created.
Delete the URL that's currently in the bookmark and paste the JavaScript code provided above.
After you've added the JavaScript, tap "Done" to save your changes.
Now, you can perform a Google search as usual, and once you are on the search results page, tap the address field to bring up your bookmarks and choose the "Search YouTube" bookmarklet. This will amend the current search to include site:youtube.com in the search and reload the page with the new results.
Make sure your Favorites Bar or Bookmarks Bar is visible in Safari.
Navigate to any website (it doesn't matter which one because you'll be replacing the URL).
Click "Bookmarks" in the menu bar, then select "Add Bookmark..." or use the shortcuts Command (⌘) + D.
In the pop-up window, set the name to "Search YouTube on Google" and choose the Favorites Bar as the location.
Click "Add" to save the bookmark.
Now, go to the Favorites Bar, control-click (or right-click) on the bookmark you just created, and select "Edit Address".
Copy and paste the JavaScript code given above into the address field.
If Safari strips the javascript: prefix when you paste the code, you have to type it in manually before the parentheses. Ensure it's literally typed as javascript: at the beginning of your bookmark address.
Close the bookmarks editor.
Now, when you use the bookmarklet while on a Google search results page, it will modify the URL to display only results from YouTube. Make sure there's a query (q= parameter) in your Google search URL; otherwise, the alert message will be shown.