Last active
September 15, 2018 12:33
-
-
Save josephrocca/4f17e3fd26d9323eda54c09ba1f85ebe to your computer and use it in GitHub Desktop.
scrap - simple web component test - async method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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