firefox, how to get the content of location bar? (

2019-08-12 13:09发布

问题:

I want get what I put in the address bar, such as "hello world", not:

var url = tabs.activeTab.url  // I don't want this

before I press enter in the address bar. Is it possible to do it in an addon?

回答1:

If you define this function in your add-on:

function getURLBarText() {
  var { getMostRecentWindow } = require("sdk/window/utils");
  var urlBar = getMostRecentWindow().document.getElementById('urlbar');
  return urlBar.value;
}

Then calling getURLBarText() will return whatever is in your active window's url bar.