// ? 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-08-17 (v0.7) Added modifications to follow today's change (http://hatena.g.hatena.ne.jp/hatenabookmark/20070817/1187335133).
// 2007-06-08 (v0.6) Added modifications to follow today's change (http://hatena.g.hatena.ne.jp/hatenabookmark/20070608/1181284251).
// 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 LEVELS = [ { name: 'filterNone', path: 'true()', text: '\u3059\u3079\u3066\u306e\u4e00\u89a7' },
                   { name: 'filterNoTag', path: 'descendant::span[@class="user-tag" or @class="comment"]', text: '\u30bf\u30b0+\u30b3\u30e1\u30f3\u30c8\u4e00\u89a7' },
                   { name: 'filterNoComment', path: 'descendant::span[@class="comment"]', text: '\u30b3\u30e1\u30f3\u30c8\u4e00\u89a7'} ];

  const container = document.getElementById('bookmarked_user');
  
  if (!container) {
    return;
  }

  GM_addStyle((function (v) {
    var s = [];
    for (var e in v) {
      s.push('body.' + v[e].name + ' li.' + v[e].name);
      v[e].path = document.createExpression(v[e].path, null)
    }
    return s.join(',');
  })(LEVELS) + '{ display: none; }');

  const optionsUI = document.getElementById('comments').parentNode;
  const bookmarks = container.getElementsByTagName('LI');

  for (var i = 0; i < bookmarks.length; ++i) {
    var e = bookmarks[i];
    var v = [];
    for (var j = 0; j < LEVELS.length; ++j) {
      if (!LEVELS[j].path.evaluate(e, XPathResult.BOOLEAN_TYPE, null).booleanValue) {
        v.push(LEVELS[j].name);
      }
    }
    e.className = v.join(' ');
  }
  
  var initMode = GM_getValue('mode', 0);

  for (var i = 0; i < LEVELS.length; ++i) {
    (function (mode) {
      var input = document.createElement('INPUT');
      input.type = 'radio';
      input.name = 'mode';
      input.checked = mode == initMode;
      input.addEventListener('click', (function (e) {
        if (input.checked) {
          GM_setValue('mode', mode);
          document.body.className = LEVELS[mode].name;
        }
        return arguments.callee;
      })(null), false);
      
      var label = document.createElement('LABEL');
      label.appendChild(input);
      label.appendChild(document.createTextNode(LEVELS[mode].text));
      
      optionsUI.appendChild(label);
    })(i);
  }
})()

