Hatena Bookmark Link Modifier

はてなブックマークの下部の記事の見出しリンク先が、記事ではなくブクマページになってしまっているのを修正します。

  1. // ==UserScript==
  2. // @name Hatena Bookmark Link Modifier
  3. // @namespace knoa.jp
  4. // @description はてなブックマークの下部の記事の見出しリンク先が、記事ではなくブクマページになってしまっているのを修正します。
  5. // @include https://b.hatena.ne.jp/entry/*
  6. // @noframes
  7. // @version 1.0.3
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function(){
  12. const modify = function(){
  13. let links = document.querySelectorAll('[class$="title"] > a[href^="/entry/"]');
  14. Array.from(links).forEach(link => {
  15. // 2階建てに対応
  16. // https://b.hatena.ne.jp/entry/s/b.hatena.ne.jp/entry/s/www.famitsu.com/news/202008/14204041.html
  17. if(link.href.startsWith('https://b.hatena.ne.jp/entry/s/')){
  18. link.href = link.href.replace('https://b.hatena.ne.jp/entry/s/', 'https://');
  19. }else{
  20. link.href = link.href.replace('https://b.hatena.ne.jp/entry/', 'http://');
  21. }
  22. });
  23. };
  24. modify();
  25. observe(document.body, modify);
  26. if(document.hidden) window.addEventListener('focus', modify, {once: true});
  27. function observe(element, callback, options = {childList: true, characterData: false, subtree: false, attributes: false, attributeFilter: undefined}){
  28. let observer = new MutationObserver(callback.bind(element));
  29. observer.observe(element, options);
  30. return observer;
  31. }
  32. })();