Comments by George on scripts

1 comment

Comment on:
Google Image Type Recognition

Jul 9, 2007

Very cool. I wonder why Google hasn't modified their page themselves.

Here is a version that also offers the image type choice at the opening page of Google Image Search.

// ==UserScript==
// @name            Google Image Type Recognition
// @namespace       http://loucypher.wordpress.com/
// @include         http://images.google.*/*
// @description     Adds image type selections to Google Image search
// ==/UserScript==

// Last updated: 2007-07-09

(function() {
  var doc = typeof unsafeWindow == "object" ? unsafeWindow.document : document;
  var form = doc.gs;
  if (!form) {
      modifyFrontDoor();
      return;
  }

  var yellowbar = document.evaluate("//span[contains(@style, " +
                                    "'rgb(255, 255, 153)')]/parent::center",
                                    document, null, 0, null).iterateNext();
  if (yellowbar) yellowbar.parentNode.removeChild(yellowbar);

  var imgtype;
  if (form.imgtype != undefined) {
    imgtype = form.imgtype.value;
  } else {
    try {
      imgtype = location.href.match(/imgtype\=(\w+)/)[1];
    } catch(ex) {
      imgtype = "";
    }
    var inp = form.appendChild(document.createElement("input"));
    inp.type = "hidden";
    inp.name = "imgtype";
    inp.value = imgtype;
  }

  var elm = document.getElementsByName("imagesize")[0] ||
            document.getElementById("sd");
  if (!elm) return;
  var bluebar = elm.parentNode;

  var menu = makeMenu(form.hl.value);
  bluebar.appendChild(menu);
  menu.style.margin = "2px 0pt";
  menu.addEventListener("change", function(e) {
    form.imgtype.value = this.options[this.selectedIndex].value;
    form.submit();
  }, false);

  function makeMenu(language)
  {
      var menu = document.createElement('select');
      menu.name = "imgtype";

      var text;

      switch (language) {
        case "id":  // Indonesian
        case "ms":  // Malay; merge with ID til I get the translations
          text = ["Semua jenis gambar", "Wajah", "Berita"];
          break;

        case "jw":  // Javanese
          text = ["Sedoyo jenis gambar", "Wajah", "Kabar"];
          break;

        case "su":  // Sundanese
          text = ["Sakab\u00e9h jenis gambar", "Muka", "Beja"];
          break;

        case "nl":  // Dutch; contrib. by Deathstar-NL
          text = ["Alle afbeelding types", "Gezichten", "Nieuws"];
          break;

        case "fr":  // French; contrib. by Mortimer
          text = ["Tous les types d\'image", "Figures", "Actualit�s"];
          break;

        case "de":  // German
          text = ["alle Bildarten", "Gesichter", "Nachrichten"];
          break;

        case "it":  // Italian
          text = ["Tutti i tipi di immagine", "Facce", "Notizie"];
          break;

        case "eu":  // Basque; -----+
        case "ca":  // Catalan;     +-- merge with ES til I get the translations
        case "gl":  // Galician; ---+
        case "es":  // Spanish
          text = ["Todos los tipos de la imagen", "Caras", "Noticias"];
          break;

        case "pt-BR":  // Portuguese (Brazil); contrib. by Bruno 'ReX' Barbieri
          text = ["Todos tipos de imagem", "Rostos", "Not�cias"];
          break;

        case "pt-PT":  // Portuguese (Portugal); contrib. by vOidSenses
          text = ["Todos os tipos da imagem", "Caras", "Noticia"];
          break;

        case "xx-elmer":  // Elmer Fudd; contrib. by himself
          text = ["Aww image types", "Faces", "News"];
          break;

        case "xx-piglatin":  // Pig Latin; contrib. by Arcusmay Ulliustay Icerocay
          text = ["Allay imageway ypestay", "Acesfay", "Ewsnay"];
          break;

        case "xx-hacker":  // Hacker; contrib. by 4(1|)|3|_||2|\|
          text = ["4|| 1/\\/\\463 +yP3z", "P|-|4(3z", "|\\|3\\/\\/5"];
          break;

        case "th":  // Thai; contrib. by tz
          text = ["\u0e17\u0e38\u0e01\u0e23\u0e39\u0e1b\u0e20\u0e32\u0e1e",
                  "\u0e43\u0e1a\u0e2b\u0e19\u0e49\u0e32",
                  "\u0e02\u0e48\u0e32\u0e27"];
          break;

        case "fi":  // Finnish; contrib. by kenmooda
          text = ["Kaikki kuvatyypit", "Kasvokuvat", "Uutiskuvat"];
          break;

        case "af":  // Afrikaans
        case "sq":  // Albanian
        case "am":  // Amharic
        case "ar":  // Arabic
        case "hy":  // Armenian
        case "az":  // Azerbaijani
        case "be":  // Belarusian
        case "bn":  // Bengali
        case "bh":  // Bihari
        case "xx-bork":  // Bork, bork, bork!
        case "bs":  // Bosnian
        case "br":  // Breton
        case "bg":  // Bulgarian
        case "km":  // Cambodian
        case "zh-CN":  // Chinese (Simplified)
        case "zh-TW":  // Chinese (Traditional)
        case "co":  // Corsican
        case "hr":  // Croatian
        case "cs":  // Czech
        case "da":  // Danish
        case "eo":  // Esperanto
        case "et":  // Estonian
        case "fo":  // Faroese
        case "tl":  // Filipino
        case "fy":  // Frisian
        case "ka":  // Georgian
        case "el":  // Greek
        case "gn":  // Guarani
        case "gu":  // Gujarati
        case "iw":  // Hebrew
        case "hi":  // Hindi
        case "hu":  // Hungarian
        case "is":  // Icelandic
        case "ia":  // Interlingua
        case "ga":  // Irish
        case "ja":  // Japanese
        case "kn":  // Kannada
        case "kk":  // Kazakh
        case "xx-klingon":  // Klingon
        case "ko":  // Korean
        case "ku":  // Kurdish
        case "ky":  // Kyrgyz
        case "lo":  // Laothian
        case "la":  // Latin
        case "lv":  // Latvian
        case "ln":  // Lingala
        case "lt":  // Lithuanian
        case "mk":  // Macedonian
        case "ml":  // Malayalam
        case "mt":  // Maltese
        case "mr":  // Marathi
        case "mo":  // Moldavian
        case "mn":  // Mongolian
        case "ne":  // Nepali
        case "no":  // Norwegian
        case "nn":  // Norwegian (Nynorsk)
        case "oc":  // Occitan
        case "or":  // Oriya
        case "ps":  // Pashto
        case "fa":  // Persian
        case "pl":  // Polish
        case "pa":  // Punjabi
        case "qu":  // Quechua
        case "ro":  // Romanian
        case "rm":  // Romansh
        case "ru":  // Russian
        case "gd":  // Scots Gaelic
        case "sr":  // Serbian
        case "sh":  // Serbo-Croatian
        case "st":  // Sesotho
        case "sn":  // Shona
        case "sd":  // Sindhi
        case "si":  // Sinhalese
        case "sk":  // Slovak
        case "sl":  // Slovenian
        case "so":  // Somali
        case "sw":  // Swahili
        case "sv":  // Swedish
        case "tg":  // Tajik
        case "ta":  // Tamil
        case "tt":  // Tatar
        case "te":  // Telugu
        case "ti":  // Tigrinya
        case "to":  // Tonga
        case "tr":  // Turkish
        case "tk":  // Turkmen
        case "tw":  // Twi
        case "ug":  // Uighur
        case "uk":  // Ukrainian
        case "ur":  // Urdu
        case "uz":  // Uzbek
        case "vi":  // Vietnamese
        case "cy":  // Welsh
        case "xh":  // Xhosa
        case "yi":  // Yiddish
        case "yo":  // Yoruba
        case "zu":  // Zulu
        case "en":  // English
        default:
          text = ["All image types", "Faces", "News"];
      }

      var opt, values = ["", "face", "news"];

      for (var i in values) {
        opt = menu.appendChild(document.createElement("option"));
        opt.appendChild(document.createTextNode(text[i]));
        opt.value = values[i];
        if (values[i] == imgtype) {
          opt.selected = true;
        }
      }
      return menu;
  }

  function modifyFrontDoor()
  {
      var form = doc.f;
      if (!form)
          return;
      var button = form.btnG;
      if (!button)
          return;
      var menu = makeMenu(form.hl.value);
      menu.style.marginRight = '3px';
      button.parentNode.insertBefore(menu, button);
  }

})()