Created
May 31, 2011 16:23
-
-
Save xulapp/1000798 to your computer and use it in GitHub Desktop.
viewCharInfo.uc.js
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
// ==UserScript== | |
// @name viewCharInfo.uc.js | |
// @description | |
// @include main | |
// @compatibility Firefox | |
// @namespace http://twitter.com/xulapp | |
// @author xulapp | |
// @license MIT License | |
// @version 2011/06/01 01:20 +09:00 | |
// ==/UserScript== | |
(function viewCharInfo() { | |
const XUL_NS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'; | |
var chr = ''; | |
var menuitem = document.createElementNS(XUL_NS, 'menuitem'); | |
menuitem.setAttribute('id', 'context-viewcharinfo'); | |
menuitem.setAttribute('accesskey', 'i'); | |
menuitem.addEventListener('command', function VCI_onCommand() { | |
var code = getUnicodeCharCode(chr); | |
gBrowser.loadOneTab('http://unicode.org/cgi-bin/GetUnihanData.pl?codepoint=' + code.toString(16)); | |
}, false); | |
var contentAreaContextMenu = $('contentAreaContextMenu'); | |
contentAreaContextMenu.insertBefore(menuitem, $('context-searchselect').nextSibling); | |
contentAreaContextMenu.addEventListener('popupshowing', function VCI_onContextPopupShowing() { | |
if (gContextMenu.isTextSelected) { | |
var sel = content.getSelection().toString().substring(0, 2); | |
chr = getUnicodeChar(sel); | |
menuitem.setAttribute('label', U('文字の情報を調べる: "') + chr + '"'); | |
} | |
gContextMenu.showItem(menuitem, gContextMenu.isTextSelected); | |
}, false); | |
function isSurrogateCodePoint(chr) { | |
var code = chr.charCodeAt(0); | |
return 0xd800 <= code && code <= 0xdb7f; | |
} | |
function getUnicodeChar(str) { | |
var result = str[0]; | |
if (isSurrogateCodePoint(result)) | |
result += str[1]; | |
return result; | |
} | |
function getUnicodeCharCode(str) { | |
var code = str.charCodeAt(0); | |
if (isSurrogateCodePoint(str)) | |
code = ((code & 0x3ff) << 10 | str.charCodeAt(1) & 0x3ff) + 0x10000; | |
return code; | |
} | |
function U(text) 1 < 'あ'.length ? decodeURIComponent(escape(text)) : text; | |
function $(id) document.getElementById(id); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment