Google Chrome Extension get page information

浪尽此生 提交于 2019-12-30 04:24:13

问题


I'm making a google chrome extension, and I need to get the current page URL and title. How can I achieve this?


回答1:


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
});



回答2:


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.



来源:https://stackoverflow.com/questions/6871309/google-chrome-extension-get-page-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!