0
  1. 文档onActivated描述为:chrome.tabs.onActivated.addListener(function(object activeInfo) {...});

    当窗口中的活动选项卡更改时触发。[...]

  2. 作为对他自己对另一个问题的回答的评论,@RobW 说:

    正确的方法是 chrome.tabs.onActiveChanged,与不存在的 chrome.tabs.onActivated 具有相同的签名。

  3. 最后,一个示例扩展使用onSelectionChanged似乎是出于相同的目的:

    chrome.tabs.onSelectionChanged.addListener(function(tabId) {
      lastTabId = tabId;
      chrome.pageAction.show(lastTabId);
    });
    

onSelectionChangedonActiveChanged 有什么区别?为什么没有关于onSelectionChanged的​​文档?应该用什么来收听标签更改?

4

1 回答 1

3

您应该使用onActivated(仅限 Chrome 18+)来监听标签更改。onActiveChanged并且onSelectionChanged是已弃用的事件。

在源代码中(见附件),这些事件的目的,以及它们的用法。为了澄清,我已经用一个小演示更新了我之前的答案。

这是源代码的相关部分:

void ExtensionBrowserEventRouter::ActiveTabChanged(
    TabContentsWrapper* old_contents,
    TabContentsWrapper* new_contents,
    int index,
    bool user_gesture) {
  ListValue args;
  int tab_id = ExtensionTabUtil::GetTabId(new_contents->web_contents());
  args.Append(Value::CreateIntegerValue(tab_id));

  DictionaryValue* object_args = new DictionaryValue();
  object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue(
      ExtensionTabUtil::GetWindowIdOfTab(new_contents->web_contents())));
  args.Append(object_args);

  // The onActivated event replaced onActiveChanged and onSelectionChanged. The
  // deprecated events take two arguments: tabId, {windowId}.
  std::string old_json_args;
  base::JSONWriter::Write(&args, &old_json_args);

  // The onActivated event takes one argument: {windowId, tabId}.
  std::string new_json_args;
  args.Remove(0, NULL);
  object_args->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id));
  base::JSONWriter::Write(&args, &new_json_args);

  Profile* profile = new_contents->profile();
  DispatchEvent(profile, events::kOnTabSelectionChanged, old_json_args);
  DispatchEvent(profile, events::kOnTabActiveChanged, old_json_args);
  DispatchEvent(profile, events::kOnTabActivated, new_json_args);
}
于 2012-04-06T07:44:55.833 回答