If you control the webpage, it's better to communicate to it via DOM events.
If you absolutely have to access unsafe JS objects, you can do that
using wrappedJSObject property. It's probably a good idea to avoid
interacting with the page directly from chrome though, run the code in
a sandbox (http://developer.mozilla.org/en/docs/Components.utils.evalInSandbox
) instead. Something like:
var s = Components.utils.Sandbox(content);
s.win = content;
Components.utils.evalInSandbox("win.function()", s);
Nickolay
I m repeating my prob. here:
m creating a toolbar for Mozilla firefox browser. I am handling
toolbar button click events in a .js file which is included in xul file
of Toolbar. Now i have a web page which has defined some js file in its
source page ( means some js files are inserted in asp page using Tag
<script>) ., These js file of web page have some functions defined. Now
i want to call these functions from my toolbar js file.
If i call function directly by its name then i m getting error
"function is not defined". Then i thought XPCOM can help me. I can use
EvaluateScript method .
I can call GetContext method of nsIScriptGlobalObject interface. and
then i will call EvaluateScript
method. i know how to use them here is code:
nsCOMPtr<nsIWindowWatcher>
wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
nsIDOMWindow *activeWindow = nsnull;
rv = wwatch->GetActiveWindow(&activeWindow);
nsCOMPtr<nsIScriptGlobalObject>
pScriptObject(do_QueryInterface(activeWindow));
nsCOMPtr<nsIScriptContext> pScript;
pScriptObject->GetContext(getter_AddRefs(pScript));
if i will get nsIScriptContext variable then i can call EvaluateScript
method.
But my problem is that i dont know how to use
"nsCOMPtr<nsIWindowWatcher>"
type of statements in javascript , because when i write this code i m
getting error like
nsCOMPtr is undeclared identifier.
Can someone plz help me how to use above code in javascript/
how to use XPCOM, which library/SDK i will need?
You're trying to use C++ within javascript. That won't work. The XPCOM interface nsIScriptContextOwner is exposed to javascript (http://www.xulplanet.com/references/xpcomref/ifaces/nsIScriptContextOwner.html). Maybe that does what you need, but in general calling JS functions defined in a webpage from an extension shouldn't/can't be done (see previous post from Nickolay on this topic last week or so).
But i called same functions for my IE toolbar.
so it should also call from firefox , means there should be some
method...