|
| 1 | +function updateElement(elem) { |
| 2 | + const _nid = elem.getAttribute('nid'); |
| 3 | + const nid = `${_nid.slice(0, 5)}...${_nid.slice(-5)}`; |
| 4 | + const thumbnail = elem.getAttribute('thumbnail'); |
| 5 | + const creator = elem.getAttribute('creator') || 'N/A'; |
| 6 | + const headline = elem.getAttribute('headline'); |
| 7 | + const _captureUpdatedDate = new Date(elem.getAttribute('captureUpdatedDate')); |
| 8 | + // Format the date to "Jul 25 2024 15:05:45" in GMT+8 timezone |
| 9 | + const options = { |
| 10 | + year: 'numeric', |
| 11 | + month: 'short', |
| 12 | + day: 'numeric', |
| 13 | + hour: '2-digit', |
| 14 | + minute: '2-digit', |
| 15 | + second: '2-digit', |
| 16 | + hour12: false, |
| 17 | + timeZone: 'Asia/Taipei', |
| 18 | + }; |
| 19 | + const captureUpdatedDate = _captureUpdatedDate.toLocaleString( |
| 20 | + 'en-US', |
| 21 | + options |
| 22 | + ); |
| 23 | + |
| 24 | + // Clear existing content safely |
| 25 | + elem.replaceChildren(); |
| 26 | + |
| 27 | + // Build DOM safely without innerHTML to prevent XSS |
| 28 | + const captureEye = document.createElement('capture-eye'); |
| 29 | + captureEye.setAttribute('nid', _nid); |
| 30 | + |
| 31 | + const container = document.createElement('div'); |
| 32 | + container.className = 'container'; |
| 33 | + |
| 34 | + const img = document.createElement('img'); |
| 35 | + img.setAttribute('src', thumbnail); |
| 36 | + img.setAttribute('alt', 'Image'); |
| 37 | + img.className = 'image'; |
| 38 | + |
| 39 | + const textContainer = document.createElement('div'); |
| 40 | + textContainer.className = 'text-container'; |
| 41 | + |
| 42 | + const createParagraph = (className, text) => { |
| 43 | + const p = document.createElement('p'); |
| 44 | + p.className = className; |
| 45 | + p.textContent = text; |
| 46 | + return p; |
| 47 | + }; |
| 48 | + |
| 49 | + textContainer.appendChild(createParagraph('title', '註冊時間')); |
| 50 | + textContainer.appendChild(createParagraph('description', captureUpdatedDate)); |
| 51 | + textContainer.appendChild(createParagraph('title', '創建者')); |
| 52 | + textContainer.appendChild(createParagraph('description', creator)); |
| 53 | + textContainer.appendChild(createParagraph('title', '簡介')); |
| 54 | + textContainer.appendChild(createParagraph('description', headline)); |
| 55 | + textContainer.appendChild(createParagraph('title', '影像 Nid')); |
| 56 | + |
| 57 | + const nidP = createParagraph('nid', nid); |
| 58 | + nidP.style.cursor = 'pointer'; |
| 59 | + nidP.addEventListener('click', () => { |
| 60 | + window.open( |
| 61 | + `https://asset.captureapp.xyz/${encodeURIComponent(_nid)}`, |
| 62 | + '_blank', |
| 63 | + 'noopener' |
| 64 | + ); |
| 65 | + }); |
| 66 | + textContainer.appendChild(nidP); |
| 67 | + |
| 68 | + container.appendChild(img); |
| 69 | + container.appendChild(textContainer); |
| 70 | + captureEye.appendChild(container); |
| 71 | + elem.appendChild(captureEye); |
| 72 | +} |
| 73 | + |
| 74 | +class CaptureEyeStyleElement extends HTMLElement { |
| 75 | + static observedAttributes = ['creator']; |
| 76 | + |
| 77 | + connectedCallback() { |
| 78 | + updateElement(this); |
| 79 | + } |
| 80 | + |
| 81 | + attributeChangedCallback(name, oldValue, newValue) { |
| 82 | + updateElement(this); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +customElements.define('capture-eye-style-element', CaptureEyeStyleElement); |
0 commit comments