// ? 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==
//
// 2007-05-29 (v0.5) Added modifications to follow today's change (http://hatena.g.hatena.ne.jp/hatenabookmark/20070529/1180419718).
// 2006-05-01 (v0.4) Changed some behaviors to allow this tool to work properly with Hatena Comment Linker or on entry pages that have no bookmarks.
// 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 container = document.evaluate('/descendant::DIV[@class="info"]/H2/A[id("comments")]/..', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

  if (!container) {
    return;
  }

  const bookmarks = document.evaluate('following-sibling::UL[1]/LI', container, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

  var initMode = GM_getValue('mode', MODE_ALL);
  
  var addSelector = function (isVisible, text, mode) {
    var input = document.createElement('INPUT');
    input.type = 'radio';
    input.name = 'mode';
    input.checked = mode == initMode;
    input.addEventListener('click', (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);
      }
      
      return arguments.callee;
    })(null), false);

    var label = document.createElement('LABEL');
    label.appendChild(input);
    label.appendChild(document.createTextNode(text));
    
    container.appendChild(label);
  }

  addSelector(function (node) {
    return true;
  }, '\u3059\u3079\u3066\u306e\u4e00\u89a7', MODE_ALL);

  addSelector(function (node) {
    return (node = node.lastChild.previousSibling) && (node.nodeName != 'SPAN' || node.firstChild);
  }, '\u30b3\u30e1\u30f3\u30c8+\u30bf\u30b0\u4e00\u89a7', MODE_TAG);

  addSelector(function (node) {
    return (node = node.lastChild) && (node.nodeName == 'SPAN' && node.firstChild);
  }, '\u30b3\u30e1\u30f3\u30c8\u4e00\u89a7', MODE_COM);
})()

