Manngo
Manngo

Reputation: 16371

Manipulating the current document from an Add-on SDK panel

I am writing a Firefox add-on using the new SDK that does 5 very simple jobs. They are roughly related, so I would prefer a single add-on with a dropdown menu which I have implemented in a Panel.

I am working my way through the new SDK documentation, but can't find a direct solution. I find I can add a panel, but I cannot see how to manipulate the document in the current tab.

This is possible, isn't it? Using the global document doesn't work as presumably refers to the panel, or at least not to the document I am viewing.

So, how do I access the document from an add-on panel?

Upvotes: 0

Views: 55

Answers (1)

Makyen
Makyen

Reputation: 33356

The short answer to your question is that you don't access web content from your main JavaScript code. You interact with the document (web content) in the browser's tab using Content Scripts. You can have the content script pass messages to your main script.

MDN summarizes the principals of using content scripts with the SDK as follows:

Content scripts can be one of the more confusing aspects of working with the SDK, but you're very likely to have to use them. There are five basic principles:

  • the add-on's main code, including "main.js" and other modules in "lib", can use the SDK high-level and low-level APIs, but can't access web content directly
  • content scripts can't use the SDK's APIs (no access to globals exports, require) but can access web content
  • SDK APIs that use content scripts, like page-mod and tabs, provide functions that enable the add-on's main code to load content scripts into web pages
  • content scripts can be loaded in as strings, but are more often stored as separate files under the add-on's "data" directory. jpm doesn't make a "data" directory by default, so you must add it and put your content scripts in there.
  • a message-passing API allows the main code and content scripts to communicate with each other

Exactly how you would do what you are wanting is unclear because you have not provided a clear description of what you are doing. However, it almost sounds like you could implement what you want using the context-menu. The context menu is one of the ways to attach a content script to the current tab and may provide you with the dropdown menu feel which you desired (although it is part of the context menu, not a dropdown menu).

Upvotes: 2

Related Questions