// ? Bookmark Filter, released under the GPL
// http://www.gnu.org/copyleft/gpl.html
//
// ==UserScript==
// @name           Hatena Bookmark Filter
// @description    Allows users to hide bookmarks without any comments nor tags.
// @include        http://b.hatena.ne.jp/entry/*://*
// ==/UserScript==
//
// 2006-03-20 (v0.3) Added modifications to follow today's change (http://hatena.g.hatena.ne.jp/hatenabookmark/20060320/1142826161).
// 2006-03-17 (v0.2) Added modifications to follow today's change (http://hatena.g.hatena.ne.jp/hatenabookmark/20060317/1142566819).
// 2006-02-16 (v0.1) Initial Version.

(function(){
  const MODE_ALL = 0, MODE_TAG = 1, MODE_COM = 2;
  
  const bookmarks = document.evaluate('/descendant::DIV[@class="bookmarklist"][1]/UL/LI', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  const container = document.evaluate('/descendant::DIV[@class="caption"][1]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  
  if (!container) {
    return;
  }

  var addSelector = function (isVisible, text, mode, checked) {
    var update = function (input) {
      return function (e) {
        if (input.checked) {
          for (var i = 0; i < bookmarks.snapshotLength; ++i) {
            var node = bookmarks.snapshotItem(i);

            node.style.display = isVisible(node) ? 'list-item' : 'none';
          }
          
          GM_setValue('mode', mode);
        }
      }
    };
    
    var input = document.createElement('INPUT');
    input.type = 'radio';
    input.name = 'mode';
    input.checked = checked;
    input.addEventListener('click', update(input), false);

    update(input)(null);
    
    var label = document.createElement('LABEL');
    label.appendChild(input);
    label.appendChild(document.createTextNode(text));
    
    container.appendChild(label);
  }

  var mode = GM_getValue('mode', MODE_ALL);
  
  addSelector(function (node) {
    return true;
  }, '\u3059\u3079\u3066\u306e\u4e00\u89a7', MODE_ALL, mode == MODE_ALL);

  addSelector(function (node) {
    return node.getElementsByTagName('A').length > 2;
  }, '\u30bf\u30b0\u4e00\u89a7', MODE_TAG, mode == MODE_TAG);

  addSelector(function (node) {
    return node.lastChild.nodeValue && node.lastChild.nodeValue.match(/\S/);
  }, '\u30b3\u30e1\u30f3\u30c8\u4e00\u89a7', MODE_COM, mode == MODE_COM);
})()

