lednerg

Search YouTube and More From Within Google

Apr 22nd, 2023 (edited)
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.96 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name            Search YouTube and More From Within Google
  3. // @namespace       lednerg
  4. // @author          lednerg
  5. // @license         MIT
  6. // @description     Adds icons next to Google's search bar which let you search from other sites.
  7. // @version         2023.4.22
  8. // @include         /https?:\/\/(www\.)?google\.(com|(?:com?\.)?\w\w)\/.*/
  9. // @run-at          document-end
  10. // @supportURL      https://pastebin.com/D1PnSq6M
  11. // ==/UserScript==
  12.  
  13. /* I am not providing any kind of help or support for this script at this time.
  14.  * I've tested it for a week in Chrome and Firefox and it seems to be fine, but your mileage may vary.
  15.  * If it stopped working, then Google probably changed something about their website some time after this was written.
  16.  * EDIT April 22 2023: No longer using GM_AddStyle, which I hear doesn't work for Greasemonkey anymore. Also added a background for
  17.  * the button container for whenever Google does their "Google doodle" things. The search form on the Google homepage is now centered. */
  18.  
  19. /* You can change the buttons as you like below. The only caveat is that the search terms need to be the very last part of the URL.
  20.  * The domains are separate from the rest so that when there's no search query entered, the buttons will point to the domains.
  21.  * I'm using favicons, which maybe isn't the best idea, but I don't feel like embedding images. That would work, though. */
  22. const newButtonsList = [
  23.     [ 'YouTube', 'https://www.youtube.com/', 'results?search_query=', 'https://www.youtube.com/favicon.ico'],
  24.     [ 'YouTube Music', 'https://music.youtube.com/', 'search?q=', 'https://music.youtube.com/favicon.ico'],
  25.     [ 'The Movie Database', 'https://www.themoviedb.org/', 'search?query=', 'https://www.themoviedb.org/favicon.ico'],
  26.     [ 'Just Watch', 'https://www.justwatch.com/', 'us/search?q=', 'https://www.justwatch.com/favicon.ico'],
  27.     [ 'Thesaurus', 'https://www.thesaurus.com/', 'browse/', 'https://www.thesaurus.com/assets/favicon-tcom-dae75822a91aa45e01d52041b4baf101.png'],
  28.     [ 'Wolfram Alpha', 'https://www.wolframalpha.com/', 'input/?i=', 'https://www.wolframalpha.com/favicon.ico'],
  29.     [ 'Bing', 'https://www.bing.com/', 'search?q=', 'https://www.bing.com/favicon.ico'],
  30.     [ 'Twitter', 'https://twitter.com/', 'search?q=', 'https://www.twitter.com/favicon.ico']
  31. ]    /* [0] = Title,  [1] = Domain,  [2] = Search URL,  [3] = Image URL */
  32.  
  33. let searchForm = document.querySelector('*[name="q"]');
  34. let searchTerms = searchForm.value;
  35.  
  36. if (searchForm) {
  37.     searchForm.addEventListener('input', function(event) {
  38.         searchTerms = event.target.value;
  39.         letsRock(true);
  40.     });
  41. }
  42.  
  43. let newButtonsHTML = ''
  44.  
  45. function makeButtonsHTML() {
  46.     newButtonsHTML = '<div class="customSearch">';
  47.     for ( var i = 0; i < newButtonsList.length; i++ ) {
  48.         newButtonsHTML = newButtonsHTML.concat( '<a title="', newButtonsList[i][0], '" class="customSearchItem" href="', newButtonsList[i][1] );
  49.         if (searchTerms) { newButtonsHTML = newButtonsHTML.concat( newButtonsList[i][2], encodeURIComponent(searchTerms) ) }
  50.         newButtonsHTML = newButtonsHTML.concat( '" target="_self"><span><img src="', newButtonsList[i][3], '" /></span></a>');
  51.     }
  52.     newButtonsHTML = newButtonsHTML.concat( '</div>' );
  53. }
  54.  
  55. function letsRock(update) {
  56.     var container = document.querySelector('.customSearch');
  57.     if (!update && container ) { return }
  58.     makeButtonsHTML();
  59.     var insertHere = document.querySelector('button[aria-label="Search"]') || document.querySelector('button[aria-label="Google Search"]') || document.querySelector('div[aria-label="Search by image"]');
  60.     /* First one is for most results pages such as All, News, Videos, Books, etc. Second one is for the Images results page. Third one is for Google's home page. */
  61.     if (container) { container.remove() }
  62.     insertHere.insertAdjacentHTML('afterend', newButtonsHTML);
  63. }
  64.  
  65. const bodyColor = window.getComputedStyle(document.querySelector('body')).backgroundColor;
  66.  
  67. const buttonWidth = Math.ceil(newButtonsList.length / 2) * 16;
  68.  
  69. document.body.appendChild(document.createElement('style')).textContent = `
  70.     .customSearch {
  71.         position: relative;
  72.         left: 10px;
  73.         display: flex;
  74.         flex-flow: column wrap;
  75.         align-items: center;
  76.         justify-content: space-around;
  77.         gap: 4px;
  78.         height: 44px;
  79.         width: 0;
  80.         padding: 0;
  81.     }
  82.     .customSearchItem:hover {
  83.         filter: drop-shadow(1px 1px 1px #000)
  84.             drop-shadow(0px 0px 2px #8b9ba1)
  85.             drop-shadow(0px 0px 2px #8b9ba1)
  86.             drop-shadow(0px 0px 2px #8b9ba1)
  87.             drop-shadow(0px 0px 2px #8b9ba1);
  88.     }
  89.     .customSearchItem {
  90.         display: flex;
  91.         height: 18px;
  92.         width: 18px;
  93.         padding: 1px 5px;
  94.         margin: 0px;
  95.     }
  96.     .customSearchItem svg,
  97.     .customSearchItem img,
  98.     .customSearchItem > span {
  99.         height: 16px;
  100.         width: 16px;
  101.     }
  102.     /* .minidiv = when search box is fixed to the top after scrolling down  */
  103.     .minidiv .RNNXgb {
  104.         margin-top: 10px !important;
  105.         height: 32px !important;
  106.     }
  107.     .minidiv .customSearch {
  108.         margin: -6px 0;
  109.     }
  110.     /* for Google.com home page */
  111.     div[aria-label="Search by image"] + .customSearch {
  112.         left: 20px;
  113.     }
  114.     /* centering on home page  */
  115.     .o3j99.ikrT4e.om7nvf .A8SBwf[jscontroller="cnjECf"] {
  116.         position: relative;
  117.         left: -`+ buttonWidth +`px;
  118.     }
  119.     .o3j99.ikrT4e.om7nvf .FPdoLc.lJ9FBc {
  120.         position: relative;
  121.         left: `+ buttonWidth +`px;
  122.     }
  123.     /* for Google doodle underneath buttons  */
  124.     .customSearch:before {
  125.         z-index: -1;
  126.         position: absolute;
  127.         left: 0;
  128.         content: "";
  129.         background-color: `+ bodyColor +`;
  130.         opacity: .8;
  131.         width: `+ (buttonWidth * 2) +`px;
  132.         height: 55px;
  133.     }
  134.     .minidiv .customSearch:before {
  135.         opacity: 0;    
  136.     }
  137. `;
  138.  
  139. letsRock();
Advertisement
Add Comment
Please, Sign In to add comment