Question

I'm using Add-on Builder Beta (Firefox) and I'm trying to do something like this:

(There are 3 sections in Addon Builder: Lib, Data, Libraries)

  1. Get "def.htm" file from data section and open it in new tab I do it with this:

    var tabs = require("tabs");
    var data = require("self").data;
    tabs.open(data.url('def.htm'));
    
  2. Get the JSON definition from "def.htm" def.htm looks like this:

    <html><head>
    <title>Def title</title>
    </head><body>
    <script type="text/javascript">
        this.definition = {
            aaa: 1000,
            bbb: {
                ccc: {
                    ddd: "eee",
                    ...
                    ...
       };
    </script>
    </body></html>
    
  3. Call JS function and pass the definition to it:

It works with the Jetpack extension (on Firefox 3.5), here is the code:

jetpack.tabs.onReady(function() {
    var window = this.contentWindow.wrappedJSObject;
    var def = window.definition;

    dowork (def); 
});

I need the same functionality on Add-on SDK.

Was it helpful?

Solution

That's a rather strange approach, why are you trying to get JSON data in such a complicated way? How about putting it into a file data/definition.json (properly encoded then):

{
    "aaa": 1000,
    "bbb": {
        "ccc": {
            "ddd": "eee",
            ...
            ...
};

And reading it out using the request package:

var Request = require("request").Request;
var data = require("self").data;
Request({
  url: data.url("definitions.json"),
  onComplete: function(response)
  {
    dowork(response.json);
  }
});

But if you really want to access data in a tab - the Add-on SDK doesn't allow direct access to content pages from your extension. You can use the page-mod package to inject a content script into this page that will then send the data back to the extension. Something along these lines:

var PageMod = require("page-mod").PageMod;
PageMod({
  include: data.url("def.htm"),
  contentScriptWhen: 'end',
  contentScript: 'self.postMessage(definition)',
  onAttach: function onAttach(worker)
  {
    worker.on("message", function(data)
    {
      dowork(data);
    });
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top