Hatena Bookmark Users Filter

はてなブックマークの検索結果ページで、ブックマーク数によるフィルタリング選択肢を改変します。

Ekde 2020/02/04. Vidu La ĝisdata versio.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Hatena Bookmark Users Filter
// @description はてなブックマークの検索結果ページで、ブックマーク数によるフィルタリング選択肢を改変します。
// @namespace   knoa.jp
// @include     https://b.hatena.ne.jp/search/*
// @version     1.0.1
// @grant       none
// ==/UserScript==

(function(){
  const COUNTS = [1, 3, 10, 30, 100, 300, 1000];
  const DEFAULT = 3;
  let current = location.href.includes('&users=') ? parseInt(location.href.match(/&users=([0-9]*)/)[1]) : DEFAULT;
  let filterH3s = document.querySelectorAll('.left-container h3');
  let countsH3 = Array.from(filterH3s).find(h3 => h3.textContent === 'ブックマーク数');
  if(countsH3 === undefined) return console.log('Not found H3.');
  let countsUl = countsH3.parentNode.querySelector('ul');
  let countsLis = countsUl.querySelectorAll('ul > li');
  while(countsUl.children.length > 1) countsUl.removeChild(countsUl.lastElementChild);
  COUNTS.forEach(c => {
    if(c === 1) return;
    let li = countsLis[0].cloneNode(true);
    let a = li.querySelector('a');
    if(a === null) return console.log('Not found a.');
    if(c === current) a.classList.add('is-current');
    a.href = a.href.replace(/(&users)=1$/, '$1=' + c);
    a.textContent = c + ' users';
    countsUl.appendChild(li);
  });
})();