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

    @@ -1,8 +1,7 @@ /* - * @title 現在のドキュメントをSJISで読み直す + * @title 現在のドキュメントを文字コード自動判定で読み直す * @include http://* * @license MIT License - * @require */ const xhr = new XMLHttpRequest(); @@ -11,10 +10,34 @@ xhr.onload = function() { const reader = new FileReader(); reader.onload = function() { - document.write(b.result); + + // 簡易文字コード検出(このbookmarkletの用途的にUTF-8は考慮してない) + const enc = (function(bytes) { + let cnt = 0; + for (let 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() { alert(reader.result);document.write(reader.result); }; + reader.readAsText(xhr.response, enc); }; - reader.readAsText(a.response, prompt("enc?", "Shift_JIS")) + reader.readAsArrayBuffer(xhr.response); }; -xhr.send(); - -// javascript:(function(a,b){a.open("GET",location.href);a.responseType="blob";a.onload=function(){b.onload=function(){document.write(b.result)};b.readAsText(a.response,prompt("enc?","Shift_JIS"))};a.send()})(new XMLHttpRequest,new FileReader) +xhr.send();
  • /*
     * @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 cnt = 0;
          for (let 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() { alert(reader.result);document.write(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