首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用木偶词典操纵/设置样式shadowRoot

使用木偶词典操纵/设置样式shadowRoot
EN

Stack Overflow用户
提问于 2019-05-25 15:22:46
回答 1查看 3.2K关注 0票数 0

我试着拍摄AMP故事的截图,没有声音和分享按钮。

当我发现有一种叫做影子DOM的东西后,我想知道如何设置显示:没有:

代码语言:javascript
复制
addStyleTag({content: '.i-amphtml-story-system-layer-buttons { display : none!important }'})

我想我是来像这样访问影子DOM的。

代码语言:javascript
复制
const shadowRootInnerHTML = await page.evaluate(() => {
    return document.querySelector("body > amp-story > div").shadowRoot.innerHTML
});

这是我目前使用的,

代码语言:javascript
复制
const browser = await puppeteer.launch({
  slowMo: 250,
  args: [
    '--disable-infobars',
  ]
});
const page = await browser.newPage()

await page.emulate({
  name: 'iPhone1080x1920',
  userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
  viewport: {
    width: 360,
    height: 640,
    deviceScaleFactor: 3,
    isLandscape: false
  }
});

await page.goto(urlToTest, {
  waitUntil: 'networkidle2',
  timeout: 0
});

const textContent = await page.evaluate(() => {
  return document.querySelector("body > amp-story > div").shadowRoot.innerHTML
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-27 00:06:18

放大器页面内有多个div元素。

我们可以执行这个普通的JavaScript,这将隐藏顶部幻灯片和分享按钮的安培页面。

我在这段代码中添加了两种方法,您可以应用其中一种。

代码语言:javascript
复制
// there are multiple div elements inside amp-story
const elements = [...document.querySelectorAll("body > amp-story > div")];

elements.map(rootElement => {
  // find the shadowRoot inside if it exists
  const shadowRoot = rootElement.shadowRoot;

  if (shadowRoot) {
    /**
     * WAY 1: Find the element and apply css to it directly
     */
    // this holds the top share button and pagination slides
    const element = shadowRoot.querySelector(
      ".i-amphtml-story-system-layer"
    );

    // forcefully hide the element
    if (element) element.style.setProperty("display", "none", "important");

    /**
     * WAY 2: Append your own style to the <style> tag inside the amp shadowRoot
     */
    shadowRoot.querySelector('style').innerHTML += `.i-amphtml-story-system-layer { display : none!important }`
  }
});

基本上,

  • 找到div
  • 获取shadowRoot
  • 要么找到元素并直接应用,要么向标记添加样式。

在示例放大器页面上的结果:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56306165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档