0

I am trying to create an add-on for Firefox which enables me to input password from a REST based server to input field.

To achieve this, I added context menus with the add-on to Firefox.

As you can see I already succeeded in creating the context Menus

The listener for these context menus is implemented as follows:

add_listener(menu_id, listener) {
  const click_listener = function(info, tab) {
  if ('' + info.menuItemId == menu_id + '') {
    console.debug(info.targetElementId);
    listener(browser.menus.getTargetElement(info.targetElementId));
    }
  };
  this.listeners.push(click_listener);
  browser.contextMenus.onClicked.addListener(click_listener);
}

for (const pwDataEntry of pwDataSet.entries) {
  const entryId = menu_service.create_sub_menu(null, pwDataEntry.key, setId);
  menu_service.add_listener(entryId, element => {
    console.debug(element);
    element.value = pwDataEntry.content;
  });
}

I am working of the documentation from Firefox for creating a menu and the documentation for ClickData (=info). Here the documentation says:

targetElementI integer. An identifier of the element, if any, over which the context menu was created. Use menus.getTargetElement() in the content script to locate the element. Note that this is not the id attribute of the page element.

As you can see I have implemented the retrieval of the element exactly as documented. The thing is Firefox tells me, that it did not find the element:

3056573 background.js:32:25
null  background.js:120:29
element is null background.js:121

background.js:32:25 -> console.debug(info.targetElementId);
background.js:120:29 -> console.debug(element);
background.js:121 -> element.value = pwDataEntry.content;

If you need the more info about the code, I have uploaded the entire background.js here

I hope you know why it did not work as documented and where I went wrong ...

Thanks for your help and time in advance :)

reschiram
  • 33
  • 4

1 Answers1

0

What you are doing won't work. The important part is this sentence in the documentation:

Use menus.getTargetElement() in the content script to locate the element.

You are however calling menus.getTargetElement() in your background script. You will have to send that ID from the background script to your content script. Mozilla provides an example for this called menu-remove-element.

The documentation for menus.getTargetElement also has a very short example using browser.tabs.executeScript with targetElementId.

evilpie
  • 2,718
  • 20
  • 21
  • Ah thanks :) I didn't really understood the difference between the background script and content script... For anyone else reading this, i solved the issue by adding a right click listener in my content script (to mark the clicked input field) and pinging the content script from the background script through 'browser.tabs.sendMessage()' – reschiram May 07 '20 at 16:53