素人がプログラミングを勉強していたブログ

プログラミング、セキュリティ、英語、Webなどのブログ since 2008

連絡先: twitter: @javascripter にどうぞ。

はてなダイアリーのタイトルを必ずリンクにするGreasemonkeyスクリプト

たまにタイトル部分がリンクになっていないはてなダイアリーがあって、そういう場合は「最新の日記」までカーソルを動かさなきゃならなくて不便。

// ==UserScript==
// @name           hatenaDiaryH1Linker
// @namespace      http://d.hatena.ne.jp/javascripter/
// @include        http://d.hatena.ne.jp/*
// ==/UserScript==
(function() {
  const Node = document.createElement('span');
  var [h1] = document.getElementsByTagName('h1');
  if (h1.firstChild.nodeType == Node.ELEMENT_NODE) return;
  var a = document.createElement('a');
  a.textContent = h1.firstChild.nodeValue;
  [a.href] = location.pathname.match(/\/\w+\//);
  h1.replaceChild(a, h1.firstChild);
})();