0

Summary:

I'm looking to save the first 4kB of a webpage (actually an MP3, but shouldn't matter) from another domain to a variable in JavaScript, ideally without jQuery, as I don't need jQuery for anything else on my page. This is what I have used before in a python app previously to achieve the same goal:

host = 'http://www.wikipedia.org/somepath/tosome/file.mp3'
req = urllib2.Request(host, headers={'User-Agent' : "Magic Browser"})
response = urllib2.urlopen(req).read(4*1024)

.

Background:

I'm aiming to get just the few kilobytes of an MP3 file (from a different domain) so I can strip the ID3 info from it. However after a lot of googling, and messing around with xmlhttprequests, jsonp, and a few other things, I still cant find something that fulfills all of my requirements. Reading this post- Basic example of using .ajax() with JSONP? was really helpful but I cant seem to work out how to limit the callback size of something like that (perhaps limiting the scripts source size?) and I REALLY don't want to have to include the jQuery library.

Any help greatly is appreciated!

Community
  • 1
  • 1
tomatosource
  • 256
  • 2
  • 12
  • I think you'd have to use a server side language for that. JSONP is not really suitable to download arbitrary data (well, it's possible as long as the final result is valid JavaScipt) and it does not give you *any* control over the request. – Felix Kling Jan 12 '13 at 16:37
  • First thing to do is find out whether the site you're accessing has the appropriate headers to allow cross-domain access. If it doesn't, and it has no JSONP API (which for your purpose wouldn't really work anyway unless it was designed to do exactly what you're asking), then you can't do it from JavaScript in a browser. – Pointy Jan 12 '13 at 16:37
  • Do you need a client-side or a server-side solution? For server-side you can use [Node.js](http://nodejs.org) to implement this thing in JavaScript. – Stan Jan 12 '13 at 16:44
  • @pointy - The files will be served up by dropbox media URLs (basically temporary dl URLs) and from this http://stackoverflow.com/questions/6964598/dropbox-jsonp-file it looks like the support the JSONP API. (AT)stan - Client side unfortunately. – tomatosource Jan 12 '13 at 16:44
  • @FelixKling would it be possible to keep an eye on where the data is being read to and then cancel the request once I have enough data? – tomatosource Jan 12 '13 at 16:48
  • 1
    @tomatosource The basic way that JSONP works makes it impossible to do that. – Pointy Jan 12 '13 at 16:48
  • Also that other question is useful only if you have direct control over the file. If you had such control, then it would make far more sense to just strip out the ID3 information and provide that directly. – Pointy Jan 12 '13 at 16:50
  • Ok- so ruling is, not possible? Hmm ok. Thanks everyone, will have to start working on alternatives then! – tomatosource Jan 12 '13 at 16:54

1 Answers1

1

Note: am posting a solution that contains javascript + php. But you can use any other programming laguage like python (only syntax changes. not logic).

. Use Ajax to call a php file in Your server

. Inside your server side php file:

Use curl to read the cross domain html.

$url = "http://www.google.com;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);

. Then use PHP's DOM object model to parse the HTML - (http://php.net/manual/en/domdocument.loadhtml.php).

For example:

$domObj = new DOMDocument;
$domObj->loadHTML( $output);
$divs = $domObj->getElementsByTagName('div');

. Return the o/p html content back to the client side.

. Finally collect the ajax response (html) into a local javascript variable.

Jirilmon
  • 1,924
  • 1
  • 12
  • 13
  • Thanks for the suggestion, unfortunately I am limited to the client side only unfortunately, so I'm working my way around the issue in other ways...Giving you the tick though as I feel like there doesn't seem to be a better answer to the question I proposed. – tomatosource Jan 13 '13 at 13:29