Getting current browser url in Firefox Addon
Asked Answered
C

7

8

I within a panel and I want to get the current browser URL. Nothing so far works. Here's what I've tested:

Only thing that even returns anything, I get something like resource://jid0-18z0ptaugyu0arjkaoywztggyzg-at-jetpack/ and then my current panel resource. Obviously this is a scope problem but I don't know how to refer to the actual browser.

window.location.href 

I've tried literally everything in the biggest Stack Overflow thread on this: Get current page URL from a firefox sidebar extension. None of them return anything.

If it helps, I am using the Firefox Addon Builder.

Catchfly answered 21/7, 2012 at 17:54 Comment(0)
H
8
// you need to use this service first
var windowsService = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);

// window object representing the most recent (active) instance of Firefox
var currentWindow = windowsService.getMostRecentWindow('navigator:browser');

// most recent (active) browser object - that's the document frame inside the chrome
var browser = currentWindow.getBrowser();

// object containing all the data about an address displayed in the browser
var uri = browser.currentURI;

// textual representation of the actual full URL displayed in the browser
var url = uri.spec;
Homoio answered 21/7, 2012 at 21:7 Comment(1)
@anunixercoder I doubt it will work. This is obsolete. If you want to get current URL in current Firefox extension, just add content script (developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/…) and ask directly for document.location from it.Homoio
A
17

getting the URL from a sidebar or popup

To retrieve the URL from a sidebar or popup requires tab permissions

"permissions": [
    "tabs"
  ]

then you need to find the tab you want. If you just want the active tab this would work fine, for anything more advanced I'd look here.

function getPage(){
  browser.tabs.query({currentWindow: true, active: true})
    .then((tabs) => {
      console.log(tabs[0].url);
  })
}

getting the URL from injected javascript

If you want the URL for a background task I suggest this method as you do not need permissions.

this will give you a background script and then inject a script onto almost any webpage on the internet.

"background": {
    "scripts": ["background.js"]
},

"content_scripts": [
    {
      "matches": ["https://www.*"],
      "js": ["modify-page/URL.js"]
    }
  ],

this will be injected into webpages through the URL js and will send a message to your background js to use.

var service= browser.runtime.connect({name:"port-from-cs"});

service.postMessage({location: document.URL});

This code is in your background js and will collect each new page's url as it changes.

var portFromCS;

function connected(p) {
  portFromCS = p;
  portFromCS.onMessage.addListener(function(m) {
    if(m.location !== undefined){
      console.log(m.location);
    }
  });
}

browser.runtime.onConnect.addListener(connected);
Astern answered 25/1, 2018 at 16:12 Comment(0)
H
8
// you need to use this service first
var windowsService = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);

// window object representing the most recent (active) instance of Firefox
var currentWindow = windowsService.getMostRecentWindow('navigator:browser');

// most recent (active) browser object - that's the document frame inside the chrome
var browser = currentWindow.getBrowser();

// object containing all the data about an address displayed in the browser
var uri = browser.currentURI;

// textual representation of the actual full URL displayed in the browser
var url = uri.spec;
Homoio answered 21/7, 2012 at 21:7 Comment(1)
@anunixercoder I doubt it will work. This is obsolete. If you want to get current URL in current Firefox extension, just add content script (developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/…) and ask directly for document.location from it.Homoio
S
2

I believe using the API tabs from SDK can do this:

// Get the active tab's title.
var tabs = require("tabs");
console.log("title of active tab is " + tabs.activeTab.title);
Stibine answered 23/7, 2012 at 5:57 Comment(0)
A
1

The API shows that in order to retrieve the current tabs URL

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

   console.log('active: ' + tabs.activeTab.url);

This will print to the console: " active: http://www.example.com "

Atalya answered 21/11, 2013 at 20:49 Comment(0)
O
0

window gives you the current window. top gives you the outermost frame.

Ornis answered 21/7, 2012 at 18:19 Comment(1)
window.top.location.href returns the same thing.Catchfly
W
0

For the addon you can use following code to get URL from the address bar

Javascript code:

function Doit(){
   var link = window.top.getBrowser().selectedBrowser.contentWindow.location.href;
   alert (link); 
}

HTML code:

<div onclick = "Doit()">Generate URL</div>

This will generate the URL presented on the present tab of the browser.

Wharfage answered 15/1, 2015 at 9:15 Comment(0)
I
0

You can now access the current URL directly via document.location. Example:

console.log(`Current URL is ${document.location}`);

The special magic is no longer required.

Infamous answered 1/10, 2023 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.