embetter 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -156,8 +156,8 @@ class p {
156
156
  static buildFromText(t, i) {
157
157
  const e = t.match(this.regex)[1];
158
158
  if (e != null) {
159
- const s = this.link(e), a = this.thumbnail(e);
160
- i(e, s, a);
159
+ const a = this.link(e), s = this.thumbnail(e);
160
+ i(e, a, s);
161
161
  }
162
162
  }
163
163
  }
@@ -178,16 +178,16 @@ class b {
178
178
  }
179
179
  static getData(t) {
180
180
  return new Promise((i, e) => {
181
- const s = `https://vimeo.com/api/v2/video/${t}.json`;
182
- fetch(s).then((a) => a.json()).then((a) => i(a[0].thumbnail_large)).catch(() => i(""));
181
+ const a = `https://vimeo.com/api/v2/video/${t}.json`;
182
+ fetch(a).then((s) => s.json()).then((s) => i(s[0].thumbnail_large)).catch(() => i(""));
183
183
  });
184
184
  }
185
185
  static buildFromText(t, i) {
186
186
  const e = t.match(this.regex);
187
187
  if (e && e[1]) {
188
- const s = e[1], a = this.link(s);
189
- this.getData(s).then((r) => {
190
- i(s, a, r);
188
+ const a = e[1], s = this.link(a);
189
+ this.getData(a).then((r) => {
190
+ i(a, s, r);
191
191
  });
192
192
  }
193
193
  }
@@ -213,9 +213,9 @@ class A {
213
213
  static buildFromText(t, i) {
214
214
  const e = t.match(this.regex);
215
215
  if (e && e[1]) {
216
- const s = e[1], a = this.link(s);
217
- this.getData(a).then((r) => {
218
- i(s, a, r);
216
+ const a = e[1], s = this.link(a);
217
+ this.getData(s).then((r) => {
218
+ i(a, s, r);
219
219
  });
220
220
  }
221
221
  }
@@ -238,8 +238,8 @@ class g {
238
238
  t = t.split("_")[0];
239
239
  const e = t.match(this.regex);
240
240
  if (e && e[1]) {
241
- const s = e[1], a = this.link(s), r = this.thumbnail(s);
242
- i(s, a, r);
241
+ const a = e[1], s = this.link(a), r = this.thumbnail(a);
242
+ i(a, s, r);
243
243
  }
244
244
  }
245
245
  }
@@ -264,9 +264,9 @@ class f {
264
264
  static buildFromText(t, i) {
265
265
  const e = t.match(this.regex);
266
266
  if (e && e[1]) {
267
- const s = e[1], a = this.link(s);
268
- this.getData(a).then((r) => {
269
- i(s, a, r);
267
+ const a = e[1], s = this.link(a);
268
+ this.getData(s).then((r) => {
269
+ i(a, s, r);
270
270
  });
271
271
  }
272
272
  }
@@ -276,8 +276,8 @@ class y {
276
276
  static dataAttribute = "codepen-id";
277
277
  static regex = /(?:https?:\/\/)?(?:w{3}\.)?(?:codepen\.io)\/([a-zA-Z0-9_\-%]*\/[a-zA-Z0-9_\-%]*\/[a-zA-Z0-9_\-%]*)/;
278
278
  static embed(t) {
279
- const i = t.id.replace("/pen/", "/embed/"), e = i.split("/")[0], s = i.split("/")[2];
280
- return `<iframe src="https://codepen.io/${i}?height=${t.h}&theme-id=0&slug-hash=${s}&default-tab=result&user=${e}" frameborder="0" scrolling="no" allowtransparency="true" allowfullscreen allow=autoplay></iframe>`;
279
+ const i = t.id.replace("/pen/", "/embed/"), e = i.split("/")[0], a = i.split("/")[2];
280
+ return `<iframe src="https://codepen.io/${i}?height=${t.h}&theme-id=0&slug-hash=${a}&default-tab=result&user=${e}" frameborder="0" scrolling="no" allowtransparency="true" allowfullscreen allow=autoplay></iframe>`;
281
281
  }
282
282
  static thumbnail(t) {
283
283
  return `https://codepen.io/${t}/image/large.png`;
@@ -288,9 +288,9 @@ class y {
288
288
  static buildFromText(t, i) {
289
289
  const e = t.match(this.regex);
290
290
  if (e && e[1]) {
291
- let s = e[1].replace("/embed/", "/pen/");
292
- const a = this.link(s), r = this.thumbnail(s);
293
- i(s, a, r);
291
+ let a = e[1].replace("/embed/", "/pen/");
292
+ const s = this.link(a), r = this.thumbnail(a);
293
+ i(a, s, r);
294
294
  }
295
295
  }
296
296
  }
@@ -313,9 +313,9 @@ class w {
313
313
  static buildFromText(t, i) {
314
314
  const e = t.match(this.regex);
315
315
  if (e && e[1]) {
316
- const a = `https://${e[1]}`;
317
- this.getData(a).then((r) => {
318
- r.id && i(r.id, a, r.thumbnail || "");
316
+ const s = `https://${e[1]}`;
317
+ this.getData(s).then((r) => {
318
+ r.id && i(r.id, s, r.thumbnail || "");
319
319
  });
320
320
  }
321
321
  }
@@ -334,9 +334,9 @@ class v {
334
334
  return `https://giphy.com/gifs/${t}`;
335
335
  }
336
336
  static buildFromText(t, i) {
337
- const e = t.split("/"), s = e[e.length - 1], a = s.split("-"), r = a[a.length - 1];
337
+ const e = t.split("/"), a = e[e.length - 1], s = a.split("-"), r = s[s.length - 1];
338
338
  if (r) {
339
- const o = this.link(s), n = this.thumbnail(r);
339
+ const o = this.link(a), n = this.thumbnail(r);
340
340
  i(r, o, n);
341
341
  }
342
342
  }
@@ -346,8 +346,8 @@ class k {
346
346
  static dataAttribute = "video-url";
347
347
  static regex = /(?:https?:\/\/)?(?:w{3}\.)?(.+\.(?:mp4|mov|m4v))(?:\/|$|\s|\?|#)/;
348
348
  static embed(t) {
349
- const i = t.autoplay === !0 ? ' autoplay="true"' : "", e = t.loops === !0 ? ' loop="true"' : "", s = t.muted === !0 ? " muted" : "";
350
- return `<video src="${t.id}" width="${t.w}" height="${t.h}"${i}${e}${s} controls playsinline webkitallowfullscreen mozallowfullscreen allowfullscreen></video>`;
349
+ const i = t.autoplay === !0 ? ' autoplay="true"' : "", e = t.loops === !0 ? ' loop="true"' : "", a = t.muted === !0 ? " muted" : "";
350
+ return `<video src="${t.id}" width="${t.w}" height="${t.h}"${i}${e}${a} controls playsinline webkitallowfullscreen mozallowfullscreen allowfullscreen></video>`;
351
351
  }
352
352
  static thumbnail(t) {
353
353
  return t.replace(".mp4", "-poster.jpg").replace(".mov", "-poster.jpg").replace(".m4v", "-poster.jpg");
@@ -358,8 +358,8 @@ class k {
358
358
  static buildFromText(t, i) {
359
359
  const e = t.match(this.regex);
360
360
  if (e && e[1]) {
361
- const s = e[1], a = this.thumbnail(s);
362
- i(s, s, a);
361
+ const a = e[1], s = this.thumbnail(a);
362
+ i(a, a, s);
363
363
  }
364
364
  }
365
365
  }
@@ -379,8 +379,8 @@ class $ {
379
379
  static buildFromText(t, i) {
380
380
  const e = t.match(this.regex);
381
381
  if (e && e[1]) {
382
- const s = e[1], a = this.thumbnail(s);
383
- i(s, s, a);
382
+ const a = e[1], s = this.thumbnail(a);
383
+ i(a, a, s);
384
384
  }
385
385
  }
386
386
  }
@@ -389,15 +389,15 @@ class c extends HTMLElement {
389
389
  defaultThumbnail = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAArwAAAGcAQMAAAABMOGrAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAADUExURQAAAKd6PdoAAAA6SURBVHja7cGBAAAAAMOg+VPf4ARVAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAN488AAGP4e1mAAAAAElFTkSuQmCC";
390
390
  static EMBETTER_ACTIVATED = "embetter-activated";
391
391
  connectedCallback() {
392
- this.shadow = this.attachShadow({ mode: "open" }), this.el = this.shadow ?? this, this.initComponent(), this.render(), this.checkThumbnail(), this.addListeners();
392
+ setTimeout(() => {
393
+ this.isConnected && (this.shadow || (this.initialHTML = this.innerHTML, this.innerHTML = "", this.shadow = this.attachShadow({ mode: "open" }), this.el = this.shadow ?? this, this.initComponent(), this.render(), this.getElements(), this.checkThumbnail()), this.addListeners());
394
+ }, 0);
393
395
  }
394
396
  disconnectedCallback() {
395
397
  this.unembedMedia(), this.removeAttribute("ready"), this.playButton && this.playButton.removeEventListener("click", this.clickListener), document.removeEventListener(c.EMBETTER_ACTIVATED, this.embedListener), this.observer && (this.observer.disconnect(), this.observer = null);
396
398
  }
397
399
  initComponent() {
398
- this.markup = "embetter-media component not initialized properly.", this.loops = this.hasAttribute("loops"), this.muted = this.hasAttribute("muted"), this.posterURL = null;
399
- const t = this.querySelector("img");
400
- t && t.src && (this.posterURL = t.src), this.innerHTML = "", this.findAndActivateService();
400
+ this.markup = "embetter-media component not initialized properly.", this.loops = this.hasAttribute("loops"), this.muted = this.hasAttribute("muted"), this.findAndActivateService();
401
401
  }
402
402
  getElements() {
403
403
  this.thumbnail = this.el.querySelector("img");
@@ -415,30 +415,25 @@ class c extends HTMLElement {
415
415
  for (let t of d) {
416
416
  let i = t.dataAttribute;
417
417
  if (this.hasAttribute(i)) {
418
- this.service = t, this.serviceType = t.type, this.serviceId = this.getAttribute(i);
419
- let e = this.posterURL || t.thumbnail(this.serviceId);
420
- if (this.markup = this.playerHTML(t.link(this.serviceId), e), t.getData) {
421
- const s = t.link(this.serviceId);
422
- t.getData(s).then((a) => {
423
- const r = typeof a == "string" ? a : a?.thumbnail;
424
- !e && !this.posterURL && r && this.thumbnail && (this.thumbnail.src = r);
425
- });
426
- }
418
+ this.service = t, this.serviceType = t.type, this.serviceId = this.getAttribute(i), this.markup = this.playerHTML(t.link(this.serviceId));
427
419
  break;
428
420
  }
429
421
  }
430
422
  }
423
+ onReady() {
424
+ this.removeAttribute("loading"), this.setAttribute("ready", "");
425
+ }
431
426
  checkThumbnail() {
432
- this.thumbnail && (this.setAttribute("loading", ""), this.thumbnail.onload = () => {
433
- this.removeAttribute("loading"), this.setAttribute("ready", "");
427
+ this.thumbnail && (this.setAttribute("loading", ""), this.thumbnail.complete ? this.onReady() : (this.thumbnail.onload = () => {
428
+ this.onReady();
434
429
  }, this.thumbnail.onerror = () => {
435
430
  if (this.thumbnail.src.includes("/maxresdefault.jpg")) {
436
431
  this.thumbnail.src = this.thumbnail.src.replace("/maxresdefault.jpg", "/0.jpg");
437
432
  return;
438
433
  }
439
- this.thumbnail.src = this.defaultThumbnail, this.removeAttribute("loading"), this.setAttribute("ready", "");
440
- }, setTimeout(() => {
441
- this.thumbnail.height < 50 && (this.thumbnail.src = this.defaultThumbnail), this.removeAttribute("loading"), this.setAttribute("ready", "");
434
+ this.thumbnail.src = this.defaultThumbnail, this.onReady();
435
+ }), setTimeout(() => {
436
+ this.thumbnail.height < 50 && (this.thumbnail.src = this.defaultThumbnail), this.onReady();
442
437
  }, 4e3));
443
438
  }
444
439
  setupMobileObserver() {
@@ -480,23 +475,21 @@ class c extends HTMLElement {
480
475
  var i = document.createElement("div");
481
476
  return i.innerHTML = t, i.firstChild;
482
477
  }
483
- static componentHTML(t, i, e = null, s = null) {
484
- let a = "";
485
- if (s || e) {
486
- const r = s || "#", o = e ? `<img src="${e}">` : "";
487
- a = `<a href="${r}">${o}</a>`;
478
+ static componentHTML(t, i, e = null, a = null) {
479
+ let s = "";
480
+ if (a || e) {
481
+ const r = a || "#", o = e ? `<img src="${e}">` : "";
482
+ s = `<a href="${r}">${o}</a>`;
488
483
  }
489
- return `<embetter-media ${t}="${i}">${a}</embetter-media>`;
484
+ return `<embetter-media ${t}="${i}">${s}</embetter-media>`;
490
485
  }
491
- playerHTML(t, i) {
486
+ playerHTML(t) {
492
487
  return (
493
488
  /* html */
494
489
  `
495
- <a href="${t}">
496
- <img src="${i}" />
497
- <div class="embetter-loading"></div>
498
- <div class="embetter-play-button"></div>
499
- </a>
490
+ ${this.initialHTML}
491
+ <div class="embetter-loading"></div>
492
+ <div class="embetter-play-button"></div>
500
493
  `
501
494
  );
502
495
  }
@@ -510,18 +503,18 @@ class c extends HTMLElement {
510
503
  this.el.innerHTML = `
511
504
  ${this.html()}
512
505
  <style>${this.css()}</style>
513
- `, this.getElements();
506
+ `;
514
507
  }
515
508
  static register() {
516
509
  customElements.define("embetter-media", c);
517
510
  }
518
511
  static upgradeLegacyEmbeds(t = document) {
519
512
  t.querySelectorAll(".embetter").forEach((e) => {
520
- for (const s of d) {
521
- const a = `data-${s.dataAttribute}`;
522
- if (e.hasAttribute(a)) {
523
- const r = e.getAttribute(a), o = document.createElement("embetter-media");
524
- o.setAttribute(s.dataAttribute, r);
513
+ for (const a of d) {
514
+ const s = `data-${a.dataAttribute}`;
515
+ if (e.hasAttribute(s)) {
516
+ const r = e.getAttribute(s), o = document.createElement("embetter-media");
517
+ o.setAttribute(a.dataAttribute, r);
525
518
  const n = e.querySelector("a");
526
519
  n && o.appendChild(n.cloneNode(!0)), e.hasAttribute("data-loops") && o.setAttribute("loops", ""), e.hasAttribute("data-muted") && o.setAttribute("muted", ""), e.replaceWith(o);
527
520
  break;
@@ -531,20 +524,20 @@ class c extends HTMLElement {
531
524
  }
532
525
  static componentMarkupFromURL(t, i) {
533
526
  for (let e = 0; e < d.length; e++) {
534
- const s = d[e];
535
- if (t.match(s.regex) != null) {
536
- s.buildFromText(t, (a, r, o) => {
527
+ const a = d[e];
528
+ if (t.match(a.regex) != null) {
529
+ a.buildFromText(t, (s, r, o) => {
537
530
  const n = (h) => {
538
531
  let u = c.componentHTML(
539
- s.dataAttribute,
540
- a,
532
+ a.dataAttribute,
533
+ s,
541
534
  h,
542
535
  r
543
536
  );
544
- i(u, s);
537
+ i(u, a);
545
538
  };
546
- if (!o && s.getData && r) {
547
- s.getData(r).then((h) => {
539
+ if (!o && a.getData && r) {
540
+ a.getData(r).then((h) => {
548
541
  const u = typeof h == "string" ? h : h?.thumbnail;
549
542
  n(u || o);
550
543
  }).catch(() => n(o));
@@ -135,13 +135,11 @@
135
135
  to { transform: rotate(360deg); }
136
136
  }
137
137
 
138
- `;class d{static type="youtube";static dataAttribute="youtube-id";static regex=/(?:.+?)?(?:youtube\.com\/v\/|watch\/|\?v=|\&v=|youtu\.be\/|\/v=|^youtu\.be\/)([a-zA-Z0-9_-]{11})+/;static embed(t){const i=t.autoplay===!0?"&autoplay=1":"",e=t.loops===!0?`&loop=1&playlist=${t.id}`:"";return`<iframe class="video" enablejsapi="1" width="${t.w}" height="${t.h}" src="https://www.youtube.com/embed/${t.id}?rel=0&suggestedQuality=hd720&enablejsapi=1${i}${e}" frameborder="0" scrolling="no" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return"https://img.youtube.com/vi/"+t+"/maxresdefault.jpg"}static link(t){return"https://www.youtube.com/watch?v="+t}static buildFromText(t,i){const e=t.match(this.regex)[1];if(e!=null){const s=this.link(e),a=this.thumbnail(e);i(e,s,a)}}}class b{static type="vimeo";static dataAttribute="vimeo-id";static regex=/(?:vimeo\.com\/(?:video\/)?|player\.vimeo\.com\/video\/)(\d+)/i;static embed(t){const i=t.autoplay===!0?"&autoplay=1":"",e=t.loops===!0?"&loop=1":"";return`<iframe id="${t.id}" src="https://player.vimeo.com/video/${t.id}?title=0&byline=0&portrait=0&color=ffffff&api=1&player_id=${t.id}${i}${e}" width="${t.w}" height="${t.h}" frameborder="0" scrolling="no" webkitallowfullscreen mozallowfullscreen allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return""}static link(t){return"https://vimeo.com/"+t}static getData(t){return new Promise((i,e)=>{const s=`https://vimeo.com/api/v2/video/${t}.json`;fetch(s).then(a=>a.json()).then(a=>i(a[0].thumbnail_large)).catch(()=>i(""))})}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const s=e[1],a=this.link(s);this.getData(s).then(r=>{i(s,a,r)})}}}class A{static type="soundcloud";static dataAttribute="soundcloud-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(?:soundcloud\.com|snd\.sc)\/([a-zA-Z0-9_-]*(?:\/sets)?(?:\/groups)?\/[a-zA-Z0-9_-]*)/;static embed(t){const i=t.autoplay===!0?"&auto_play=true":"";return`<iframe width="100%" height="600" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=${encodeURIComponent(`https://soundcloud.com/${t.id}`)}${i}&hide_related=false&color=373737&show_comments=false&show_user=true&show_reposts=false&visual=true" allow="autoplay"></iframe>`}static thumbnail(t){return""}static link(t){return`https://soundcloud.com/${t}`}static getData(t){const i=`/api/soundcloud?url=${encodeURIComponent(t)}&format=json`;return fetch(i).then(e=>e.json()).then(e=>e.thumbnail_url||"").catch(()=>"")}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const s=e[1],a=this.link(s);this.getData(a).then(r=>{i(s,a,r)})}}}class f{static type="dailymotion";static dataAttribute="dailymotion-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?dailymotion\.com\/video\/([a-zA-Z0-9-_]*)/;static embed(t){const i=t.autoplay===!0?"?autoPlay=1":"";return`<iframe class="video" width="${t.w}" height="${t.h}" src="https://www.dailymotion.com/embed/video/${t.id}${i}" frameborder="0" scrolling="no" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return`https://www.dailymotion.com/thumbnail/video/${t}`}static link(t){return`https://www.dailymotion.com/video/${t}`}static buildFromText(t,i){t=t.split("_")[0];const e=t.match(this.regex);if(e&&e[1]){const s=e[1],a=this.link(s),r=this.thumbnail(s);i(s,a,r)}}}class g{static type="mixcloud";static dataAttribute="mixcloud-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(?:mixcloud\.com)\/(.*\/.*)/;static embed(t){const i=t.autoplay===!0?"&autoplay=true":"";return`<iframe width="660" height="180" src="https://www.mixcloud.com/widget/iframe/?feed=${encodeURIComponent("https://www.mixcloud.com/"+t.id)}&replace=0&hide_cover=1&stylecolor=ffffff&embed_type=widget_standard${i}" frameborder="0" scrolling="no"></iframe>`}static thumbnail(t){return""}static link(t){return`https://www.mixcloud.com/${t}`}static getData(t){const i=`/api/mixcloud/?url=${encodeURIComponent(t)}&format=json`;return fetch(i).then(e=>e.json()).then(e=>e.image||"").catch(()=>"")}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const s=e[1],a=this.link(s);this.getData(a).then(r=>{i(s,a,r)})}}}class y{static type="codepen";static dataAttribute="codepen-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(?:codepen\.io)\/([a-zA-Z0-9_\-%]*\/[a-zA-Z0-9_\-%]*\/[a-zA-Z0-9_\-%]*)/;static embed(t){const i=t.id.replace("/pen/","/embed/"),e=i.split("/")[0],s=i.split("/")[2];return`<iframe src="https://codepen.io/${i}?height=${t.h}&theme-id=0&slug-hash=${s}&default-tab=result&user=${e}" frameborder="0" scrolling="no" allowtransparency="true" allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return`https://codepen.io/${t}/image/large.png`}static link(t){return`https://codepen.io/${t.replace("/embed/","/pen/")}`}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){let s=e[1].replace("/embed/","/pen/");const a=this.link(s),r=this.thumbnail(s);i(s,a,r)}}}class w{static type="bandcamp";static dataAttribute="bandcamp-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?([a-zA-Z0-9_\-]*\.bandcamp\.com\/(?:album|track)\/[a-zA-Z0-9_\-%]*)/;static embed(t){return`<iframe src="https://bandcamp.com/EmbeddedPlayer/${t.id}/size=large/bgcol=ffffff/linkcol=333333/tracklist=true/artwork=small/transparent=true/" frameborder="0" scrolling="no" allowtransparency="true" allowfullscreen seamless></iframe>`}static thumbnail(t){return""}static link(t){return t.match(/^(album|track)=/)?"":`https://${t}`}static getData(t){return fetch(`/api/bandcamp?url=${encodeURIComponent(t)}`).then(i=>i.json()).catch(()=>({id:null,thumbnail:null}))}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const a=`https://${e[1]}`;this.getData(a).then(r=>{r.id&&i(r.id,a,r.thumbnail||"")})}}}class v{static type="giphy";static dataAttribute="giphy-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?giphy\.com\/gifs\/([a-zA-Z0-9_\-%]*)/;static embed(t){return`<iframe width="${t.w}" height="${t.h}" src="https://giphy.com/embed/${t.id}/twitter/iframe" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return`https://media.giphy.com/media/${t}/giphy_s.gif`}static link(t){return`https://giphy.com/gifs/${t}`}static buildFromText(t,i){const e=t.split("/"),s=e[e.length-1],a=s.split("-"),r=a[a.length-1];if(r){const o=this.link(s),c=this.thumbnail(r);i(r,o,c)}}}class k{static type="video";static dataAttribute="video-url";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(.+\.(?:mp4|mov|m4v))(?:\/|$|\s|\?|#)/;static embed(t){const i=t.autoplay===!0?' autoplay="true"':"",e=t.loops===!0?' loop="true"':"",s=t.muted===!0?" muted":"";return`<video src="${t.id}" width="${t.w}" height="${t.h}"${i}${e}${s} controls playsinline webkitallowfullscreen mozallowfullscreen allowfullscreen></video>`}static thumbnail(t){return t.replace(".mp4","-poster.jpg").replace(".mov","-poster.jpg").replace(".m4v","-poster.jpg")}static link(t){return t}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const s=e[1],a=this.thumbnail(s);i(s,s,a)}}}class ${static type="gif";static dataAttribute="gif-url";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(.+\.gif)(?:\/|$|\s|\?|#)/;static embed(t){return`<img class="gif" src="${t.id}" width="${t.w}" height="${t.h}">`}static thumbnail(t){return t.replace(".gif","-poster.jpg")}static link(t){return t}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const s=e[1],a=this.thumbnail(s);i(s,s,a)}}}const m=[k,$,d,b,A,f,g,y,w,v];class l extends HTMLElement{defaultThumbnail="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAArwAAAGcAQMAAAABMOGrAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAADUExURQAAAKd6PdoAAAA6SURBVHja7cGBAAAAAMOg+VPf4ARVAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAN488AAGP4e1mAAAAAElFTkSuQmCC";static EMBETTER_ACTIVATED="embetter-activated";connectedCallback(){this.shadow=this.attachShadow({mode:"open"}),this.el=this.shadow??this,this.initComponent(),this.render(),this.checkThumbnail(),this.addListeners()}disconnectedCallback(){this.unembedMedia(),this.removeAttribute("ready"),this.playButton&&this.playButton.removeEventListener("click",this.clickListener),document.removeEventListener(l.EMBETTER_ACTIVATED,this.embedListener),this.observer&&(this.observer.disconnect(),this.observer=null)}initComponent(){this.markup="embetter-media component not initialized properly.",this.loops=this.hasAttribute("loops"),this.muted=this.hasAttribute("muted"),this.posterURL=null;const t=this.querySelector("img");t&&t.src&&(this.posterURL=t.src),this.innerHTML="",this.findAndActivateService()}getElements(){this.thumbnail=this.el.querySelector("img")}addListeners(){this.clickListener=this.onClick.bind(this),this.playButton=this.el.querySelector(".embetter-play-button"),this.playButton&&this.playButton.addEventListener("click",this.clickListener),this.embedListener=this.onEmbedActivated.bind(this),document.addEventListener(l.EMBETTER_ACTIVATED,this.embedListener)}onClick(t){t.preventDefault(),this.embedMedia()}onEmbedActivated(t){t.detail!==this&&this.unembedMedia()}findAndActivateService(){for(let t of m){let i=t.dataAttribute;if(this.hasAttribute(i)){this.service=t,this.serviceType=t.type,this.serviceId=this.getAttribute(i);let e=this.posterURL||t.thumbnail(this.serviceId);if(this.markup=this.playerHTML(t.link(this.serviceId),e),t.getData){const s=t.link(this.serviceId);t.getData(s).then(a=>{const r=typeof a=="string"?a:a?.thumbnail;!e&&!this.posterURL&&r&&this.thumbnail&&(this.thumbnail.src=r)})}break}}}checkThumbnail(){this.thumbnail&&(this.setAttribute("loading",""),this.thumbnail.onload=()=>{this.removeAttribute("loading"),this.setAttribute("ready","")},this.thumbnail.onerror=()=>{if(this.thumbnail.src.includes("/maxresdefault.jpg")){this.thumbnail.src=this.thumbnail.src.replace("/maxresdefault.jpg","/0.jpg");return}this.thumbnail.src=this.defaultThumbnail,this.removeAttribute("loading"),this.setAttribute("ready","")},setTimeout(()=>{this.thumbnail.height<50&&(this.thumbnail.src=this.defaultThumbnail),this.removeAttribute("loading"),this.setAttribute("ready","")},4e3))}setupMobileObserver(){this.isMobile()&&(this.observer=new IntersectionObserver(t=>{t.forEach(i=>{i.isIntersecting?this.embedMedia(!1):this.unembedMedia()})},{threshold:.3}),this.observer.observe(this))}embedMedia(t){if(document.dispatchEvent(new CustomEvent(l.EMBETTER_ACTIVATED,{bubbles:!0,composed:!0,detail:this})),this.hasAttribute("playing"))return;t===void 0&&(t=this.hasAttribute("autoplay")?this.getAttribute("autoplay")!=="false":!0);let i=this.service.embed({id:this.serviceId,w:this.thumbnail&&this.thumbnail.width||"100%",h:this.thumbnail&&this.thumbnail.height||"100%",autoplay:t,loops:this.loops,muted:this.muted||t});this.playerEl=l.stringToDomElement(i),this.el.appendChild(this.playerEl),this.setAttribute("playing","")}unembedMedia(){this.playerEl!=null&&this.playerEl.parentNode!=null&&this.playerEl.parentNode.removeChild(this.playerEl),this.removeAttribute("playing")}isMobile(){return/iphone|ipad|ipod|android/i.test(navigator.userAgent)}static stringToDomElement(t){var i=document.createElement("div");return i.innerHTML=t,i.firstChild}static componentHTML(t,i,e=null,s=null){let a="";if(s||e){const r=s||"#",o=e?`<img src="${e}">`:"";a=`<a href="${r}">${o}</a>`}return`<embetter-media ${t}="${i}">${a}</embetter-media>`}playerHTML(t,i){return`
139
- <a href="${t}">
140
- <img src="${i}" />
141
- <div class="embetter-loading"></div>
142
- <div class="embetter-play-button"></div>
143
- </a>
138
+ `;class d{static type="youtube";static dataAttribute="youtube-id";static regex=/(?:.+?)?(?:youtube\.com\/v\/|watch\/|\?v=|\&v=|youtu\.be\/|\/v=|^youtu\.be\/)([a-zA-Z0-9_-]{11})+/;static embed(t){const i=t.autoplay===!0?"&autoplay=1":"",e=t.loops===!0?`&loop=1&playlist=${t.id}`:"";return`<iframe class="video" enablejsapi="1" width="${t.w}" height="${t.h}" src="https://www.youtube.com/embed/${t.id}?rel=0&suggestedQuality=hd720&enablejsapi=1${i}${e}" frameborder="0" scrolling="no" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return"https://img.youtube.com/vi/"+t+"/maxresdefault.jpg"}static link(t){return"https://www.youtube.com/watch?v="+t}static buildFromText(t,i){const e=t.match(this.regex)[1];if(e!=null){const a=this.link(e),s=this.thumbnail(e);i(e,a,s)}}}class b{static type="vimeo";static dataAttribute="vimeo-id";static regex=/(?:vimeo\.com\/(?:video\/)?|player\.vimeo\.com\/video\/)(\d+)/i;static embed(t){const i=t.autoplay===!0?"&autoplay=1":"",e=t.loops===!0?"&loop=1":"";return`<iframe id="${t.id}" src="https://player.vimeo.com/video/${t.id}?title=0&byline=0&portrait=0&color=ffffff&api=1&player_id=${t.id}${i}${e}" width="${t.w}" height="${t.h}" frameborder="0" scrolling="no" webkitallowfullscreen mozallowfullscreen allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return""}static link(t){return"https://vimeo.com/"+t}static getData(t){return new Promise((i,e)=>{const a=`https://vimeo.com/api/v2/video/${t}.json`;fetch(a).then(s=>s.json()).then(s=>i(s[0].thumbnail_large)).catch(()=>i(""))})}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const a=e[1],s=this.link(a);this.getData(a).then(r=>{i(a,s,r)})}}}class A{static type="soundcloud";static dataAttribute="soundcloud-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(?:soundcloud\.com|snd\.sc)\/([a-zA-Z0-9_-]*(?:\/sets)?(?:\/groups)?\/[a-zA-Z0-9_-]*)/;static embed(t){const i=t.autoplay===!0?"&auto_play=true":"";return`<iframe width="100%" height="600" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=${encodeURIComponent(`https://soundcloud.com/${t.id}`)}${i}&hide_related=false&color=373737&show_comments=false&show_user=true&show_reposts=false&visual=true" allow="autoplay"></iframe>`}static thumbnail(t){return""}static link(t){return`https://soundcloud.com/${t}`}static getData(t){const i=`/api/soundcloud?url=${encodeURIComponent(t)}&format=json`;return fetch(i).then(e=>e.json()).then(e=>e.thumbnail_url||"").catch(()=>"")}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const a=e[1],s=this.link(a);this.getData(s).then(r=>{i(a,s,r)})}}}class f{static type="dailymotion";static dataAttribute="dailymotion-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?dailymotion\.com\/video\/([a-zA-Z0-9-_]*)/;static embed(t){const i=t.autoplay===!0?"?autoPlay=1":"";return`<iframe class="video" width="${t.w}" height="${t.h}" src="https://www.dailymotion.com/embed/video/${t.id}${i}" frameborder="0" scrolling="no" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return`https://www.dailymotion.com/thumbnail/video/${t}`}static link(t){return`https://www.dailymotion.com/video/${t}`}static buildFromText(t,i){t=t.split("_")[0];const e=t.match(this.regex);if(e&&e[1]){const a=e[1],s=this.link(a),r=this.thumbnail(a);i(a,s,r)}}}class g{static type="mixcloud";static dataAttribute="mixcloud-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(?:mixcloud\.com)\/(.*\/.*)/;static embed(t){const i=t.autoplay===!0?"&autoplay=true":"";return`<iframe width="660" height="180" src="https://www.mixcloud.com/widget/iframe/?feed=${encodeURIComponent("https://www.mixcloud.com/"+t.id)}&replace=0&hide_cover=1&stylecolor=ffffff&embed_type=widget_standard${i}" frameborder="0" scrolling="no"></iframe>`}static thumbnail(t){return""}static link(t){return`https://www.mixcloud.com/${t}`}static getData(t){const i=`/api/mixcloud/?url=${encodeURIComponent(t)}&format=json`;return fetch(i).then(e=>e.json()).then(e=>e.image||"").catch(()=>"")}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const a=e[1],s=this.link(a);this.getData(s).then(r=>{i(a,s,r)})}}}class y{static type="codepen";static dataAttribute="codepen-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(?:codepen\.io)\/([a-zA-Z0-9_\-%]*\/[a-zA-Z0-9_\-%]*\/[a-zA-Z0-9_\-%]*)/;static embed(t){const i=t.id.replace("/pen/","/embed/"),e=i.split("/")[0],a=i.split("/")[2];return`<iframe src="https://codepen.io/${i}?height=${t.h}&theme-id=0&slug-hash=${a}&default-tab=result&user=${e}" frameborder="0" scrolling="no" allowtransparency="true" allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return`https://codepen.io/${t}/image/large.png`}static link(t){return`https://codepen.io/${t.replace("/embed/","/pen/")}`}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){let a=e[1].replace("/embed/","/pen/");const s=this.link(a),r=this.thumbnail(a);i(a,s,r)}}}class w{static type="bandcamp";static dataAttribute="bandcamp-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?([a-zA-Z0-9_\-]*\.bandcamp\.com\/(?:album|track)\/[a-zA-Z0-9_\-%]*)/;static embed(t){return`<iframe src="https://bandcamp.com/EmbeddedPlayer/${t.id}/size=large/bgcol=ffffff/linkcol=333333/tracklist=true/artwork=small/transparent=true/" frameborder="0" scrolling="no" allowtransparency="true" allowfullscreen seamless></iframe>`}static thumbnail(t){return""}static link(t){return t.match(/^(album|track)=/)?"":`https://${t}`}static getData(t){return fetch(`/api/bandcamp?url=${encodeURIComponent(t)}`).then(i=>i.json()).catch(()=>({id:null,thumbnail:null}))}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const s=`https://${e[1]}`;this.getData(s).then(r=>{r.id&&i(r.id,s,r.thumbnail||"")})}}}class v{static type="giphy";static dataAttribute="giphy-id";static regex=/(?:https?:\/\/)?(?:w{3}\.)?giphy\.com\/gifs\/([a-zA-Z0-9_\-%]*)/;static embed(t){return`<iframe width="${t.w}" height="${t.h}" src="https://giphy.com/embed/${t.id}/twitter/iframe" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen allow=autoplay></iframe>`}static thumbnail(t){return`https://media.giphy.com/media/${t}/giphy_s.gif`}static link(t){return`https://giphy.com/gifs/${t}`}static buildFromText(t,i){const e=t.split("/"),a=e[e.length-1],s=a.split("-"),r=s[s.length-1];if(r){const o=this.link(a),c=this.thumbnail(r);i(r,o,c)}}}class k{static type="video";static dataAttribute="video-url";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(.+\.(?:mp4|mov|m4v))(?:\/|$|\s|\?|#)/;static embed(t){const i=t.autoplay===!0?' autoplay="true"':"",e=t.loops===!0?' loop="true"':"",a=t.muted===!0?" muted":"";return`<video src="${t.id}" width="${t.w}" height="${t.h}"${i}${e}${a} controls playsinline webkitallowfullscreen mozallowfullscreen allowfullscreen></video>`}static thumbnail(t){return t.replace(".mp4","-poster.jpg").replace(".mov","-poster.jpg").replace(".m4v","-poster.jpg")}static link(t){return t}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const a=e[1],s=this.thumbnail(a);i(a,a,s)}}}class x{static type="gif";static dataAttribute="gif-url";static regex=/(?:https?:\/\/)?(?:w{3}\.)?(.+\.gif)(?:\/|$|\s|\?|#)/;static embed(t){return`<img class="gif" src="${t.id}" width="${t.w}" height="${t.h}">`}static thumbnail(t){return t.replace(".gif","-poster.jpg")}static link(t){return t}static buildFromText(t,i){const e=t.match(this.regex);if(e&&e[1]){const a=e[1],s=this.thumbnail(a);i(a,a,s)}}}const m=[k,x,d,b,A,f,g,y,w,v];class n extends HTMLElement{defaultThumbnail="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAArwAAAGcAQMAAAABMOGrAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAADUExURQAAAKd6PdoAAAA6SURBVHja7cGBAAAAAMOg+VPf4ARVAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAN488AAGP4e1mAAAAAElFTkSuQmCC";static EMBETTER_ACTIVATED="embetter-activated";connectedCallback(){setTimeout(()=>{this.isConnected&&(this.shadow||(this.initialHTML=this.innerHTML,this.innerHTML="",this.shadow=this.attachShadow({mode:"open"}),this.el=this.shadow??this,this.initComponent(),this.render(),this.getElements(),this.checkThumbnail()),this.addListeners())},0)}disconnectedCallback(){this.unembedMedia(),this.removeAttribute("ready"),this.playButton&&this.playButton.removeEventListener("click",this.clickListener),document.removeEventListener(n.EMBETTER_ACTIVATED,this.embedListener),this.observer&&(this.observer.disconnect(),this.observer=null)}initComponent(){this.markup="embetter-media component not initialized properly.",this.loops=this.hasAttribute("loops"),this.muted=this.hasAttribute("muted"),this.findAndActivateService()}getElements(){this.thumbnail=this.el.querySelector("img")}addListeners(){this.clickListener=this.onClick.bind(this),this.playButton=this.el.querySelector(".embetter-play-button"),this.playButton&&this.playButton.addEventListener("click",this.clickListener),this.embedListener=this.onEmbedActivated.bind(this),document.addEventListener(n.EMBETTER_ACTIVATED,this.embedListener)}onClick(t){t.preventDefault(),this.embedMedia()}onEmbedActivated(t){t.detail!==this&&this.unembedMedia()}findAndActivateService(){for(let t of m){let i=t.dataAttribute;if(this.hasAttribute(i)){this.service=t,this.serviceType=t.type,this.serviceId=this.getAttribute(i),this.markup=this.playerHTML(t.link(this.serviceId));break}}}onReady(){this.removeAttribute("loading"),this.setAttribute("ready","")}checkThumbnail(){this.thumbnail&&(this.setAttribute("loading",""),this.thumbnail.complete?this.onReady():(this.thumbnail.onload=()=>{this.onReady()},this.thumbnail.onerror=()=>{if(this.thumbnail.src.includes("/maxresdefault.jpg")){this.thumbnail.src=this.thumbnail.src.replace("/maxresdefault.jpg","/0.jpg");return}this.thumbnail.src=this.defaultThumbnail,this.onReady()}),setTimeout(()=>{this.thumbnail.height<50&&(this.thumbnail.src=this.defaultThumbnail),this.onReady()},4e3))}setupMobileObserver(){this.isMobile()&&(this.observer=new IntersectionObserver(t=>{t.forEach(i=>{i.isIntersecting?this.embedMedia(!1):this.unembedMedia()})},{threshold:.3}),this.observer.observe(this))}embedMedia(t){if(document.dispatchEvent(new CustomEvent(n.EMBETTER_ACTIVATED,{bubbles:!0,composed:!0,detail:this})),this.hasAttribute("playing"))return;t===void 0&&(t=this.hasAttribute("autoplay")?this.getAttribute("autoplay")!=="false":!0);let i=this.service.embed({id:this.serviceId,w:this.thumbnail&&this.thumbnail.width||"100%",h:this.thumbnail&&this.thumbnail.height||"100%",autoplay:t,loops:this.loops,muted:this.muted||t});this.playerEl=n.stringToDomElement(i),this.el.appendChild(this.playerEl),this.setAttribute("playing","")}unembedMedia(){this.playerEl!=null&&this.playerEl.parentNode!=null&&this.playerEl.parentNode.removeChild(this.playerEl),this.removeAttribute("playing")}isMobile(){return/iphone|ipad|ipod|android/i.test(navigator.userAgent)}static stringToDomElement(t){var i=document.createElement("div");return i.innerHTML=t,i.firstChild}static componentHTML(t,i,e=null,a=null){let s="";if(a||e){const r=a||"#",o=e?`<img src="${e}">`:"";s=`<a href="${r}">${o}</a>`}return`<embetter-media ${t}="${i}">${s}</embetter-media>`}playerHTML(t){return`
139
+ ${this.initialHTML}
140
+ <div class="embetter-loading"></div>
141
+ <div class="embetter-play-button"></div>
144
142
  `}css(){return h}html(){return this.markup}render(){this.el.innerHTML=`
145
143
  ${this.html()}
146
144
  <style>${this.css()}</style>
147
- `,this.getElements()}static register(){customElements.define("embetter-media",l)}static upgradeLegacyEmbeds(t=document){t.querySelectorAll(".embetter").forEach(e=>{for(const s of m){const a=`data-${s.dataAttribute}`;if(e.hasAttribute(a)){const r=e.getAttribute(a),o=document.createElement("embetter-media");o.setAttribute(s.dataAttribute,r);const c=e.querySelector("a");c&&o.appendChild(c.cloneNode(!0)),e.hasAttribute("data-loops")&&o.setAttribute("loops",""),e.hasAttribute("data-muted")&&o.setAttribute("muted",""),e.replaceWith(o);break}}})}static componentMarkupFromURL(t,i){for(let e=0;e<m.length;e++){const s=m[e];if(t.match(s.regex)!=null){s.buildFromText(t,(a,r,o)=>{const c=u=>{let p=l.componentHTML(s.dataAttribute,a,u,r);i(p,s)};if(!o&&s.getData&&r){s.getData(r).then(u=>{const p=typeof u=="string"?u:u?.thumbnail;c(p||o)}).catch(()=>c(o));return}c(o)});break}}}}return l.register(),l}));
145
+ `}static register(){customElements.define("embetter-media",n)}static upgradeLegacyEmbeds(t=document){t.querySelectorAll(".embetter").forEach(e=>{for(const a of m){const s=`data-${a.dataAttribute}`;if(e.hasAttribute(s)){const r=e.getAttribute(s),o=document.createElement("embetter-media");o.setAttribute(a.dataAttribute,r);const c=e.querySelector("a");c&&o.appendChild(c.cloneNode(!0)),e.hasAttribute("data-loops")&&o.setAttribute("loops",""),e.hasAttribute("data-muted")&&o.setAttribute("muted",""),e.replaceWith(o);break}}})}static componentMarkupFromURL(t,i){for(let e=0;e<m.length;e++){const a=m[e];if(t.match(a.regex)!=null){a.buildFromText(t,(s,r,o)=>{const c=u=>{let p=n.componentHTML(a.dataAttribute,s,u,r);i(p,a)};if(!o&&a.getData&&r){a.getData(r).then(u=>{const p=typeof u=="string"?u:u?.thumbnail;c(p||o)}).catch(()=>c(o));return}c(o)});break}}}}return n.register(),n}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "embetter",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "type": "module",
5
5
  "description": "Lazy-loading iframe embed web component",
6
6
  "main": "dist/embetter-media.umd.js",