honjarake blog

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

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

greasemonkey 自分用ソースコードまとめ 長いので分割

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

innerHTML追加後にイベントを追加するサンプル

// ==UserScript==
// @name            test item
// @namespace       http://espion.just-size.jp/archives/05/136155838.html
// @description     code test
// @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="lbOptAny">label_1:</label><input type="text" id="tbOptAny"></input><input type="button" id="bOptAny" value="any" ></input>';
  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><label>label_4:</label><input type="text" id="tbD"></input><input type="button"></input>';
  t_code += '</ul>';
  //----------------------------------------
  m_div.innerHTML += t_code;
  //----------------------------------------
  var m_list = m_div.getElementsByTagName('li');
  //---------------------------------------
  var bOptAny = document.getElementById('bOptAny');
  bOptAny.value = '検索';
  var lbOptAny = document.getElementById('lbOptAny');
  lbOptAny.innerHTML = 'twitter:';
  //----------------------------------------
  bOptAny.onclick = function onclick_any() {
    var tbOptAny = document.getElementById('tbOptAny');
    if (tbOptAny.value != '') {
      window.open('http://t-proj.com/twitter/?q=' + tbOptAny.value);
    }
  };
  //---------------------------------------
  var tb = document.getElementById('tbOptAny');
  //focus
  tb.onfocus = function () {
    var b = document.getElementById('m_div');
    b.style.left = 0;
  };
  //outfocus
  tb.onblur = function () {
    var b = document.getElementById('m_div');
    if (b.style.removeProperty) {
      b.style.removeProperty('left');
    }
    if (b.style.removeAttribute) {
      b.style.removeAttribute('left');
    }
  };
  //----------------------------------------
  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);
    }
  };
  var tbc = document.getElementById('tbC');
  tbc.onfocus = function () {
    var b = document.getElementById('m_div');
    b.style.left = 0;
  };
  tbc.onblur = function () {
    var b = document.getElementById('m_div');
    if (b.style.removeProperty) {
      b.style.removeProperty('left');
    }
    if (b.style.removeAttribute) {
      b.style.removeAttribute('left');
    }
  };
}) ();

相変わらずのデバックそのまま状態

ヤクテナ(海外の反応アンテナ)NGサイト機能サンプル

// ==UserScript==
// @name        yakutena helper
// @namespace   http://honjarake.hatenablog.jp/
// @description ヤクテナにNGサイト機能を付加
// @include     http://www.yakutena.com/*
// @version     1
// @grant       none
// ==/UserScript==
var LIST;
var DEBUG;
console.log('start');

if (typeof DEBUG != 'undifined') {
    LIST = 'カイカイ反応通信'; //恨みはないんやで
    console.log('DEBUG_MODE : ' + LIST);
}

var init = function () {
    console.log(' init start');
    
    //var list = document.getElementById('antena-topics');
    var list = document.getElementsByClassName('listview');
    
    var col = list[0].getElementsByTagName('tr');
    console.log(' col search start');
    
    for (var i = 0; i < col.length; i++) {
        //console.log(' col[ ' + i + ']:' + col[i].innerHTML);
        var flg = false;
        var txt = '';
/*        
        var dat = col[i].getElementsByTagName('td');
        for (var j = 0; j < dat.length; j++) {
            if (typeof dat[j] != 'undefined') {
                txt = dat[j].innerHTML;
                //console.log(' col[ ' + i + ']dat[ ' + j + ']:' + txt);
                if (txt.indexOf(LIST) > - 1) flg = true;
            }
        }
*/
        //console.log(' site name search');
        var site = col[i].getElementsByClassName('site');
        //console.log(site);
        
        if (site.length > 0){
            txt = site[0].innerHTML;
            //console.log(' col[ ' + i + '].site : ' + txt);
            //chaeck : site name
            (txt.indexOf(LIST) > - 1) ? flg = true : flg = false ;
        } 
        
        if (flg == true) {
            console.log('  col ' + i + ' is hidden_column');
            col[i].style.display = 'none';
        }
    }
    console.log(' col search end');
    console.log(' init end');
}
// start function

init();
console.log('end');

LISTをどうやって保存しておくか考えたらやる気減
カンマ区切りsetValueか…で、splitして、ループで…
エスケープ文字列変換とか…ぐわあー!