Chrome 扩展获取 element.data
我有问题,因为我无法获取 element.data。当我尝试在扩展中使用它时,它总是未定义,但是当我将相同的代码粘贴到 youtube 上的控制台(开发人员模式)时它的工作,在清单版本 2 中,我将此代码注入到内容脚本中,但在版本 3 中这是不可能的。
第一:console.log(el)
=>作品 第二:console.log(el.data) =>未定义
清单版本 3 服务工作者
function injectFc() {
document.querySelectorAll('yt-live-chat-text-message-renderer:not([profile-link])').forEach(el => {
console.log(el)
console.log(el.data)
})
}
setInterval(() => {
let queryOptions = { active: true, currentWindow: true };
chrome.tabs.query(queryOptions, (tb) => {
var actTid = tb[0]
chrome.scripting.executeScript(
{
target: {tabId: actTid.id, allFrames: true},
func: injectFc,
},
() => {
console.log(123);
};
});
}, 20000);
i have problem, because i cannot get element.data. Its always undefined, when i try to use it in extension, but when i paste the same code in console on youtube (developer mode) its working, In manifest version 2 i inject this code in content script but in version 3 its inpossible.
First: console.log(el)
=> works
Second: console.log(el.data)
=> undefined
Manifest version 3
service_worker
function injectFc() {
document.querySelectorAll('yt-live-chat-text-message-renderer:not([profile-link])').forEach(el => {
console.log(el)
console.log(el.data)
})
}
setInterval(() => {
let queryOptions = { active: true, currentWindow: true };
chrome.tabs.query(queryOptions, (tb) => {
var actTid = tb[0]
chrome.scripting.executeScript(
{
target: {tabId: actTid.id, allFrames: true},
func: injectFc,
},
() => {
console.log(123);
};
});
}, 20000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Chrome 扩展清单 v3 中,您仍然使用内容脚本。内容脚本允许您将其设置为特定域,例如www.youtube.com< /em>.请检查您使用的匹配项是否正确。
manifest.json
main.js
要收集直播消息,请检查官方直播 API。
In Chrome extension manifest v3 you still use the content script. The content script allows you to set it to a specific domain, such as www.youtube.com. Please check you are using the correct matches.
manifest.json
main.js
To collect the Live stream messages please check the official Live Streaming API.