0

javascript xpcom コンポーネントを作成しています そのソースは次のとおりです-

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);
}

次を使用して呼び出すと:

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;
  }

次のようにエラーが発生します:

[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]
4

2 に答える 2

3

これは、 が XPCOM コードで定義されてalert いないためです (ただし、コンポーネントは取得できます)。拡張機能でアラートを出すのは良い考えではありません。ユーザーがそのページを操作するのをブロックするからです。非モーダル トースター ボックス通知関数を使用します。

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

次の構文で使用しますalert(icon, title, body)

MDC の詳細については、こちらをご覧ください。

于 2009-09-30T10:25:54.247 に答える