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

查看:28
本文介绍了获得“这个"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.

推荐答案

Tab id 在 MessageSender 对象:

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天全站免登陆