开发者

Google Chrome Extension get page information

开发者 https://www.devze.com 2023-03-24 06:28 出处:网络
I\'m making a google chrome extension, and I need to get the current page URL and title. How can I开发者_如何学运维 achieve this?chrome.tabs.getSelected(null, function(tab) { //<-- \"tab\" has all

I'm making a google chrome extension, and I need to get the current page URL and title. How can I开发者_如何学运维 achieve this?


chrome.tabs.getSelected(null, function(tab) { //<-- "tab" has all the information
    console.log(tab.url);       //returns the url
    console.log(tab.title);     //returns the title
});

For more please read chrome.tabs. About the tab object, read here.


Note: chrome.tabs.getSelected has been deprecated since Chrome 16. As the documentation has suggested, chrome.tabs.query() should be used along with the argument {'active': true} to select the active tab.

chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    tabs[0].url;     //url
    tabs[0].title;   //title
});


The method getSelected() has been deprecated since Google Chrome 16 (but many articles in the official documentation had not yet been updated). Official message is here. To get the tab that is selected in the specified window, use chrome.tabs.query() with the argument {'active': true}. So now it should looks like this:

chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
  console.log(tabs[0].url);
  console.log(tabs[0].title);
});

Edit: tabs[0] is the first active tab.

0

精彩评论

暂无评论...
验证码 换一张
取 消