获得“这个” Chrome扩展程序中内容脚本的标签ID? [英] Obtaining "this" tab ID from content script in Chrome extension?

查看:815
本文介绍了获得“这个” Chrome扩展程序中内容脚本的标签ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在内容脚本中,是否可以访问该标签的ID?我想从内容脚本向后台页面发送一条消息,告诉我的扩展程序使用chrome.tabs。* api使用此选项卡执行某些操作。 TabID是必需的,当我的内容脚本可以简单地告诉它在消息内容中的TabID时,在背景页面中做一堆逻辑来寻找TabID没有意义。

From a content script, is it possible to access that tab's ID? I want to send a message to the background page from the content script that tells my extension to "do something with this tab" using the chrome.tabs.* api. A TabID is needed, and there is no point in doing a bunch of logic in the background page to hunt for a TabID when my content script can simply tell it the TabID in the message contents.

推荐答案

标签ID自动传递到 MessageSender object:

Tab id is automatically passed inside MessageSender object:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    console.log("sent from tab.id=", sender.tab.id);
});

这篇关于获得“这个” Chrome扩展程序中内容脚本的标签ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆