Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the currently opened tab's URL in my page action popup?

I want to create an extension for automatically logging into my servers. So I created a background page to check the current URL and if it conforms to my URL regex, I'll display the page action icon. On click of the page action I'm opening a popup with some fields. I need to get the currently opened URL and fill it in one of the fields in the popup(like when we click the standard bookmark page action, the URL gets automatically filled in the popup that opens). How can I do something like this in chrome extensions? I tried Message Passing from the background page to the popup html but it is not working. Is it possible to send messages like that? Also I tried setting onload for the popup html file but i noticed that it is not triggering. Please suggest a suitable method to deal with this scenario.

like image 530
Jophin Joseph Avatar asked May 02 '12 12:05

Jophin Joseph


People also ask

How do I get a new tab URL?

How to Open Hyperlinks in a New Browser Tab or Window. The short answer is: just add a target="_blank" attribute to your links (anchor tags). Now when your visitors click that link, it will open in a new window or tab (depending on which web browser they are using and how they configured that browser).

How do I get a link to open all tabs in Chrome?

1) In the Chrome browser with all the tabs open, click the menu icon in the top left corner. 2) In the Chrome menu, click on Bookmarks. 3) From the menu that opens, select Bookmark all tabs. 4) In the dialogue box that opens, assign a name to the collection of Bookmarks.

What is URL tab?

In a web browser, the address bar (also location bar or URL bar) is a GUI widget that shows the current URL. The user can type a URL into the bar to navigate to a chosen website; in most modern browsers, non-URLs are automatically sent to a search engine.


2 Answers

Use chrome.tabs.query with the following parameters:

  • queryInfo object:
    • active: true - To get the active tab
    • lastFocusedWindow: true - To select the active window
  • callback function:
    This function receives one argument: An array of matched tabs. Since only one window can be active, and one tab within this window, the array has only one element. This element is an object with the Tab signature.

Code snippet:

// Do NOT forget that the method is ASYNCHRONOUS
chrome.tabs.query({
    active: true,               // Select active tabs
    lastFocusedWindow: true     // In the current window
}, function(array_of_Tabs) {
    // Since there can only be one active tab in one active window, 
    //  the array has only one element
    var tab = array_of_Tabs[0];
    // Example:
    var url = tab.url;
    // ... do something with url variable
});

The activeTab permission is sufficient for this to work.

like image 173
Rob W Avatar answered Oct 14 '22 15:10

Rob W


You can also use promises for a cleaner way of retrieving a tab:

  getCurrentTab().then(function(tab){
    // Do something w/tab
  });

  function getCurrentTab(){
    return new Promise(function(resolve, reject){
      chrome.tabs.query({
        active: true,               // Select active tabs
        lastFocusedWindow: true     // In the current window
      }, function(tabs) {
        resolve(tabs[0]);
      });
    });
  }
like image 41
dipole_moment Avatar answered Oct 14 '22 17:10

dipole_moment