您应该使用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);
}