Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling functions in content [was Re: (no subject)]

764 views
Skip to first unread message

Nickolay Ponomarev

unread,
Oct 27, 2006, 11:59:17 AM10/27/06
to harish kumar dixit, dev-ext...@lists.mozilla.org
On 27 Oct 2006 12:48:05 -0000, harish kumar dixit
<harish...@rediffmail.com> wrote:
> I am creating a toolbar for Firefox browser. There is my site on which i have some javascript functions by including some .JS files. Now my problem is that , i m unable to call function on page . I want to know how to call such a function from my toolbar JS file.
>
It is not recommended to do that, as it may cause security bugs.
XPCNativeWrapper is the security measure preventing you from accessing
JS inside content webpage (see
http://developer.mozilla.org/en/docs/XPCNativeWrapper ).

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

harish...@gmail.com

unread,
Nov 8, 2006, 12:46:45 AM11/8/06
to
I aslo try this code but it is giving error "win.function" is not a
function..

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?

Eric H. Jung

unread,
Nov 8, 2006, 1:08:39 AM11/8/06
to dev-ext...@lists.mozilla.org

From: "harish...@gmail.com" <harish...@gmail.com>

>>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/<<

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).

harish...@gmail.com

unread,
Nov 16, 2006, 2:18:25 AM11/16/06
to
u r right friend,,

But i called same functions for my IE toolbar.
so it should also call from firefox , means there should be some
method...

0 new messages