Search code examples
phpsimple-html-dom

Scraping Js file with Simple HTML DOM


Im using Simple HTML DOM to scrape a Javascript file like this:

$html = file_get_html('http://www.The-Javascript-File.js');
echo $html;

The problem is that I get this error in console:

Resource interpreted as Script but transferred with MIME type text/html: http://www.The-Javascript-File.js

Obviously because its a HTML parser('file_get_html')...Is there something I can do with Simple HTML DOM to get the file as a proper js file?


Solution

  • SimpleHTMLDom, as the name indicates is for parsing HTML, not JavaScript. If you want to parse JavaScript, then why not simply use file_get_contents()?

    $js = file_get_contents('http://www.example.com/file.js');
    //parse it!
    

    There's also UglifyJS (originally based on parse-js). You may want to check that out, as well.