Skip to content

Instantly share code, notes, and snippets.

@Griever
Created May 22, 2012 14:01
Show Gist options
  • Save Griever/2769271 to your computer and use it in GitHub Desktop.
Save Griever/2769271 to your computer and use it in GitHub Desktop.
全角英数を半角英数にするBookmarklet
「ページ内の全角英数を半角英数に置換するだけのユーザースクリプト」を見てなんとなく書いた。需要は知らん。
http://oflow.me/archives/1002
Firefox, Opera, Chrome で動くと思う。
以下の文字が置き換えられるはず。
 ¥!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}
javascript:/*Zenkaku-Hankaku*/(function(doc){
var zen = /[\u3000\uFFE5\uFF01-\uFF5E]/g;
var arr = ['contains(.,"\u3000")','contains(.,"\uFFE5")'];
for (var i = 0xFF01; i < 0xFF5E; ++i) {
arr.push('contains(.,"' + String.fromCharCode(i) + '")');
}
var xpath = '//text()[(' + arr.join(' or ') + ') and not(ancestor::style) and not(ancestor::script)]';
var x = doc.evaluate(xpath, doc.body, null, 7, null);
for (var i = 0, len = x.snapshotLength; i < len; ++i) {
var node = x.snapshotItem(i);
node.nodeValue = node.nodeValue.replace(zen, function(str){
if (str === '\u3000') return ' ';
if (str === '\uFFE5') return '\\';
return String.fromCharCode( str.charCodeAt(0) - 0xFF01 + 0x21 );
});
}
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment