diff --git a/lib/main.js b/lib/main.js index 7b0d2a7..5d90248 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,63 +1,92 @@ var contextMenu = require("context-menu"); +var data = require("self").data; var tabs = require("tabs"); var selection = require("selection"); var deasciifier = require("deasciifier"); - var dea = new deasciifier.Deasciifier(); var rea = new deasciifier.Asciifier(); -// Create a new context menu item. -var deasciifyItem = contextMenu.Item({ - - label: "Convert to Turkish", - - // A CSS selector. Matching on this selector triggers the - // display of our context menu. - - // context: "input[type=text]", - context: "input[type=text], textarea", - - // When the context menu item is clicked, perform a change - onClick: function (contextObj, item) { - var textBox = contextObj.node; - textBox.value = dea.deasciify(textBox.value); - //console.log("selected text:" + selection.text); - } -}); - -// Create a new context menu item. -var asciifyItem = contextMenu.Item({ - - label: "Convert to pure ASCII", - - // A CSS selector. Matching on this selector triggers the - // display of our context menu. - - // context: "input[type=text]", - context: "input[type=text], textarea", - - // When the context menu item is clicked, perform a change - onClick: function (contextObj, item) { - var textBox = contextObj.node; - textBox.value = rea.asciify(textBox.value); - } -}); - -// Add menu items to the application's context menu. -contextMenu.add(deasciifyItem); -contextMenu.add(asciifyItem); - -// Add keypress event listener to the current tab -// after the web page is loaded and ready -tabs.onReady.add( - function(tab) { - tab.contentWindow.addEventListener('keypress', - function(e) { - if (e.metaKey && e.charCode == 84) { - // run code for WindowsKey + Shift + t - tab.contentDocument.activeElement.value = dea.deasciify(tab.contentDocument.activeElement.value); - } - }, - false - ); -}); +exports.main = function(options, callbacks) { + console.log(options.loadReason); + + var deasciifySelection = contextMenu.Item + ({ + label: "Convert selected text into Turkish (deasciify)", + + context : contextMenu.SelectionContext(), + + contentScript: 'self.on("click", function (node) {' + + ' var text = window.getSelection().toString();' + + ' self.postMessage(text);' + + '});', + + onMessage: function(text) { + if (text.length == 0) { + throw ("Text to convert must not be empty!"); + } + selection.text = dea.deasciify(text); + } + }); + + var asciifySelection = contextMenu.Item + ({ + label: "Convert selected text into ASCII (asciify)", + + context : contextMenu.SelectionContext(), + + contentScript: 'self.on("click", function (node) {' + + ' var text = window.getSelection().toString();' + + ' self.postMessage(text);' + + '});', + + onMessage: function(text) { + if (text.length == 0) { + throw ("Text to convert must not be empty!"); + } + selection.text = rea.asciify(text); + } + }); + + var deasciifyItem = contextMenu.Item + ({ + label: "Convert to Turkish (deasciify)", + + context : contextMenu.SelectorContext("input[type=text], textarea"), + + contentScriptFile: data.url('deasciify-content-script.js') + }); + + var asciifyItem = contextMenu.Item + ({ + label: "Convert to ASCII (asciify)", + + context : contextMenu.SelectorContext("input[type=text], textarea"), + + contentScriptFile: data.url('asciify-content-script.js') + }); +}; + + +exports.onUnload = function (reason) { + console.log(reason); +}; + +/* + * TO DO: Key press events section below should be modified to work with the + * latest version of Add-on SDK, probably using 'hotkeys' API. + */ + + // Add keypress event listener to the current tab + // after the web page is loaded and ready +// tabs.onReady.add( +// function(tab) { +// tab.contentWindow.addEventListener('keypress', +// function(e) { +// if (e.metaKey && e.charCode == 84) { +// // run code for WindowsKey + Shift + t +// tab.contentDocument.activeElement.value = dea.deasciify(tab.contentDocument.activeElement.value); +// } +// }, +// false +// ); +// });