Click here to Skip to main content
15,886,724 members
Articles / Desktop Programming / MFC

SmartLexicon

Rate me:
Please Sign up or sign in to vote.
4.93/5 (14 votes)
30 Aug 2006GPL310 min read 85.9K   3.3K   51  
A multilingual dictionary engine with regular expressions support and Web browser integration.
var smartLex = {

	lookUpWord : function()
	{
		//var focusedWindow = document.commandDispatcher.focusedWindow;
		//var SelectionText = focusedWindow.__proto__.getSelection.call(focusedWindow);
		//SelectionText = SelectionText.toString();    

		var SelectionText;
		
		// Returns the selected text as a string.
		// Copied from dict extension (http://dict.mozdev.org/)
		{
			var node = document.popupNode;

			var selection;

			var nodeLocalName = node.localName.toUpperCase();

			if ((nodeLocalName == "TEXTAREA") ||
				(nodeLocalName == "INPUT" && node.type == "text")) {

				// The selection is either in a TEXTAREA of an INPUT HTML node.
				// This includes the text area of Mozilla Mail.
				selection = node.value.substring(
					node.selectionStart, 
					node.selectionEnd);
			}
			else {
				var focusedWindow = document.commandDispatcher.focusedWindow;

				try {
					// Get the selection page using XPCNativeWrapper
					// I can't use the more elegant "contentWindow.getSelection()",
					// because that only works in Firefox >= 1.0.2. 
					var winWrapper = new XPCNativeWrapper(
						focusedWindow,
						'document',
						'getSelection()');

					selection = winWrapper.getSelection();
				}
				catch (ex) {
					// XPCNativeWrapper doesn't exist.
					// This must be an old version of Firefox/Thunderbird/Mozilla
					// Use the less secure "__proto__" method.
					selection = focusedWindow.__proto__.getSelection.call(focusedWindow);
				}
			}

			var selectedWord = selection.toString();

			// Sanitize the selection by removing leading an trailing non-word characters.
			// NOTE: We cannot use \W because that matches many Unicode word characters.
			// Instead, we explicitly spell out the range of non-word ASCII-7 characters.
			selectedWord = selectedWord.replace(/^[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/, ''); // remove leading non-alnums
			selectedWord = selectedWord.replace(/[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+$/, ''); // remove trailing non-alnums
			selectedWord = selectedWord.replace(/\s+/, ' '); // conflate whitespace runs

			SelectionText = selectedWord;
		}

		var params = "";
		for(var i = 0; i < SelectionText.length; i++)
		{
			params += SelectionText.charCodeAt(i)
			if (i < SelectionText.length - 1)
				params += "-";
		}
		//params = document.actualEncoding + " " + params;
		//params = document.characterSet + " " + params;

		util = new objUtil();
		util.execCommand("C:\\Program Files\\SmartLexicon\\SmLexClient.exe", 
						 params);
	},
	
	init : function()
	{
		var cACM = document.getElementById("contentAreaContextMenu");
		
		cACM.addEventListener("popupshowing",
							   smartLex.initMenu,
							   false);
	},
	
	initMenu : function()
	{
		var cm = gContextMenu;
		document.getElementById("smartlex-lookupword").hidden = !cm.isTextSelected;
	}
	
}

function objUtil()
{
	this.error = function(msg) { alert("Error: " + msg);	}
	
	this.execCommand = function(command,strArgs) {
		// separate command arguments
		var args = new Array();
		var quoted = false;
		var double_quoted = false;
		var param = "";
		for (var i = 0; i < strArgs.length; i++) { 
			var charArgs = strArgs.substring(i,i + 1);
			if ( charArgs == "\""  && !quoted ) { double_quoted =! double_quoted; }
			else if ( charArgs == "\'" && !double_quoted ) { quoted =! quoted; } 
			else if ( /\s/.test(charArgs) && !quoted && !double_quoted ) {
				if (param != "") args.push( param );
			param = "";
			} else param += charArgs;	
		}
		if (param != "") args.push( param );
		// create a file object for the external program
		try {
			var applicFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
			var applic = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
			applicFile.initWithPath(command);
			if (! applicFile.exists()) {
				this.error("Executable '" + command + "' does not exist.");
			} else {
				applic.init(applicFile);
				applic.run(false, args, args.length);
			}
		} catch (e) {
			this.error("Cannot run executable: " + e);
			return false;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) Self employed
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions