Google Chrome扩展程序获取页面信息
问题描述:
我正在制作Google Chrome浏览器扩展程序,并且需要获取当前网页的网址和标题。
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
});
欲了解更多信息,请阅读 chrome.tabs
。关于选项卡
对象,请阅读此处。
For more please read chrome.tabs
. About the tab
object, read here.
注: chrome.tabs.getSelected
已被弃用 自Chrome 16以来。正如文档中的建议, chrome.tabs.query()
应与参数 {'active':true}
一起使用以选择活动选项卡。
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
});