Fill YouTube Search Box

If YouTube search box is empty, check for query text in the page URL and fill it. v0.1 2018-09-16

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        Fill YouTube Search Box
// @author      Jefferson "jscher2000" Scher
// @namespace   JeffersonScher
// @version     0.1
// @copyright   Copyright 2018 Jefferson Scher
// @license     BSD-3-Clause
// @description If YouTube search box is empty, check for query text in the page URL and fill it. v0.1 2018-09-16
// @match       https://www.youtube.com/*
// ==/UserScript==

function fillQuery(frmctrl){
  // Is there a query in the URL?
  if (location.search.indexOf('search_query=') < 1) return;
  // Fetch the parameters and create an array of them
  var parms = location.search.substr(1).split("&");
  var qtext = '';
  for (var j=0; j<parms.length; j++){
    if (parms[j].indexOf('search_query=') === 0) {
      // Grab what follows the = sign
      qtext = parms[j].split('=')[1];
      // Decode from URL-safe back to normal text
      qtext = decodeURIComponent(qtext);
      // Turn + back into a space
      qtext = qtext.replace(/\+/g, ' ');
      // We're done with the parms array
      break;
    }
  }
  // Stop if the query string is blank
  if (qtext.length < 1) return;
  // Insert query text into the search box
  frmctrl.value = qtext;
}

// Check for a blank search box and call function to fill it -- do we need to wait for any reason?
var ytsearchbox = document.querySelector('input#search');
if (ytsearchbox) {
  if (ytsearchbox.value == ''){
    fillQuery(ytsearchbox);
  } else {
    console.log('Fill YouTube Search Box says: search box isn\'t empty, so not messing');
  }
} else {
  // WTF, no search box?
  console.log('Fill YouTube Search Box says: no search box found on this page');
}