views:

462

answers:

2

I am creating a javascript xpcom component Its source is as follows-

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function callback() { }

callback.prototype = {
  classDescription: "My Hello World Javascript XPCOM Component",
  classID:          Components.ID("{3459D788-D284-4ef0-8AFF-96CBAF51BD35}"),
  contractID:       "@jscallback.p2psearch.com/f2f;1",
  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.ICallback]),
  received:function(data){
          alert("Received: " +data);
         },
  status:function(data){
            alert("Status: "+data);
           },
  expr:function(data){
          alert("Expr: "+expr);
         }
};
var components = [callback];
function NSGetModule(compMgr, fileSpec) {
  return XPCOMUtils.generateModule(components);
}

When I call it using:

try {
    const p2pjcid = "@jscallback.p2psearch.com/f2f;1";
    var jobj = Components.classes[p2pjcid].createInstance();
    jobj = jobj.QueryInterface(Components.interfaces.ICallback);
    jobj.received("Hello from javascript")
  }
  catch (err) {
    alert(err);
    return;
  }

I get Error as :

[Exception... "'[JavaScript Error: "alert is not defined" {file: "file:///C:/Documents%20and%20Settings/mypc/Application%20Data/Mozilla/Firefox/Profiles/ngi5btaf.default/extensions/spsarolkar@gmail/components/callback.js" line: 11}]' when calling method: [ICallback::received]"  nsresult: "0x80570021 (NS_ERROR_XPC_JAVASCRIPT_ERROR_WITH_DETAILS)"  location: "JS frame :: chrome://sample/content/clock.js :: initClock :: line 28"  data: yes]
+3  A: 

That's because alert is not defined in XPCOM code (though you can get the component). It's not a good idea to alert in an extension as it blocks the user from interacting with the page(s) they are on. Use the non-modal toaster-box notification function:

const alert = Components.classes['@mozilla.org/alerts-service;1']
                  .getService(Components.interfaces.nsIAlertsService)
                  .showAlertNotification;

Use it in the following syntax: alert(icon, title, body).

Here's more info on MDC.

Eli Grey
If I need to access javascript dom?? As I am also getting document is undefined. Can you please point me a link. Thanks
Xinus
Search around for XPCSafeJSObjectWrapper, etc.
Eli Grey
XPCNativeWrapper should also be what you're looking for.
Eli Grey
+1  A: 

You can also use dump() from a JavaScript component.

Neil