我试着拍摄AMP故事的截图,没有声音和分享按钮。
当我发现有一种叫做影子DOM的东西后,我想知道如何设置显示:没有:
addStyleTag({content: '.i-amphtml-story-system-layer-buttons { display : none!important }'})我想我是来像这样访问影子DOM的。
const shadowRootInnerHTML = await page.evaluate(() => {
return document.querySelector("body > amp-story > div").shadowRoot.innerHTML
});这是我目前使用的,
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
});发布于 2019-05-27 00:06:18
放大器页面内有多个div元素。
我们可以执行这个普通的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 }`
}
});基本上,
在示例放大器页面上的结果:

https://stackoverflow.com/questions/56306165
复制相似问题