0

有没有办法从谷歌扩展中获取页面标题的值?

4

2 回答 2

8

首先,你应该在你的声明tabsAPI 权限manifest.json

{
  "name": "My extension",
  ...
  "permissions": ["tabs"],
  ...
}

然后您将能够使用选项卡 API,您正在寻找chrome.tabs.getSelected(windowId, callback)方法。

要获取当前窗口的选定选项卡,您只需nullwindowId.

此方法将执行回调函数,传递一个Tab对象作为其第一个参数,您可以在其中简单地获取title属性:

chrome.tabs.getSelected(null,function(tab) { // null defaults to current window
  var title = tab.title;
  // ...
});
于 2010-05-14T23:38:26.397 回答
2

Notice that the above method mentioned by CMS is deprecated since Chrome 33.

You don't really need to specify the tabs permission in your manifest file since what you're doing here isn't some advanced action. You can perform most of the tabs actions without specifying the permission; only for some certain methods will you need to.

The new way of querying the currently selected tab is by the following code:

chrome.tabs.query({ active: true }, function (tab) {
  // do some stuff here
});

This will give you the selected tabs in all windows, if you have multiple windows open. If you want to get only the selected tab in the current window, use the following:

chrome.tabs.query({ active: true, currentWindow: true }, function (tab) {
  // do some other fanciful stuff here
});

For more details, refer to https://developer.chrome.com/extensions/tabs#method-query

于 2015-01-11T08:12:43.140 回答