简体   繁体   English

从 Shadow DOM 中获取元素或使用主站点的 styles

[英]Get Element out of Shadow DOM or use styles of main site

I'm working in a project which uses Polymer (still on 1.7, we'll relaunch using a different technology instead of upgrading...) and I'm supposed to check out the possibilities to make it work together with DynamicYield, which is a personalization platform that injects HTML of personalized items or recommendations into one's page via ShadowDOM .我正在一个使用 Polymer 的项目中工作(仍在 1.7 上,我们将使用不同的技术重新启动而不是升级......)我应该检查使其与 DynamicYield 一起工作的可能性,即一个个性化平台, 通过ShadowDOM 将个性化项目或推荐的 HTML 注入到一个人的页面中。

The problem with shadowDOM is that, of course, your site's styles are missing, meaning you'd have to inject all your styles and thus maintain them in DynamicYield. shadowDOM 的问题在于,当然,您站点的 styles 丢失了,这意味着您必须注入所有 styles 并因此将它们维护在 DynamicYield 中。

So my idea is to take the injected content and get it out of the shadow DOM.所以我的想法是把注入的内容从shadow DOM中取出来。 I tried to type something like我试着输入类似的东西

document.getElementById('rec').innerHTML = document.getElementById('rec').shadowRoot.innerHTML; 
document.getElementById('rec').shadowRoot.innerHTML = '';

... into the JS console, but, although the DOM looks good now, the content disappered from the visible site. ...进入 JS 控制台,但是,虽然 DOM 现在看起来不错,但内容从可见站点中消失了。 (Maybe that's a Polymer issue?) (也许这是 Polymer 问题?)

Do you have any ideas what I could do to use the main sites styles inside the injected code?您有什么想法可以在注入的代码中使用主要站点 styles 吗? The DynamicYield docs unfortunately don't mention any setting to prevent it from using ShadowDOM (apart from unsetting the SPA_FLOW flag which is not feasible in our case).不幸的是,DynamicYield 文档没有提到任何阻止它使用 ShadowDOM 的设置(除了取消设置在我们的案例中不可行的 SPA_FLOW 标志)。

EDIT: Adding a small snippet to show the problem.编辑:添加一个小片段来显示问题。 Use inspect to see the innerHTML is in div#rec but not visible.使用检查查看 innerHTML 在 div#rec 但不可见。

 //Simulating DynamicYield adding code. Second headline/Shadow DOM will NOT be green. const el = document.getElementById('rec'); const shadowRoot = el.attachShadow({mode: 'open'}); shadowRoot.innerHTML = '<h1>I belong to Shadow DOM</h1>'; const container = document.querySelector('body'); container.appendChild(el); //Trying to get Code out of Shadow DOM el.innerHTML = el.shadowRoot.innerHTML; el.shadowRoot.innerHTML = '';
 h1 { color: green; }
 <h1>I belong to Light DOM</h1> <div id="rec"></div>

Ok, I think I found a workaround.好的,我想我找到了解决方法。 It seems nodes that still have a shadow DOM attached do not take normal innerHTML or at least can't make innerHTML visible.似乎仍然附加了影子 DOM 的节点不采用正常的 innerHTML 或至少不能使 innerHTML 可见。

Modified Snippet修改片段

 //Simulating DynamicYield adding code. Second headline/Shadow DOM will NOT be green. const el = document.getElementById('rec'); const shadowRoot = el.attachShadow({mode: 'open'}); shadowRoot.innerHTML = '<h1>I belong to Shadow DOM</h1>'; const container = document.querySelector('body'); container.appendChild(el); //Trying to get Code out of Shadow DOM //WORKAROUND: Put HTML in another div that never had a shadow DOM attached. const el2 = document.getElementById('rec2'); el2.innerHTML = el.shadowRoot.innerHTML; el.shadowRoot.innerHTML = '';
 h1 { color: green; }
 <h1>I belong to Light DOM</h1> <div id="rec"></div> <div id="rec2"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM