我想等到 Shadow DOM 元素加载完毕

发布于 2025-01-16 06:37:18 字数 1299 浏览 1 评论 0原文

我正在开发一个 Chrome 扩展程序。

我想开发一个功能,当您将鼠标悬停在页面上的某个元素时,该元素周围会出现一个框架,但它不适用于 Shadow DOM 元素。

我相信这是因为在加载事件触发时,shadow dom 元素没有加载到页面上。

对于具有嵌套影子 dom 的页面尤其如此。 例如) https://developer.servicenow.com/dev.do

但是,我愿意不知道如何检测整个页面,包括shadow dom,已经完成加载。

如果您知道解决方案,请告诉我。

function addAttribute(e) {
  const el = e.path || (e.composedPath && e.composedPath());
  el.setAttribute('style', "outline: solid 4px !important");
}

function removeAttribute(e) {
  const el = e.path || (e.composedPath && e.composedPath());
  el.removeAttribute('style', "outline: solid 4px !important");
}

function addEventListenerToShadow(root) {
  const tw = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
  
  do {
    const currentNode = tw.currentNode;
    if (currentNode instanceof ShadowRoot) continue;
    if (currentNode.shadowRoot) {
      currentNode.shadowRoot.addEventListener('mouseover', addAttribute)
      currentNode.shadowRoot.addEventListener('mouseout', removeAttribute)
      addEventListenerToShadow(currentNode.shadowRoot);
    }
  } while (tw.nextNode());
}

window.onload = addEventListenerToShadow(document.body);

I am developing a Chrome extension.

I would like to develop a feature that when you Hover an element on the page, a frame appears around the element, but it doesn't work with the Shadow DOM element.

I believe this is due to the fact that the shadow dom element is not loaded on the page at the time the load event fires.

This is especially true for pages with nested shadow doms.
ex.) https://developer.servicenow.com/dev.do

However, I do not know how to detect that the entire page, including the shadow dom, has finished loading.

Please let me know if you know of a solution.

function addAttribute(e) {
  const el = e.path || (e.composedPath && e.composedPath());
  el.setAttribute('style', "outline: solid 4px !important");
}

function removeAttribute(e) {
  const el = e.path || (e.composedPath && e.composedPath());
  el.removeAttribute('style', "outline: solid 4px !important");
}

function addEventListenerToShadow(root) {
  const tw = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
  
  do {
    const currentNode = tw.currentNode;
    if (currentNode instanceof ShadowRoot) continue;
    if (currentNode.shadowRoot) {
      currentNode.shadowRoot.addEventListener('mouseover', addAttribute)
      currentNode.shadowRoot.addEventListener('mouseout', removeAttribute)
      addEventListenerToShadow(currentNode.shadowRoot);
    }
  } while (tw.nextNode());
}

window.onload = addEventListenerToShadow(document.body);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

隐诗 2025-01-23 06:37:18

(嵌套)shadowDOM 是进入教室的 (DOM) 子级。

只有当你知道会有多少人进来时,你才能知道最后一个人是什么时候进来的。

这意味着每个(影子)DOM/元素应该通知它来到主应用程序。

然后应用程序处理/倒计时每个“到达的”DOM Child/shadowDOM

connectedCallback 在自定义元素的opening 标签上触发

,以便是向应用程序发送 CustomEvent I_WILL_COME 的适当时间。

但是......在您的用例中,您无法控制组件的功能。
因此,唯一的逃避方法就是等待 N 秒并考虑您的页面已加载。

(nested) shadowDOM are (DOM) children entering your class-room.

Only when you know how many there will come you know when the last one entered.

That means each (shadow) DOM/Element should notify it will come to the main application.

And the application then processes/counts down each "arrived" DOM Child/shadowDOM

The connectedCallback fires on the opening TAG of your Custom Element

So that is the appropriate time to send a CustomEvent I_WILL_COME to the application.

BUT... in your use-case you do not have control over what components do.
So the only escape is just wait N seconds and consider your page loaded.

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