hanzz / Trello search

// ==UserScript==
// @name        Trello search
// @namespace   hanzz
// @description Trello search
// @include     https://trello.com/b/*
// @include     https://trello.com/*
// @version     7
// @grant       none
// ==/UserScript==

//Catch board initialization
addList2Card();


function addList2Card() {
    if (document.getElementById("cardsearch")) {
        setTimeout(function(){
            addList2Card();
        }, 5000);
        return;
    }

    if (document.getElementsByClassName("board-header").length == 0 || document.getElementsByClassName("board-header")[0].children.length != 4) {
        setTimeout(function(){
            addList2Card();
        }, 5000);
        return;
    }
    
    console.log("[Trello-search] activated");
    console.log(document.getElementsByClassName("board-header"));
    console.log(document.getElementsByClassName("board-header")[0].children);

    dummy = document.createElement('div');
    
    dummy.innerHTML = '<form class="board-header-btns mod-left"><input type="text" id="cardsearch" style="width:10em;"></input></form>';
    dummy.onkeypress = function(e){
      if (!e) e = window.event;
      var keyCode = e.keyCode || e.which;
      if (keyCode == '13') {
        var cards = $("span.card-short-id:contains('#" + document.getElementById("cardsearch").value + "')").filter(function(){ return $(this).text() == "#" + document.getElementById("cardsearch").value; });
        if (cards.length != 1) {
          cards = $("span.card-short-id:contains('#" + document.getElementById("cardsearch").value + "')").filter(function(){ return $(this).text() == "#" + document.getElementById("cardsearch").value + " "; });
          if (cards.length != 1) {
             return false;
          }
        }
        console.log("opening card");
        cards[0].click();
        return false;
      }
  }

    console.log("INSERTING");
    document.getElementsByClassName("board-header")[0].insertBefore(dummy, document.getElementsByClassName("board-header")[0].children[3]);    
    console.log(document.getElementsByClassName("board-header")[0].children);
    console.log("INSERTED");
        setTimeout(function(){
            addList2Card();
        }, 5000);
}