Skip to content

Instantly share code, notes, and snippets.

@khris
Created October 2, 2012 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khris/3820346 to your computer and use it in GitHub Desktop.
Save khris/3820346 to your computer and use it in GitHub Desktop.
/**
*
* It's under MPL 1.1
* See http://www.mozilla.org/MPL/1.1/
* You can install this from https://addons.mozilla.org/ko/firefox/addon/naver-endic/
*
*/
let { Ci, Cc } = require("chrome");
let Panel = require("panel").Panel;
let selection = require("selection");
const windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
let node_ = null;
function context() {
return windowMediator.getMostRecentWindow("navigator:browser").document.
commandDispatcher.focusedWindow;
}
function trimStr(src) {
return src.replace(/^[\0\s]+|[\0\s]+$/g,'');
}
exports.main = function() {
let dictionaryPanel = new Panel({
id: "dictionaryPanel",
width: 405,
height: 513,
onHide: function() {
node_.parentNode.removeChild(node_);
node_ = null;
}
});
let onSelection = function () {
if (typeof(selection.text) == 'string') {
var selected_text = trimStr(selection.text);
if (selected_text.length > 0) {
dictionaryPanel.contentURL = 'http://endic.naver.com/popManager.nhn?m=search&query=' + selected_text;
let window_ = context();
let selection_ = window_.getSelection();
if (selection_.rangeCount < 1) {
return;
}
let curElement = window_.document.activeElement;
if (curElement.nodeName.toLowerCase() == 'textarea'
|| curElement.nodeName.toLowerCase() == 'input') {
return;
}
console.log(['\n'+prop+': '+selection_.anchorNode[prop] for (prop in selection_.anchorNode)].join(''));
let range_ = selection_.getRangeAt(0);
try {
if (node_ && node_.parentNode) {
node_.parentNode.removeChild(node_);
} else {
node_ = window_.document.createElement("span");
}
} catch (e if e instanceof TypeError) {
node_ = window_.document.createElement("span");
} finally {
range_.insertNode(node_);
dictionaryPanel.show(node_);
}
}
}
};
selection.on('select', onSelection);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment