User:Vitrinekast/Snatch these snippets: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 14: Line 14:
         link.insertAdjacentElement("afterend", audioEl)
         link.insertAdjacentElement("afterend", audioEl)
     };
     };
    // or an image
    if(link.href.match(/\.(?:jpg|png)$/i)) {
        var imgEl = document.createElement("img");
        imgEl.src = link;
        imgEl.style.display = "block";
        link.insertAdjacentElement("afterend", imgEl)
    }
});
});
</syntaxhighlight>
</syntaxhighlight>

Revision as of 11:42, 9 October 2023

Copy-paste this into your console! (Right click -> inspect elements > console)

Adding audio elements to index files

// select all links
document.querySelectorAll("a").forEach((link) => {
    // if it includes your audio format (regex)
    if(link.href.match(/\.(?:wav|mp3)$/i)) {
        // create an audio element and set its source to yourlink
        var audioEl = document.createElement("audio");
        audioEl.src = link.href;
        audioEl.controls = true;
        audioEl.style.display = "block";
        link.insertAdjacentElement("afterend", audioEl)
    };

    // or an image
    if(link.href.match(/\.(?:jpg|png)$/i)) {
        var imgEl = document.createElement("img");
        imgEl.src = link;
        imgEl.style.display = "block";
        link.insertAdjacentElement("afterend", imgEl)
    }
});