0

I'd like to display, in my app, only a part of a web page. On this website, I'd like to display, in my app, only the div id "MovieCart".

enter image description here

What should I write in my as3 code in order to do so ?

For now, I have this line :

webView.loadURL("http://www.cinecity.nc/Cinecity/Film/40565");

But, of course, it's displaying the fullwebpage.


EDIT

So, I've tried this :

 webView.addEventListener(Event.COMPLETE,onComplete);
var res : String = ExternalInterface.call("function(){return document.getElementById('movieCart').outerHTML}");
var urlOfMovie: URLRequest = new URLRequest("http://www.cinecity.nc/Cinecity/Film/40567");
var loaderMovie:URLLoader = new URLLoader();
loaderMovie.load(urlOfMovie);
 webView.loadString(res);

But, as it's an AIR app, ExternalInterface.call can't be call. Any idea ?

user5870211
  • 103
  • 8
  • 1
    You'll need to load the html (url loader), strip out just that div you want, and use `webView.loadString(divString)` to show it. – BadFeelingAboutThis Aug 09 '16 at 23:49
  • Though it's probably easier to just do it in JS with something like Jquery. – BadFeelingAboutThis Aug 09 '16 at 23:57
  • Thx for the answer. So, in this case, I should add `webView.loadString(movieCart)` ? Is that correct ? (I don't know what to put at `divString`) – user5870211 Aug 10 '16 at 00:34
  • So, something like this : ? `var urlOfMovie: URLRequest = new URLRequest("http://www.cinecity.nc/Cinecity/Film/40567"); var loaderMovie:URLLoader = new URLLoader(); loaderMovie.load(urlOfMovie);` Then `webView.loadString(divString)`. But what should I put for `divString` ? – user5870211 Aug 10 '16 at 00:51
  • I've edited my original post. – user5870211 Aug 10 '16 at 00:55
  • Do you know html? The idea was you make a `string` whose text defines a basic "empty" html page (y'know.. with and tags etc). then you also add to your string the extracted code text of relevant "div" + supporting code from that website). In short... If you saved that html code to your computer, what do you have to delete to be left with only your required content? That remaining code is how your whole `string` should look like when loaded via `webView.loadString`. – VC.One Aug 10 '16 at 01:52
  • Untested, but you could try doing this: `webView.loadURL("javascript:document.body.innerHTML = document.getElementById("MovieCart").outerHTML");` This should rewrite the document so the movie cart is the only html inside the body. This would also keep any loaded CSS files in the head of the document intact for styling. If that works, let me know and I'll type it up as an answer – BadFeelingAboutThis Aug 10 '16 at 15:58
  • I've got this message : `TypeError: Result of expression 'document.getElementById('MovieCart')' [null] is not an object.` – user5870211 Aug 11 '16 at 08:27
  • Here's what I did : `var urlOfMovie: URLRequest = new URLRequest("http://www.cinecity.nc/Cinecity/Film/40567"); var loaderMovie:URLLoader = new URLLoader(); loaderMovie.load(urlOfMovie); webView.loadURL('javascript:document.body.innerHTML = document.getElementById("MovieCart").outerHTML')` – user5870211 Aug 11 '16 at 08:27
  • Yes, sorry, should clarified you need to load the webpage first, then call the JS command. – BadFeelingAboutThis Aug 11 '16 at 16:12

1 Answers1

0

Here is one easy way you can accomplish this:

//First, load the full page as you're currently doing:
webView.addEventListener(Event.COMPLETE, webLoadComplete); //listen for when the load is finished
webView.loadURL("http://www.cinecity.nc/Cinecity/Film/40565");

//runs when the load finishes
function webLoadComplete(e:Event):void {
    webView.removeEventListener(Event.COMPLETE, webLoadComplete); //stop listening

    //second, invoke the following Javascript on the page which assigns the `MovieCart` element as the html for the whole document body
    webView.loadURL("javascript:document.body.innerHTML = document.getElementById("MovieCart").outerHTML");
}

Disclaimer: Keep in mind that scrapping content from websites is generally frowned upon and you may be infringing on peoples work/copyrights by doing so.

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40