How can I log selected text with Firefox Addon Builder?

假装没事ソ 提交于 2019-12-10 19:13:46

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/18233888/how-can-i-log-selected-text-with-firefox-addon-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!