繁体   English   中英

Google Chrome扩展程序可获取页面信息

[英]Google Chrome Extension get page information

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

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对象,请阅读此处


注意: chrome.tabs.getSelected 自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 16以来,方法getSelected()已被弃用(但官方文档中的许多文章尚未更新)。 官方消息在这里 要获取在指定窗口中选择的选项卡,请使用带参数{'active': true} chrome.tabs.query() 所以现在看起来应该是这样的:

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

编辑tabs[0]是第一个活动选项卡。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM