honjarake blog

自分用にテキトーにまとめる

自分用 select option を使った検索ボックス他

greasemonkey 自分用ソースコードまとめ2 - honjarake blog

// ==UserScript==
// @name            test item
// @namespace       http://espion.just-size.jp/archives/05/136155838.html
// @description     code test
// @grant           none
// @include         *
// @exclude         https://apis.google.com/*
// ==/UserScript==
window.load = (function () {
  //----------------------------------------
  //console.log('test item 1 start');
  //----------------------------------------
  if (window != parent) {
    //iframeで埋めこまれた場合の処理
    return;
  }
  var ElemDiv = document.createElement('div');
  //var Docbody = document.body;
  var Docbody = document.getElementsByTagName('body') [0];
  var t_code;
  //===============================================
  var m_div = ElemDiv.cloneNode(true);
  m_div.id = 'm_div';
  Docbody.insertBefore(m_div, Docbody.firstChild);
  t_code = '';
  t_code += '<ul>';
  //t_code += '     <li><label id="lbB">youtube:</label><input type="text" id="tbB"></input><input type="button" id ="bB"></input>';
  t_code += '     <li><label id="lbc">google</label><input type="text" id="tbC"></input><input type="button" id ="bC"></input>';
  t_code += '     <li><select id="sel_s"></select><input type="text" id="tb_sels"></input><input type="button" id="b_sels"></input>';
  t_code += '</ul>';
  //----------------------------------------
  m_div.innerHTML += t_code;
  //----------------------------------------
  var m_list = m_div.getElementsByTagName('li');
  //--------------------------------------------------------------
  var styleTag = document.createElement('style');
  styleTag.type = 'text/css';
  //----------------------------------------
  t_code = '';
  t_code += '#m_div{ position: fixed; z-index: 999; top:300px; width:205px; left:-200px; text-align:left; padding:1px; background: #FFF8DC; font-size:10px;}' + '\n';
  t_code += '#m_div:hover{ position: fixed; z-index: 999; top:300px; left:0px; padding:1px; background: #dedeff; }' + '\n';
  t_code += '#m_div input[type="text"]{ width:100px !important; margin:2px 2px 5px 5px; }' + '\n';
  //----------------------------------------
  var t_node = document.createTextNode(t_code);
  styleTag.appendChild(t_node);
  //----------------------------------------
  var headerTag = document.getElementsByTagName('head') [0];
  if (!headerTag) alert('header is nothing');
  headerTag.appendChild(styleTag);
  //----------------------------------------
  //console.log('test item 1 end');
  //----------------------------------------
  /*
  var bB = document.getElementById('bB');
  bB.value = 'repeat';
  bB.onclick = function () {
    var tbB = document.getElementById('tbB');
    var re = /http.*:\/\/www.youtube.*\/watch/;
    var found = location.href.match(re);
    if (found) {
      tbB.value = location.href.replace('youtube', 'youtuberepeat');
      document.location = tbB.value;
    } else {
      tbB.value = 'not youtube video';
    }
  }
  */
  var bc = document.getElementById('bC');
  bc.value = 'search';
  bc.onclick = function () {
    var tbc = document.getElementById('tbC');
    if (tbc.value != '') {
      window.open('https://www.google.co.jp/search?q=site:' + document.location.hostname + ' ' + tbC.value);
    }
  };
  // 汎用focus-blur -------------------------
  function tbonfocus() {
    var b = document.getElementById('m_div');
    b.style.left = 0;
  };
  function tbonblur() {
    var b = document.getElementById('m_div');
    if (b.style.removeProperty) {
      b.style.removeProperty('left');
    }
    if (b.style.removeAttribute) {
      b.style.removeAttribute('left');
    }
  };
  // ----------------------------------------
  var tbc = document.getElementById('tbC');
  tbc.onfocus = tbonfocus;
  tbc.onblur = tbonblur;
  // select
  // <select id="sel_s"></select>
  // <input type="text" id="tb_sels"></input>
  // <input type="button" id="b_sels"></input>
  var url = {
    monst: 'http://モンスターストライク.gamewith.jp/search/results?query=',
    twitter: 'http://t-proj.com/twitter/?q=',
  };
  var sels = document.getElementById('sel_s');
  var elopt = document.createElement('option');
  for (key in url) {
    //alert(key + ' : ' + url[key]);
    var opt = elopt.cloneNode();
    opt.id = 'sel_' + key;
    opt.innerHTML = key;
    opt.setAttribute('value', key);
    sels.appendChild(opt);
  }
  var bsels = document.getElementById('b_sels');
  var tbsel = document.getElementById('tb_sels');
  bsels.value = '探す';
  bsels.onclick = function () {
    var tbsel = document.getElementById('tb_sels');
    if (tbsel.value != '') {
      var strSelect = sels.options[sels.selectedIndex].text;
      //alert(url[strSelect]);
      window.open(url[strSelect] + tbsel.value);
    }
  };
  tbsel.onfocus = tbonfocus;
  tbsel.onblur = tbonblur;
  sels.onfocus = tbonfocus;
  sels.onblur = tbonblur;
  if (document.addEventListener) {
    // キーボードを押したときに実行されるイベント
    document.addEventListener('keydown', function (e) {
      var keyID = (e.keyCode)? e.keyCode: e.charCode;
      if (document.activeElement.id == 'tb_sels') {
        if (keyID == 13) {
          bsels.onclick();
        }
      }
    });
  }
  // ----------------------------------------

}) ();