Skip to content

Commit

Permalink
Made compatible with Firefox 4.0 using Add-on SDK 1.0b5. Added select…
Browse files Browse the repository at this point in the history
…ion deasciification and asciification but this does not extend to text boxes and areas due to limitation in the Add-on SDK. Commented out hotkey feature and marked is a TO DO item to be implemented in the future.
  • Loading branch information
emres committed May 22, 2011
1 parent b33893d commit d8d955f
Showing 1 changed file with 85 additions and 56 deletions.
141 changes: 85 additions & 56 deletions 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
// );
// });

0 comments on commit d8d955f

Please sign in to comment.