0

I'm looking to get the current url of the selected tab in firefox, I've got a Firefox SDK Panel directed at a contentURL and I've been using Javascript to try and fetch the current url;

window.content.location.href

But unfortunately it does not work unless im on my contentURL page any other pages and it displays nothing.

Thanks!

sparkones
  • 39
  • 1
  • 2
  • 9

2 Answers2

1

In JavaScript:

window.location.href

In php:

$_SERVER["REQUEST_URI"]
MrRP
  • 822
  • 2
  • 10
  • 25
  • Thanks for the quick reply, I tried both and they only return the contentURL of the SDK panel – sparkones Apr 21 '13 at 16:27
  • Okey, may this could help you: http://stackoverflow.com/questions/11594576/getting-current-browser-url-in-firefox-addon – MrRP Apr 21 '13 at 16:32
  • I've searched all over stackoverflow and im still unable to solve the problem, the only one i've had luck with is window.content.location.href and it only works on my contentURL page, once i try it on another page it does not alert me anything. – sparkones Apr 21 '13 at 21:26
1

You can get the URL like this

var url = require('sdk/tabs').activeTab.url;

This works only inside your add-on code, not inside content scripts. For the difference see this guide.

If you want to trigger the lookup from the content script and/or want to use the URL in the content script, you will have to communicate using "port".

Here is an example in which the url of the current tab is displayed in the panel upon clicking a button.

lib/main.js (add-on code):

const data = require('sdk/self').data;

var panel = require('sdk/panel').Panel({
    contentURL: data.url('panel.html'),
    contentScriptFile: data.url('panel.js')
});

panel.port.on('get url', function () {
    panel.port.emit('return url', require('sdk/tabs').activeTab.url);
});

panel.show();

data/panel.js (content script):

var button = document.getElementById('button');
var urlspan = document.getElementById('url');

button.addEventListener('click', function () {
    self.port.emit('get url');
});

self.port.on('return url', function (url) {
    urlspan.textContent = url;
});

data/panel.html:

<!DOCTYPE html>
<html>
  <head>
        <meta charset="utf-8" />
    <title>panel</title>
  </head>
  <body>
    <button id="button">
      Get Current URL
    </button>
    <br>
    Current URL: <span id="url"></span>
  </body>
</html>
  • Is it possible to append that to the ContentURL on click of a button? Sorry, i don't know much about writing sdk – sparkones Apr 23 '13 at 03:11
  • Append what to the ContentURL? And what sort of button do you mean? Inside your panel? – Martin Puppe Apr 23 '13 at 14:15
  • Yeah pretty much i would have a button on a html form "Get Current URL" then it would get the selected tab url and append it to the url ex "www.domain.com/geturl.php?url=CurrentURL" – sparkones Apr 23 '13 at 23:21
  • I've updated my answer with an example. The main point is how the URL is passed between add-on code and content script using "port". – Martin Puppe Apr 24 '13 at 20:12
  • It works perfectly! Thanks Martin Puppe, i appreciate all the time you've spent to help me. – sparkones Apr 25 '13 at 01:11
  • One other question if i goto another page in the panel and then go back or refresh the page the button doesn't work, why does it do that? – sparkones Apr 27 '13 at 23:37