Skip to content

Instantly share code, notes, and snippets.

@andreban
Created November 8, 2017 13:28
Show Gist options
  • Save andreban/ad5e41723e0532598b5add7145d974ba to your computer and use it in GitHub Desktop.
Save andreban/ad5e41723e0532598b5add7145d974ba to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script async src="https://cdn.ampproject.org/shadow-v0.js"></script>
<style>
#amp-container {
width: 100vw;
height: 80vh;
}
</style>
</head>
<body>
<!-- The Container to be used by amp-shadow -->
<div id="amp-container"></div>
<!-- The AMP doc is inlined in this template -->
<template id="amp-document">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="canonical" href="https://ampbyexample.com/introduction/hello_world/">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<style amp-custom>
h1 {
color: red;
}
</style>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
</head>
<body>
<h1>Hello World!</h1>
<amp-img src="/img/amp.jpg" width="1080" height="610" layout="responsive"></amp-img>
</body>
</html>
</template>
<script type="text/javascript">
const ampContainer = document.querySelector('#amp-container');
const ampContent = document.querySelector('#amp-document');
const ampReadyPromise = new Promise(resolve => {
(window.AMP = window.AMP || []).push(resolve);
});
ampReadyPromise.then(() => {
// The template gives a document-fragment. We need an actual document
// to load inside amp-shado.
const doc = document.implementation.createHTMLDocument();
const docBody = doc.querySelector('body');
const docHead = doc.querySelector('head');
// The template is transformed when loaded. Reorganise and add
// elements to correct place.
while(ampContent.content.children.length) {
element = ampContent.content.children[0];
// Link elements are ignored in shadow mode.
if (element.tagName === 'LINK') {
element.remove();
continue;
}
// Add scripts, noscripts and styles to the head.
if (element.tagName === 'SCRIPT' || element.tagName === 'NOSCRIPT'
|| element.tagName === 'STYLE') {
docHead.appendChild(element);
continue;
}
// Everything else goes into the body.
docBody.appendChild(element);
}
window.AMP.attachShadowDoc(ampContainer, doc, '');
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment