Chrome 扩展获取 element.data

发布于 2025-01-15 20:19:06 字数 780 浏览 4 评论 0原文

我有问题,因为我无法获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2025-01-22 20:19:06

在 Chrome 扩展清单 v3 中,您仍然使用内容脚本。内容脚本允许您将其设置为特定域,例如www.youtube.com< /em>.请检查您使用的匹配项是否正确。

manifest.json

{
  "name": "Inject Code",
  "action": {},
  "manifest_version": 3,
  "version": "0.1",
  "description": "Inject the code from content script",
  "content_scripts": [
    {
      "matches": ["https://www.youtube.com/*"],
      "js": ["main.js"],
      "run_at": "document_end",
      "all_frames": true
    }
  ],
  "permissions": ["activeTab"],
  "host_permissions": ["https://www.youtube.com"]
}

ma​​in.js

console.log("start the script");

function injectFc() {
  console.log("run injectFc");
  document.querySelectorAll('yt-live-chat-text-message-renderer:not([profile-link])').forEach(el => {
    console.log(el);
    console.log(JSON.stringify(el.data));
  })
}

window.setInterval(function () { injectFc(); }, 20000);

要收集直播消息,请检查官方直播 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

{
  "name": "Inject Code",
  "action": {},
  "manifest_version": 3,
  "version": "0.1",
  "description": "Inject the code from content script",
  "content_scripts": [
    {
      "matches": ["https://www.youtube.com/*"],
      "js": ["main.js"],
      "run_at": "document_end",
      "all_frames": true
    }
  ],
  "permissions": ["activeTab"],
  "host_permissions": ["https://www.youtube.com"]
}

main.js

console.log("start the script");

function injectFc() {
  console.log("run injectFc");
  document.querySelectorAll('yt-live-chat-text-message-renderer:not([profile-link])').forEach(el => {
    console.log(el);
    console.log(JSON.stringify(el.data));
  })
}

window.setInterval(function () { injectFc(); }, 20000);

To collect the Live stream messages please check the official Live Streaming API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文