Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tmp.html 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <!-- https://www.web3isgoinggreat.com/single/2024-02-28-0 -->
  2. <!-- https://umap.openstreetmap.fr/fr/map/grand-tour-de-la-foret-de-ouareau_1037457 -->
  3. <oembed-lazy url="https://umap.openstreetmap.fr/fr/map/grand-tour-de-la-foret-de-ouareau_1037457">
  4. Une carte de la Grande Boucle de la forêt de Ouareau
  5. </oembed-lazy>
  6. <script type="module">
  7. class OembedLazy extends HTMLElement {
  8. static tagName = 'oembed-lazy'
  9. static register(tagName, registry) {
  10. if(!registry && ('customElements' in globalThis)) {
  11. registry = globalThis.customElements
  12. }
  13. registry?.define(tagName || this.tagName, this)
  14. }
  15. get url() {
  16. return this.getAttribute('url') || ''
  17. }
  18. constructor() {
  19. super()
  20. this.attachShadow({ mode: 'open' })
  21. }
  22. async connectedCallback() {
  23. let slot = document.createElement('slot')
  24. this.shadowRoot.appendChild(slot)
  25. const html = await this.fetchText(this.url)
  26. const oEmbedLink = this.extractOembedLink(html)
  27. console.log(oEmbedLink)
  28. const corsLink = `http://cors-anywhere.herokuapp.com/${
  29. oEmbedLink.replaceAll('%3A', ':').replaceAll('%2F', '/')
  30. }`
  31. console.log(corsLink)
  32. const oEmbedJson = await this.fetchJson(corsLink)
  33. console.log(oEmbedJson)
  34. this.innerHTML = oEmbedJson.html
  35. }
  36. fetchText(url) {
  37. return fetch(url)
  38. .then((data) => data.text())
  39. .catch(console.error.bind(this))
  40. }
  41. fetchJson(url) {
  42. return fetch(url, { type: 'json' })
  43. .then((data) => data.json())
  44. .catch(console.error.bind(this))
  45. }
  46. extractOembedLink(html) {
  47. const parser = new DOMParser()
  48. const htmlDocument = parser.parseFromString(html, "text/html")
  49. const oEmbedMeta = htmlDocument.documentElement.querySelector(
  50. 'link[type="application/json+oembed"]'
  51. )
  52. return oEmbedMeta.href
  53. }
  54. }
  55. OembedLazy.register()
  56. </script>