Chrome扩展程序 – 在sidePanel中获取标签页ID?

huangapple go评论118阅读模式
英文:

Chrome Extension - get tab ID in sidePanel?

问题

我一直在我的Chrome扩展中使用消息传递来获取我的UI中的标签ID。

内容脚本注入了一个加载React应用程序的iframe。React发送一条消息:

const tabId = await chrome.runtime.sendMessage({
  message: MSG_GET_TAB_ID,
});

而后台脚本做出响应:

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  const { message } = request;
  const tabId = sender?.tab?.id;
  if (tabId === undefined) return;

  if (message === MSG_GET_TAB_ID) {
    sendResponse(tabId);
  }
});

如果我记录tabId的结果,我可以看到它已经生效。

然而,现在我想要使用侧边栏:
https://developer.chrome.com/docs/extensions/reference/sidePanel/#type-PanelOptions

我已经从内容脚本中删除了注入iframe的代码,而是只需将以下内容添加到manifest.json中(存储已经存在):

"permissions": [
  "storage",
  "sidePanel"
],
"side_panel": {
  "default_path": "./src/ui/popup.html"
},

如果我在Chrome中打开侧边栏并选择我的应用程序,我可以看到React正在正确渲染,但是tabIdundefined

通过记录后台脚本中的sender对象,我可以看到该对象具有idurlorigin,但不再具有tab,而tab具有id。因此,似乎消息侦听器不知道侧边栏是在哪个标签上打开的。

是否有办法让消息传递像以前一样工作,或者从侧边栏上下文中找出标签ID的另一种方法?

英文:

Ive been using message passing in my Chrome Extension to get the tab ID in my UI.

The content script injects an iframe which loads a React app. React sends a message:

const tabId = await chrome.runtime.sendMessage({
  message: MSG_GET_TAB_ID,
});

Which the background script responds to:

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  const { message } = request;
  const tabId = sender?.tab?.id;
  if (tabId === undefined) return;

  if (message === MSG_GET_TAB_ID) {
    sendResponse(tabId);
  }
});

If I log out the results of tabId I can see its worked.

However now I want to use the sidePanel:
https://developer.chrome.com/docs/extensions/reference/sidePanel/#type-PanelOptions

I've removed the code to inject the iframe via the content script and instead just add this to the manifest.json (storage was already there):

  "permissions": [
    "storage",
    "sidePanel"
  ],
    "side_panel": {
        "default_path": "./src/ui/popup.html"
    },

If I open the side panel in Chrome and choose my app I can see that React is rendering correctly, however tabId is undefined.

From logging out the sender object in the background script I can see the object has id, url and origin but no longer has tab which had id. So it seems the message listener doesn't know which tab the side panel was opened on.

Is there a way to get message passing working as before or another way of finding out the tab id from within the side panels context?

答案1

得分: 0

我相信问题出在侧边栏与背景脚本处于相同的上下文中,因此消息传递在它们之间无法正常工作。

就像wOxxOm在评论中所说,使用chrome.tabs.query是有效的。

英文:

I believe the issue is happening as the sidePanel is in the same context as the background script, so message passing doesn't work between the 2.

As wOxxOm said in the comments using chrome.tabs.query works.

huangapple
  • 本文由 发表于 2023年6月12日 20:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定