从内容脚本中,是否可以访问该选项卡的id
?
我想从内容脚本向后台页面发送一条消息,告诉我的扩展程序使用 chrome.tabs.*
API“使用此选项卡执行某些操作”。
需要一个 tabID
,当我的内容脚本可以简单地告诉它时,在后台页面中执行一堆逻辑来寻找 tabID
是没有意义的消息内容中的 tabID
。
最佳答案
选项卡 ID 会自动传入 MessageSender 内部对象:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("sent from tab.id=", sender.tab.id);
});
注:根据docs ,此属性并不总是可用:
This property will only be present when the connection was opened from a tab (including content scripts), and only if the receiver is an extension, not an app.
关于javascript - 从 Chrome 扩展程序中的内容脚本获取 "this"选项卡 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6202953/