Search code examples
firefoxfirefox-addonfirefox-addon-sdk

Firefox Add-on dev , How to use sample code from MDN in Addon Builder


I'm using add on builder to build extensions when searching MDN I find interfaces that I can't figure how to call them in online addon builder

for example this code

var bmsvc = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
                      .getService(Components.interfaces.nsINavBookmarksService);

doesn't build and results in XPI error


Solution

  • On the top of your document put line:

    const { Cc, Ci, Cu } = require('chrome');
    

    and instead of Components.classes try using Cc, instead of Components.interfaces try Ci.

    var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]
                      .getService(Ci.nsINavBookmarksService);
    

    This should work, if not, put link to your public addon or sample of code and link to documentation site.