raingart / Google search menu replace "Videos" tab to "YouTube"

// ==UserScript==
// @name         Google search menu replace "Videos" tab to "YouTube"
// @namespace    google-search-yt-tab
// @version      0.5
// @description  Link the video in the google search menu to the YouTube link.
// @author       raingart
// @include      http*://www.google.*/search?*
// @run-at       document-start
// @license      Apache-2.0
// ==/UserScript==
/*jshint esversion: 6 */

// fix TrustedHTML error
if(new URLSearchParams(location.search).get('tbm') == 'isch' // img page
   && typeof window.isSecureContext !== 'undefined' && window.isSecureContext
   && window.trustedTypes && window.trustedTypes.createPolicy
   && !trustedTypes.defaultPolicy )
   {
      try {
        window.trustedTypes.createPolicy('default', {createHTML: str => str, createScript: str => str, createScriptURL: str => str});
      } catch(e) { }
   } 

window.addEventListener('load', () => {
   const
      getQueryURL = query => new URLSearchParams(location.search).get(query),
      query = document.querySelector('input[name="q"]').value, // Google search input textbox
      q = query ? encodeURIComponent(query) : getQueryURL('q');

   // add new menu
   if (q && (el = document.getElementById('hdtb-tls'))) {
      el.style.marginRight = 0; // fix
      el.insertAdjacentHTML('beforebegin',
         `<div class="hdtb-mitem hdtb-imb">
            <a href="https://www.youtube.com/results?search_query=${q}">
            <span style="height:16px; width:16px; display: inline-block; fill: currentColor; margin-right: 5px; vertical-align: text-bottom;">
               <svg version="1.1" viewBox="0 0 209.673 209.673">
                  <g>
                     <path d="M173.075,29.203H36.599C16.418,29.203,0,45.626,0,65.812v78.05c0,20.186,16.418,36.608,36.599,36.608h136.477 c20.18,0,36.598-16.422,36.598-36.608v-78.05C209.673,45.626,193.255,29.203,173.075,29.203z M194.673,143.861 c0,11.915-9.689,21.608-21.598,21.608H36.599c-11.91,0-21.599-9.693-21.599-21.608v-78.05c0-11.915,9.689-21.608,21.599-21.608 h136.477c11.909,0,21.598,9.693,21.598,21.608V143.861z" />
                     <path d="M145.095,98.57L89.499,61.92c-2.303-1.519-5.254-1.649-7.684-0.342c-2.429,1.308-3.944,3.845-3.944,6.604v73.309 c0,2.759,1.515,5.295,3.944,6.604c1.113,0.6,2.336,0.896,3.555,0.896c1.442,0,2.881-0.415,4.129-1.239l55.596-36.659 c2.105-1.388,3.372-3.74,3.372-6.262C148.467,102.31,147.2,99.958,145.095,98.57z M92.871,127.562V82.109l34.471,22.723 L92.871,127.562z" />
                  </g>
               </svg>
            </span>
            YouTube</a>
         </div>`);
   } else if (q && (link = document.querySelector('a[href*="tbm=vid"]'))) {
      // replace menu "Videos" tab to "YouTube"
      link.href = 'https://www.youtube.com/results?search_query=' + q;
      try {
        // error: This document requires 'TrustedHTML' assignment.
        link.innerHTML = link.innerHTML.replace(link.textContent, 'YouTube'); // with icon
      } catch(e) {
        link.textContent = 'YouTube'; // without icon
      }
   }
});