如何在选项卡中获取所有 frameId?
How to get all frameId's in a tab?
有没有办法使用 chrome.tabs.executeScript
将所有 frameId
都放在一个选项卡中?
我试过 executeScript()
然后让代码向后台发送一条消息,后台将其发送回 frameId
的内容,但它是异步的。而且,executeScript()
需要同步 return.
是的,您可以使用 chrome.tabs.executeScript()
(MDN).As it sounds like you have determined, it is a bit convoluted to do so. You could use chrome.tabs.executeScript()
with the option allFrames:true
to inject a content script into all frames in the tab. Your content script would then need to send a message back to the background context using chrome.runtime.sendMessage()
(MDN). The frameId
will then be available for each frame as a property of the Object passed as the second argument, the sender
(MDN), to your chrome.runtime.onMessage
(MDN) 侦听器获取选项卡的 frameId 列表。您将需要自己积累列表。
更好的方法:使用 chrome.webNavigation.getAllFrames()
(MDN)
正如 wOxxOm 在评论中提到的那样,从 chrome.webNavigation.getAllFrames()
(MDN) 获得它会容易得多。
无法使用同步界面获取选项卡的 frameId 列表。正如 wOxxOm 还提到的,唯一的方法是在您需要之前获取信息。如果您需要它来调用 chrome.tabs.executeScript()
,那么您可以从 chrome.webNavigation.getAllFrames()
.
的回调中调用 chrome.tabs.executeScript()
有没有办法使用 chrome.tabs.executeScript
将所有 frameId
都放在一个选项卡中?
我试过 executeScript()
然后让代码向后台发送一条消息,后台将其发送回 frameId
的内容,但它是异步的。而且,executeScript()
需要同步 return.
是的,您可以使用 chrome.tabs.executeScript()
(MDN).As it sounds like you have determined, it is a bit convoluted to do so. You could use chrome.tabs.executeScript()
with the option allFrames:true
to inject a content script into all frames in the tab. Your content script would then need to send a message back to the background context using chrome.runtime.sendMessage()
(MDN). The frameId
will then be available for each frame as a property of the Object passed as the second argument, the sender
(MDN), to your chrome.runtime.onMessage
(MDN) 侦听器获取选项卡的 frameId 列表。您将需要自己积累列表。
更好的方法:使用 chrome.webNavigation.getAllFrames()
(MDN)
正如 wOxxOm 在评论中提到的那样,从 chrome.webNavigation.getAllFrames()
(MDN) 获得它会容易得多。
无法使用同步界面获取选项卡的 frameId 列表。正如 wOxxOm 还提到的,唯一的方法是在您需要之前获取信息。如果您需要它来调用 chrome.tabs.executeScript()
,那么您可以从 chrome.webNavigation.getAllFrames()
.
chrome.tabs.executeScript()