Clarify tab.attach() to explain how to use its return value

RESOLVED FIXED

Status

Add-on SDK
Documentation
P2
normal
RESOLVED FIXED
6 years ago
3 years ago

People

(Reporter: wbamberg, Assigned: wbamberg)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Assignee)

Description

6 years ago
See: https://groups.google.com/forum/#!msg/mozilla-labs-jetpack/T5AWgYnT63o/kazLFCxqljUJ

The documentation for tab.attach should explain that it returns a worker object which you can use to communicate with the content script. The example should also include this.

Updated

6 years ago
Priority: -- → P2
(Assignee)

Comment 1

6 years ago
Created attachment 569721 [details] [diff] [review]
Added stuff on tab.attach returning a worker; included example
Attachment #569721 - Flags: review?(poirot.alex)
Comment on attachment 569721 [details] [diff] [review]
Added stuff on tab.attach returning a worker; included example

The example expose a really bad practice that would create an useless worker for each opened document. 
You are trying to show a really common usecase with widget, where you want to evaluate a content script only for one particular tab, the current one.

In order to do so, you will write something like this:

var fetchContent = "self.postMessage(document.body.innerHTML);";

var widget = widgets.Widget({
  id: "fetch-content",
  label: "Fetch content",
  contentURL: "http://www.mozilla.org/favicon.ico",
  onClick: function() {
    require("tabs").activeTab.attach({
      contentScript: fetchContent,
      onMessage: function (message) {
        console.log(message);
      }
    });
  }
});

I removed worker usage as it will be unecessarry in most cases. 
But this example is really usefull as it will be really common, so if you have an idea of worker/postMessage/onMessage usage on top of it, it would be perfect!
Attachment #569721 - Flags: review?(poirot.alex) → review-
(Assignee)

Comment 3

6 years ago
Do you think this: https://builder.addons.mozilla.org/addon/1041869/latest/ is a better example? It's slightly long, and related to one of the new tutorials, so it might be better to include it in a tutorial rather than the API doc.
This is explained in https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs#attach%28options%29 now
Status: NEW → RESOLVED
Last Resolved: 3 years ago
Resolution: --- → FIXED
OS: Mac OS X → All
Hardware: x86 → All
You need to log in before you can comment on or make changes to this bug.