fngryboi fngryboi - 10 months ago 50
Javascript Question

Detect media files using an array with Javascript and return all the urls

I'm trying to figure out a way to extract links for media files on any website using Javascript. I'm working with Unity3D but it's related to using uWebkit which is a Web Browser plugin, this web browser plugin allows me to inject Javascript into a site so this will (hopefully) work out.

The purpose is to return a list of URLs for all the media links that I want the script to find (*.mp3, *.ogg, *.mp4, *.pdf etc.) which I then can click to open in a separate window with just that file in a custom video player/music player/pdf reader etc. I realize not all websites leave these in the open and I'll make a work-around for those.

I've searched around for a script or documentation about it here, on search engines and on GitHub without success.

PS: jQuery is fine too.


Building off of Caleb's answer, I would just optimize it for performance using a data structure to store the filetypes you want to check for.

var extensions = {};
/* etc */

var extPattern = /\.([0-9a-z]+)(?:[\?#]|$)/i;

var links = [];
    var href = $(this).attr('href');
    if (typeof href !== 'undefined') {
      var ext = href.match(extPattern) 
      if(ext!= null && ext.length >1) {
        if(extensions[ext] != undefined) {