We are trying to call a javascript function defined inside the current
opened page from our xpcom.

We found a suggestion in the mail archive (Subject: RE: XPCOM object
callback to javascript).



In suggested example the code start using a not well defined window (pWindow
var).

We use the method "GetActiveWindow" of nsIWindowWatcher object to have an
instance of this window.



We compile the code and run it, we never see the alert but we have the
javascript error "undefined is not a function".



What are we making a mistake in?



Thanks in advance.

Regards

Marily Rossotti



****** c++ side ******

nsresult rv;



nsCOMPtr<nsIWindowWatcher>

wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));

nsIDOMWindow *activeWindow = nsnull;

rv = wwatch->GetActiveWindow(&activeWindow);

if (NS_SUCCEEDED(rv) && activeWindow) {

             printf("active window found\n");



nsCOMPtr<nsIScriptGlobalObject>

pScriptObject(do_QueryInterface(activeWindow));

            if (!pScriptObject) {

                        printf("script global object not found\n");

                        return NS_KO;

}

            JSContext* pJSContext = nsnull;

             jsval jval;

             jsval jsargs = 0L;



            // Get the script context

            nsCOMPtr<nsIScriptContext> pScript;

            pScriptObject->GetContext(getter_AddRefs(pScript));



            if(pScript == nsnull){

                        printf("script context not found\n");

            return NS_KO;

}



pJSContext = (JSContext*)pScript->GetNativeContext();

if(pJSContext == nsnull){

                        printf("JSContext not null\n");

                        return NS_KO;

}

//Call the java script function "testFunction" defined in the xul file

JSBool jb = JS_CallFunctionName(pJSContext, pJSContext->globalObject,

                        "testFunction", 0, &jsargs, &jval);

if(jb == JS_TRUE)

                        printf("jb TRUe\n");



}







****** XUL side ******

<?xml version="1.0"?>

<!-- Sample XUL file -->

<window

xmlns=http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul
onload="init();"

>



<script type="text/javascript" language="JavaScript">

var prova;



function init(){


netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

            prova = Components.classes["xpc.PROVA_intf;1"].createInstance();

            prova = prova.QueryInterface(Components.interfaces.mozPROVA);

}



function testFunction(){

            alert("Hello from testFunction");

}



</script>

</window>




_______________________________________________
Mozilla-xpcom mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to