简体   繁体   中英

Unable to add Hotkey functionality to firefox addon

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?

不用编辑插件,而是使用cfx和addon-sdk从头开始创建它,并且可以正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM