Created
February 24, 2009 08:04
-
-
Save edvakf/69466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/ruby | |
require "cgi" | |
require "kconv" | |
$KCODE = 'UTF8' | |
begin | |
html = <<END | |
<html> | |
<head> | |
<!-- head --> | |
<script type="text/javascript"> | |
append = function(){ | |
v = document.getElementById('xhr').value; | |
switch(v){ | |
case 'u': | |
c = "UTF-8"; | |
break; | |
case 's': | |
c = "SHIFT_JIS"; | |
break; | |
case 'e': | |
c = "EUC-JP"; | |
break; | |
default: | |
c = document.characterSet; | |
} | |
var xhr=new XMLHttpRequest(); | |
var url = location.href.split('?')[0]+'?'+$kv('head')+'&'+$kv('meta')+'&'+$kv('enc'); | |
xhr.open('GET', url,true); | |
xhr.overrideMimeType('text/html; charset=' + c); | |
var run = function(text){ | |
if(document.implementation.createHTMLDocument) var doc = document.implementation.createHTMLDocument('hoge'); | |
else if(document.implementation.createDocument) var doc = document.implementation.createDocument(null,'hoge',null); | |
else return; | |
var range = document.createRange(); | |
range.selectNodeContents(document.body); | |
var df = range.createContextualFragment(text); | |
doc.documentElement.appendChild(df); | |
var r = doc.evaluate('/descendant-or-self::node()/child::div[@class="main"]',doc,null,0,null); | |
document.body.appendChild(r.iterateNext().cloneNode(true)); | |
} | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState == 4 && xhr.status == 200) { | |
return run(xhr.responseText); | |
}else{ | |
return; | |
} | |
} | |
xhr.send(null); | |
} | |
function $kv(id){ | |
var e = document.getElementById(id); | |
if(e) return e.name + '=' + e.value; | |
else return ''; | |
} | |
</script> | |
</head> | |
<body> | |
<div class="main" style="border:solid black 1px;"> | |
<!-- body --> | |
</div> | |
<p>各文字コードを指定してください。</p> | |
<form action="" method="get"> | |
http header <input type="text" id="head" name="head"> (s→SHIFT_JIS、e→EUC-JP、u→UTF-8、n→http header でエンコードを指定しない、空欄→空文字)<br> | |
meta tag <input type="text" id="meta" name="meta"> (s→SHIFT_JIS、e→EUC-JP、u→UTF-8、n→meta tag でエンコードを指定しない、空欄→空文字)<br> | |
encoding <input type="text" id="enc" name="enc"> (s→SHIFT_JIS、e→EUC-JP、u or 空欄→UTF-8)<br> | |
<input type="submit" value="ページを再表示する"> | |
</form> | |
overrideMimeType charset <input type="text" id="xhr" /> (s→SHIFT_JIS、e→EUC-JP、u→UTF-8、空欄→document.characterSet)<br> | |
<button onclick="append();return false;">XMLHttpRequest で追加する</button> | |
</body> | |
</html> | |
END | |
cgi = CGI.new | |
head = cgi['head'] | |
case head | |
when 'n' | |
head = nil | |
when 'u' | |
head = 'UTF-8' | |
when 's' | |
head = 'SHIFT_JIS' | |
when 'e' | |
head = 'EUC-JP' | |
end | |
if head | |
print cgi.header({"type" => "text/html", "charset" => head}) | |
html = html.sub!('<!-- body -->',"HTTP ヘッダーで charset=#{head} が指定されました。<br><!-- body -->") | |
else | |
print cgi.header({"type" => "text/html"}) | |
html = html.sub!('<!-- body -->',"HTTP ヘッダーで charset が指定されませんでした。<br><!-- body -->") | |
end | |
meta = cgi['meta'] | |
case meta | |
when 'n' | |
meta = nil | |
when 'u' | |
meta = 'UTF-8' | |
when 's' | |
meta = 'SHIFT_JIS' | |
when 'e' | |
meta = 'EUC_JP' | |
end | |
if meta | |
html.sub!('<!-- head -->', "<meta http-equiv=\"text/html; charset=#{meta}\">") | |
html.sub!('<!-- body -->', "meta タグで charset=#{meta} が指定されました。<br><!-- body -->") | |
else | |
html.sub!('<!-- body -->', "meta タグが指定されませんでした。<br><!-- body -->") | |
end | |
enc = cgi['enc'] | |
case enc | |
when 's' | |
html.sub!('<!-- body -->', "文字コード SHIFT_JIS で提供されました。<br><!-- body -->") | |
html = html.tosjis | |
when 'e' | |
html.sub!('<!-- body -->', "文字コード ECU-JP で提供されました。<br><!-- body -->") | |
html = html.toeuc | |
else | |
html.sub!('<!-- body -->', "文字コード UTF-8 で提供されました。<br><!-- body -->") | |
end | |
print html | |
rescue Exception => e | |
print e | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment