Intercepting URL in Firefox extension

2006-12-19 Thread Andrei Korostelev
I need a Firefox extension which performs some actions (redirect and
others) before the URL is open. Simply using Mozilla's
window.addEventListener("load", Handler, ..) does not work since I have
to catch the URL *before* the page is loaded. Not too exotic task on my
thought. I also read a couple of relevant posts here, however I rushed
into problems.

What exactly I did (I use Firefox 2.0, WinXP SP2):

I implemented nsIURIContentListener interface.

// MyOverlay.js

//implementation of nsIURIContentListener
var myListener =
{
QueryInterface: function(iid)
{
alert("QueryInterface "+iid);
if (iid.equals(Components.interfaces.nsIURIContentListener) ||
iid.equals(Components.interfaces.nsISupportsWeakReference)
||
iid.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},
onStartURIOpen: function(uri)
{
alert("onStartURIOpen "+uri);
return false;
},
doContent: function(contentType, isContentPreferred, request,
contentHandler)
{
alert("doContent");
return false;
},
isPreferred: function(contentType, desiredContentType)
{
alert("isPreferred");
return false;
},
canHandleContent: function(contentType, isContentPreferred,
desiredContentType)
{
alert("canHandleContent");
return false;
},
GetWeakReference: function()
{
alert("GetWeakReference");
return this;
   }
}
// I set up the content listener: attempt 1
var wnd =
window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)

.getInterface(Components.interfaces.nsIWebNavigation)

.QueryInterface(Components.interfaces.nsIDocShell)

.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
wnd.parentURIContentListener = myListener;


I get the following error the java script error log:
Error: uncaught exception: [Exception... "Cannot modify properties of a
WrappedNative"  nsresult: "0x80570034
(NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)"  location: "JS frame ::
chrome://foxmon/content/FoxmonOverlay.js ::  :: line 44"
data: no]

Line 44 is docShell.parentURIContentListener = myListener;

Trying to get the exact nsIWebBrowser interface from the window using
QueryInterface did not succeed. After googling for a while I found
another method to get nsIWebBrowser interface:

// Setting up the content listener: attempt 2
var wnd =
window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)

.getInterface(Components.interfaces.nsIWebNavigation)

.QueryInterface(Components.interfaces.nsIDocShell)

.QueryInterface(Components.interfaces.nsIInterfaceRequestor);

var treeItem =
wnd.QueryInterface(Components.interfaces.nsIDocShellTreeItem);
var treeOwner = treeItem.treeOwner;
var interfaceRequestor =
treeOwner.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
var webBrowserChrome =
interfaceRequestor.getInterface(Components.interfaces.nsIWebBrowserChrome);
if (webBrowserChrome)
{
var chromeFlags = webBrowserChrome.chromeFlags;
var res = chromeFlags &
Components.interfaces.nsIWebBrowserChrome.CHROME_ALL;
var res2 = chromeFlags &
Components.interfaces.nsIWebBrowserChrome.CHROME_DEFAULT;
if ( res == Components.interfaces.nsIWebBrowserChrome.CHROME_ALL ||
res2 == Components.interfaces.nsIWebBrowserChrome.CHROME_DEFAULT)
{
 var wb = webBrowserChrome.webBrowser;
}
}

This code works fine until the selected line where an attempt to get
webBrowser member of webBrowserChromw returned me

Error: uncaught exception: [Exception... "Component returned failure
code: 0x80004005 (NS_ERROR_FAILURE) [nsIWebBrowserChrome.webBrowser]"
nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame ::
chrome://foxmon/content/FoxmonOverlay.js ::  :: line 57"
data: no]

After that I tried the following

// Setting up the content listener: attempt 3
var uriLoader =
Components.classes["@mozilla.org/uriloader;1"].getService(Components.interfaces.nsIURILoader);
uriLoader.registerContentListener(myListener);

Things went slightly better: I traced (with alerts) two calls to
myListener.QueryInterface: first one was a request for nsISupports, the
second one was a request for
nsISupportsWeakReference when the Firefox started. However
onStartURIOpen was not called when I pointed my browser to my favourite
URL.

Finally I tried to set the content listener like that:

// Setting up the content listener: attempt 4
var myIWebBrowser =
(Components.classes["@mozilla.org/embedding/browser/nsWebBrowser;1"]).getService(Components.interfaces.nsIWebBrowser);

if(!myIWebBrowser) alert("Not Created");
myIWebBrowser.parentURIContentListener = myListener;

And, after tracing the call to nsISupports interface in
myListener.QueryInterface  I got the same error as in my first attempt
Error: uncaught exception: [Exception... "Component returned failure
code: 0x80004005 (NS_ERROR_FAILU

Re: Intercepting URL in Firefox extension

2006-12-19 Thread Andrei Korostelev
On Dec 19, 3:58 pm, Boris Zbarsky <[EMAIL PROTECTED]> wrote:
> Andrei Korostelev wrote:
> > // I set up the content listener: attempt 1
> > var wnd =
> > window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
>
> > .getInterface(Components.interfaces.nsIWebNavigation)
>
> > .QueryInterface(Components.interfaces.nsIDocShell)
>
> > .QueryInterface(Components.interfaces.nsIInterfaceRequestor);
> > wnd.parentURIContentListener = myListener;parentURIContentListener lives on 
> > nsIURIContentListener, no?  This seems to be
> missing a getInterface for that interface after you QI the docshell to
> nsIInterfaceRequestor.  This should work with that change.
> 
> -Boris


Tnanks a lot, Boris!
It works with that change.

___
dev-embedding mailing list
dev-embedding@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-embedding


Get firefox default profile directory from non-extension

2006-12-20 Thread Andrei Korostelev
Hi,

the following code, when called from Firefox extension, retrieves
firefox profile directory location.
nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
if (NS_FAILED(rv)) return;
nsCOMPtr fileLocator =
do_GetService("@mozilla.org/file/directory_service;1", &rv);
if (NS_FAILED(rv)) return;
if (fileLocator)
{
nsCOMPtr dir;
rv = fileLocator->Get("ProfD", NS_GET_IID(nsILocalFile),
getter_AddRefs(dir));
if (NS_FAILED(rv)) return; // here of course fails if called from
non-extension
if (dir)
{
nsEmbedCString path;
dir->GetNativePath(path);
aDir = path.get();
}
}

However I am wondering how to get the same from a standalone
application (not extenson).

Why do I need it for? I am making an installer which would install my
firefox extension from the command-line.
OS: Windows XP. Firefox 2.0

___
dev-embedding mailing list
dev-embedding@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-embedding


Re: Get firefox default profile directory from non-extension

2006-12-20 Thread Andrei Korostelev
On Dec 20, 4:06 pm, "Andrei Korostelev" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> the following code, when called from Firefox extension, retrieves
> firefox profile directory location.
> nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
> if (NS_FAILED(rv)) return;
> nsCOMPtr fileLocator =
> do_GetService("@mozilla.org/file/directory_service;1", &rv);
> if (NS_FAILED(rv)) return;
> if (fileLocator)
> {
> nsCOMPtr dir;
> rv = fileLocator->Get("ProfD", NS_GET_IID(nsILocalFile),
> getter_AddRefs(dir));
> if (NS_FAILED(rv)) return; // here of course fails if called from
> non-extension
> if (dir)
> {
> nsEmbedCString path;
> dir->GetNativePath(path);
> aDir = path.get();
> }
>
> }However I am wondering how to get the same from a standalone
> application (not extenson).
>
> Why do I need it for? I am making an installer which would install my
> firefox extension from the command-line.
> OS: Windows XP. Firefox 2.0

Answering to myself ;)

Look into profiles.ini in %APPDATA%\Mozilla\Firefox.

___
dev-embedding mailing list
dev-embedding@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-embedding


Redirect URL

2007-01-11 Thread Andrei Korostelev
I need to have a Firefox extension which redirects certain URLs to
another location. AFAIK using OnStartURIOpen() is not appropriate since
it only allows or disallows the url load and cannot handle redirection.
Any ideas?

Firefox 2.0.

___
dev-embedding mailing list
dev-embedding@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-embedding