【问题标题】:Google Chrome Extension get page information谷歌浏览器扩展获取页面信息
【发布时间】:2011-10-15 19:48:04
【问题描述】:

我正在制作一个 google chrome 扩展程序,我需要获取当前页面的 URL 和标题。我怎样才能做到这一点?

【问题讨论】:

标签: javascript google-chrome-extension


【解决方案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
});

更多信息请阅读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
});

【讨论】:

  • 对于最近看到这个的人来说,值得指出的是 chrome.tabs.getSelected() 已被弃用,康斯坦丁的答案现在是这个问题的正确答案。
  • 请注意,“currentWindow”并不是指您可能认为它指的内容。您很可能希望将“lastFocusedWindow”(焦点窗口)与“active”(焦点选项卡)结合使用。
【解决方案2】:

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] 是第一个活动标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2015-04-17
    相关资源
    最近更新 更多