views:

3121

answers:

5

I'm writing a sidebar extension for Firefox and need a way to get the URL of the current page so I can check it against a database and display the results. How can I do this?

+1  A: 
window.top.getBrowser().selectedBrowser.contentWindow.location.href;

might work, otherwise I think you need to use:

var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIWebNavigation)
                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                   .rootTreeItem
                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIDOMWindow);

mainWindow.getBrowser().selectedBrowser.contentWindow.location.href;
Wimmel
A: 

https://developer.mozilla.org/En/Working_with_windows_in_chrome_code

If you need to access the main browser from the code running in a sidebar, you'll something like what Wimmel posted, except the last line could be simplified to

mainWindow.content.location.href

(alternatively you could use 's API returning an nsIURI).

Depending on your task, it might make sense to run the code in the browser window instead (e.g. in a page load handler), then it can access the current page via the content shortcut and the sidebar via document.getElementById("sidebar").contentDocument or .contentWindow.

Nickolay
A: 

Hallo,

I have tried to implement this in JavaScript, because I need that in my project too, but all three possible solutions didn't work. I have also implemented a small site to test it, but this also didn't work.

Here is the source code of the small site:

<html>
<head>
<title>Test</title>
<script type="text/javascript">
function Fall1 () {

    alert(window.top.getBrowser().selectedBrowser.contentWindow.location.href);
}

function Fall2() {
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIWebNavigation)
                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                   .rootTreeItem
                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIDOMWindow);

alert(mainWindow.getBrowser().selectedBrowser.contentWindow.location.href);
}

function Fall3() {
alert(document.getElementById("sidebar").contentWindow.location.href);
}

</script>
</head>
<body>
<form name="Probe" action="">

<input type="button" value="Fall1"
onclick="Fall1()">

<input type="button" value="Fall2"
onclick="Fall2()">

<input type="button" value="Fall3"
onclick="Fall13()">
</form>
</body>
</html>
Gordon
Javascript in a webpage doesn't behave the same way as Javascript in a Firefox addon. When you're writing a webpage, `document` refers to that webpage. When you're writing an addon, `document` refers to the XUL document that the script is embedded in.
MatrixFrog
+3  A: 

I stumbled over this post while looking for an answer to the same question.

Actually I think it's as easy as

alert(window.content.location.href)

See also https://developer.mozilla.org/en/DOM/window.content

raymi
This seems to work if you're overlaying the main browser window (browser.xul) but I don't know if it will work in other cases.
MatrixFrog
+2  A: 

This seems to work fine for me

function getCurrentURL{

    var currentWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");

    var currBrowser = currentWindow.getBrowser();
    var currURL = currBrowser.currentURI.spec;

    return currURL;
}
Kapil
you should add `function getCurrentURL(){` and `}` to the code layout. They're part of the code.
dierre
oops my bad - fixed it.
Kapil
+1 This code really works, thanks :)
sumit_programmer
this is a good choice as when a new tab is open is gets that URL instead in case of @Wimmel it doesn't. Thanks.
Meher Ranjan