Substack: GIF a riproduzione su clic
Mette in pausa video in loop e GIF e aggiunge un pulsante "play" per evitare l'avvio automatico.
Codice da copiare
// Replace autoplaying GIFs (and looped MP4s used as GIFs) with click-to-play stills
(function () {
function stopVideo(v) {
try { v.autoplay = false; v.loop = false; v.removeAttribute('autoplay'); v.pause(); } catch (e) {}
if (v.dataset.jzGif) { return; }
v.dataset.jzGif = '1';
v.controls = false;
var play = document.createElement('button');
play.type = 'button';
play.textContent = '▶ play';
play.style.cssText = 'position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);background:rgba(0,0,0,.7);color:#fff;border:none;border-radius:18px;padding:6px 12px;cursor:pointer;font:600 13px/1 system-ui,sans-serif;z-index:5;';
var wrap = v.parentNode;
if (wrap && getComputedStyle(wrap).position === 'static') { wrap.style.position = 'relative'; }
if (wrap) { wrap.appendChild(play); }
play.addEventListener('click', function () {
try { v.play(); } catch (e) {}
play.remove();
});
}
function patch() {
document.querySelectorAll('video[autoplay], video[loop]').forEach(stopVideo);
document.querySelectorAll('img[src$=".gif" i]').forEach(function (img) {
if (img.dataset.jzGif) { return; }
img.dataset.jzGif = '1';
var orig = img.getAttribute('src') || '';
img.addEventListener('click', function () { img.setAttribute('src', orig + (orig.indexOf('?') > -1 ? '&' : '?') + 'jz=' + Date.now()); }, { once: true });
// freeze by reloading once it's in the DOM (browsers usually animate on load only)
img.style.cursor = 'pointer';
img.title = 'Click to play GIF';
});
}
patch();
var obs = new MutationObserver(function () {
clearTimeout(window.__jzSubGif);
window.__jzSubGif = setTimeout(patch, 300);
});
obs.observe(document.body, { childList: true, subtree: true });
})();
Come usare questo esempio
- Copia il codice con il pulsante qui sopra.
- Installa JustZix (2 minuti) e apri l'estensione sulla pagina di destinazione.
- Aggiungi una nuova regola corrispondente a quella pagina.
- Incolla il codice nel pannello JavaScript della regola e salva — viene eseguito a ogni visita della pagina.
Valuta questo esempio
Nessuna valutazione — sii il primo.