Spaces:
Running
Running
File size: 3,872 Bytes
416b544 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /* A still fallback can retain the artifact's ring as a static representation.
The source image is captured from the artifact's own settled ring canvas. */
(function () {
'use strict';
var host = document.querySelector('[data-composer-window]');
if (!host) return;
var imageUrl = new URL('assets/fallback-ring.png', document.baseURI).href;
var watched = new WeakSet();
function watch(frame) {
if (watched.has(frame)) return;
watched.add(frame);
var generation = 0;
function attach() {
var win, doc, current = ++generation, request = 0;
try { win = frame.contentWindow; doc = frame.contentDocument; if (!win || !doc) return; } catch (_) { return; }
function valid(ticket) {
try { return frame.isConnected && generation === current && request === ticket && frame.contentWindow === win && frame.contentDocument === doc; }
catch (_) { return false; }
}
win.addEventListener('message', function (event) {
if (!valid(request) || event.source !== window || event.origin !== location.origin || !event.data) return;
if (event.data.lensmithHost === 'play') {
request++;
if (frame.isConnected && frame.contentDocument === doc) {
var old = doc.querySelector('img.space-fallback-ring');
if (old) old.remove();
}
return;
}
if (event.data.lensmithHost !== 'still' || !doc.location || doc.location.hash !== '#replay') return;
var ticket = ++request, deadline = Date.now() + 10000;
function settle() {
if (!valid(ticket)) return;
var stage = doc.getElementById('cmpStage'), parent = stage && stage.offsetParent;
if (!stage || !parent) return;
if (doc.body.getAttribute('data-replay') !== 'done') {
if (Date.now() < deadline) window.setTimeout(settle, 100);
return;
}
if (doc.querySelector('canvas.cmp-entry-sand-rest') || doc.querySelector('img.space-fallback-ring')) return;
var img = doc.createElement('img');
img.className = 'space-fallback-ring';
img.alt = '';
img.setAttribute('aria-hidden', 'true');
img.style.cssText = 'position:absolute;pointer-events:none;z-index:3;';
img.onload = function () {
var decoded = img.decode ? img.decode() : Promise.resolve();
decoded.then(function () {
if (!valid(ticket) || !img.naturalWidth || !img.naturalHeight || !stage.isConnected || !parent.isConnected || doc.querySelector('canvas.cmp-entry-sand-rest') || doc.querySelector('img.space-fallback-ring')) return;
parent.appendChild(img);
function position() {
if (!valid(ticket) || !img.isConnected) return;
var r = stage.getBoundingClientRect(), p = parent.getBoundingClientRect();
img.style.left = (r.left - p.left - parent.clientLeft - 6) + 'px';
img.style.top = (r.top - p.top - parent.clientTop - 6) + 'px';
img.style.width = (r.width + 12) + 'px';
img.style.height = (r.height + 12) + 'px';
}
position();
win.addEventListener('resize', position, { passive: true });
}).catch(function () { /* Plain finished card remains visible. */ });
};
img.onerror = function () { /* Plain finished card remains visible. */ };
img.src = imageUrl;
}
window.setTimeout(settle, 0);
});
}
frame.addEventListener('load', attach);
try { if (frame.contentDocument && frame.contentDocument.readyState === 'complete') attach(); } catch (_) {}
}
function scan() { host.querySelectorAll('iframe').forEach(watch); }
new MutationObserver(scan).observe(host, { childList: true });
scan();
})();
|