現在のドキュメントを文字コード自動判定で読み直す

    @@ -36,7 +36,10 @@ })(new Uint8Array(reader.result)); // TextDecoder使いたいですね... http://caniuse.com/#feat=textencoder - reader.onload = function() { document.documentElement.innerHTML = reader.result; }; + reader.onload = function() { + // document.writeだと(iOS9のSafariでは)タイトルが更新されなかった + document.documentElement.innerHTML = reader.result; + }; reader.readAsText(xhr.response, enc); }; reader.readAsArrayBuffer(xhr.response);
  • /*
     * @title 現在のドキュメントを文字コード自動判定で読み直す
     * @include http://*
     * @license MIT License
     */
    
    const xhr = new XMLHttpRequest();
    xhr.open("GET", location.href);
    xhr.responseType = "blob";
    xhr.onload = function() {
      const reader = new FileReader();
      reader.onload = function() {
        
        // 簡易文字コード検出(このbookmarkletの用途的にUTF-8は考慮してない)
        const enc = (function(bytes) {
          // let->var はiOS9対策
          /*let*/var cnt = 0;
          for (/*let*/var i = 0; i < bytes.length - 1 && cnt < 50; ++i) {
            const b = bytes[i];
            
            if (b == 0x1B) return "iso-2022-jp";
            if (b < 0x81) continue;
            
            ++cnt;
            if (b < 0xA0) {
              if ((b == 0x8E || b == 0x8F) && bytes[i + 1] >= 0xA1) {
                ++i;
              } else {
                return "shift-jis";
              }
            } else if (b >= 0xE0) {
              ++i;
            }
          }
          return "euc-jp";
        })(new Uint8Array(reader.result));
        
        // TextDecoder使いたいですね... http://caniuse.com/#feat=textencoder
        reader.onload = function() {
          // document.writeだと(iOS9のSafariでは)タイトルが更新されなかった
          document.documentElement.innerHTML = reader.result;
        };
        reader.readAsText(xhr.response, enc);
      };
      reader.readAsArrayBuffer(xhr.response);
    };
    xhr.send();
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2017/05/28 09:24:34 - 2017-05-28
  2. 2017/05/28 09:18:43 - 2017-05-28
  3. 2017/04/18 21:21:46 - 2017-04-18
  4. 2017/04/18 21:16:06 - 2017-04-18
  5. 2017/02/24 05:22:06 - 2017-02-24
  6. 2017/02/24 05:19:02 - 2017-02-24
  7. 2017/02/24 05:07:50 - 2017-02-24
  8. 2017/02/24 05:00:53 - 2017-02-24
  9. 2017/02/01 17:01:56 - 2017-02-01
  10. 2017/02/01 17:01:27 - 2017-02-01