【发布时间】:2011-10-15 19:48:04
【问题描述】:
我正在制作一个 google chrome 扩展程序,我需要获取当前页面的 URL 和标题。我怎样才能做到这一点?
【问题讨论】:
标签: javascript google-chrome-extension
我正在制作一个 google chrome 扩展程序,我需要获取当前页面的 URL 和标题。我怎样才能做到这一点?
【问题讨论】:
标签: javascript google-chrome-extension
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。关于tab 对象,请阅读here。
注意: chrome.tabs.getSelected has been deprecated since Chrome 16。正如文档所建议的,chrome.tabs.query() 应与参数 {'active': true} 一起使用以选择活动选项卡。
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
tabs[0].url; //url
tabs[0].title; //title
});
【讨论】:
getSelected() 方法自 Google Chrome 16 起已被弃用(但官方文档中的许多文章尚未更新)。 Official message is here。要获取在指定窗口中选择的选项卡,请使用 chrome.tabs.query() 和参数 {'active': true}。所以现在应该是这样的:
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
console.log(tabs[0].url);
console.log(tabs[0].title);
});
编辑:tabs[0] 是第一个活动标签。
【讨论】: