Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log selected text with Firefox Addon Builder?

I have followed several tutorials with no success. I think this is the classic example but I can't make it work. I can save my project, install the addon and I can see the context menu item "Log Selection" when I select some text, but when I click on it nothing happens.

exports.main = function() {

    var contextMenu = require("context-menu");
    var request = require("request");
    var selection = require("selection");

    var menuItem = contextMenu.Item({
        label: "Log Selection",
        context: contextMenu.SelectionContext(),
        contentScript: 'self.on("click", function () {' +
                 '  var text = window.getSelection().toString();' +
                 '  self.postMessage(text);' +
                 '});',
        onMessage: function (selectionText) {
            alert(selectionText);
        }
    });
}

Even if my addon contains only one alert, the addon is installed but the alert is not shown.

exports.main = function() {
       alert("Hello world");
}

Extra info:

  • SDK: 1.14 (up to date)
  • Add-on Builder Helper: 1.7 (up to date)
  • Add On Builder Webpage: https://builder.addons.mozilla.org
like image 742
gal007 Avatar asked Dec 12 '25 03:12

gal007


1 Answers

You cannot use alert directly in a lib/ module. There is simply no window that could display the alert and hence there is no alert function.

Have a look instead at the Logging documentation.

Should you really want to display something, you could e.g. use notifications, or alert using nsIPromptService (example on this page) or from within a content document (widget, etc).

Here is an example showing off different methods.

like image 61
nmaier Avatar answered Dec 15 '25 13:12

nmaier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!