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

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

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

CSSを編集するブックマークレット

SafariにはStylish相当のものがないので自分で書く。
リアルタイムに編集できるようにするとかなり動作が重くなるので、少し工夫してる。

javascript:(function () {
  var css_edit = document.getElementsByClassName('_css_edit');
  if (css_edit.item(0)) {
    Array.prototype.slice.call(css_edit).forEach(function (elem) {
      elem.parentNode.removeChild(elem);
    });
    return;
  }
  var t = document.createElement('textarea');
  var s = document.createElement('style');
  s.type = 'text/css';
  t.className = s.className = '_css_edit';
  t.style.cssText = 'position:fixed;bottom:0;right:0;width:40em;height:10em;z-index:99;';
  var tid = null;
  t.addEventListener('keypress', function () {
    if (tid) {
      clearTimeout(tid);
      tid = null;
    } else {
      tid = setTimeout(function () {
        s.textContent = t.value;
      }, 100);
    }
  }, false);
  var head = document.getElementsByTagName('head').item(0);
  head.appendChild(s);
  document.body.appendChild(t);
})();