Reputation: 183
I took an existing addon which searches a given word on google. Here is the link: https://addons.mozilla.org/en-US/firefox/addon/inline-google-search/?src=api
Addon works by selecting text, then right clicking on it, and in context menu get an option to search it on google.
Below is the main js file:
exports.main = function() {};
var panel = require("sdk/panel").Panel({
width:700,
height: 500,
contentURL: "about:blank",
onHide : function(){
this.contentURL = "about:blank"
}
});
var contextMenu = require("sdk/context-menu");
var menuItem = contextMenu.Item({
label: "Search Google Inline",
context: contextMenu.SelectionContext(),
contentScript: 'self.on("click", function () {' +
' var text = window.getSelection().toString();' +
' self.postMessage(text);' +
'});',
onMessage: function (selectionText) {
panel.contentURL = "https://www.google.co.in/search?q="+selectionText;
panel.show();
}
});
I am adding functionality so that after selecting text and pressing Ctrl+Shift+d, the search is performed for the text on google.
Few extra lines added by me at the end:
var { Hotkey } = require("sdk/hotkeys");
var selection = require("sdk/selection");
var showHotKey = Hotkey({
combo: "accel-shift-d",
onPress: function() {
panel.contentURL = "https://www.google.co.in/search?q="+selection.text;
panel.show();
}
});
The above snippet I found from here : Access selected text within a Hotkey object
Also there was a file harness-options.json, which in which i updated sha256 sum of main.js file and added requirements sdk/hotkeys and sdk/selection in manifest section.
But the addon fails to work after installing. Even the context menu option doesn't appear anymore. So it seems like I broke the code.
What may I be doing wrong?
Upvotes: 1
Views: 194
Reputation: 183
Instead of editing the addon I created it from scratch using cfx and addon-sdk and it worked.
Upvotes: 1