Skip to content

Instantly share code, notes, and snippets.

@josephrocca
Last active September 15, 2018 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephrocca/4f17e3fd26d9323eda54c09ba1f85ebe to your computer and use it in GitHub Desktop.
Save josephrocca/4f17e3fd26d9323eda54c09ba1f85ebe to your computer and use it in GitHub Desktop.
scrap - simple web component test - async method
<script>
(function(window, document, undefined) {
const thisDoc = document.currentScript.ownerDocument;
class TestComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
this.shadowRoot.innerHTML = "<h1>Simple Test WebComponent</h1>";
}
async getImageWidthByUrl(url, method) {
let imgEl;
if(method === "new Image()") {
imgEl = new Image();
} else if(method === "document.createElement") {
imgEl = document.createElement("img");
} else if(method === "document.currentScript.ownerDocument.createElement") {
imgEl = thisDoc.createElement("img");
}
let imgLoad = new Promise((resolve, reject) => {
imgEl.onload = resolve;
imgEl.onerror = reject;
});
imgEl.src = url;
await imgLoad;
return imgEl.naturalWidth;
}
}
window.customElements.define('test-component', TestComponent);
})(window, document);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment