MozRepl の日本語文字化け解消

■後日追記: 続き → 今見ているページの url と title をメモる - armbrust の日記


なんとかしたいと調べていましたが、ズバリこれという情報に当たりません。

で・・・、

Javascriptのalertで日本語が文字化けするんだけれども - 牌語備忘録 -pygo
Text Escaping and Unescaping in JavaScript

を見つけて中身を読んでいるうちに、
これと同じことを Firefox に js でやらせちゃえばいいんじゃね? と思いました。
ゴチャゴチャと試した結果が ↓ です。

Welcome to MozRepl.

:

repl> document.title
"ofj - Mozilla Firefox" // 化けてる (ほんとは "はてな"って出るはず)

repl> alert("あああ") // 化ける

:

repl> repl.load("http://0xcc.net/jsescape/strutil.js")
!!! Trying to load a non-local URI.

repl> function r_conv(str){
        return convertUnicodeCodePointsToString(
          convertUnicodeCodePointsToUtf8Bytes(
            convertStringToUnicodeCodePoints(str))) // 理解できてないけど・・・
      }

repl> function s_conv(str){
        return convertUnicodeCodePointsToString(
          convertUtf8BytesToUnicodeCodePoints(
            convertStringToUnicodeCodePoints(str))) // 理解できてないけど・・・
      }


repl> r_conv(document.title)
"はてな - Mozilla Firefox" // どうやら上手くいったらしい

repl> alert(s_conv("あああ")) // どうやら上手く出るみたい

もしくは、こういうほうがいいかな・・・。

Welcome to MozRepl.

:

repl> repl.load("file:///c:/temp/js/strutil.js") // DLしてローカルに置いた

repl> String.prototype.CodeToU8 = function(){
        return convertUnicodeCodePointsToString(
          convertUnicodeCodePointsToUtf8Bytes(
            convertStringToUnicodeCodePoints(this)))
      }

repl> String.prototype.U8ToCode = function(){
        return convertUnicodeCodePointsToString(
          convertUtf8BytesToUnicodeCodePoints(
            convertStringToUnicodeCodePoints(this)))
      }

repl> document.title.CodeToU8()
"はてな - Mozilla Firefox"

repl> alert("あああ".U8ToCode())

もっと清く正しい方法がありそうなんだけどなぁ。

あ、js 部分が妙な書き方でスミマセン。