views:

238

answers:

4

I have a html page on my localhost - get_description.html.

The snippet below is part of the code:

<input type="text" id="url"/>
<button id="get_description_button">Get description</button>
<iframe id="description_container" src="#"/>

When the button is clicked the src of the iframe is set to the url entered in the textbox. The pages fetched this way are very big with lots of linked files. What I am interested in the page is a block of text contained in a <div id="description"> element.

Is there a way to mitigate downloading of resources linked in the page that loads into the iframe?

I don't want to use curl because the data is only available to logged in users and the steps to take with curl to get the content is too complicated. The iframe is simple as I use this on a box which sends the right cookies to identify the request as coming from a logged in user, but the problem is that it is very wasteful to get nearly 1 MB of data to keep 1 KB of it and throw out the rest.

Edit

If the proposed method just works in Firefox it is fine, so I added firefox tag. Also, it is possible that the answer actually is from the realm of Firefox add-on techniques, so I added that tag as well.

The problem is not that I cannot get at what I'm looking for, rather, the problem is the easy iframe method is wasteful.

I know that Firefox does allow loading only the text of a page. If you open a page and press Ctrl+U you are taken to 'view page source' window, There links behave as normal and are clickable, if you click on a link in source view, the source of the new page is loaded into the view source window, without the linked resources being downloaded, exactly what I'm trying to get. But I don't know how to access this behaviour.

Another example is the Adblock add-on. It somehow kills elements before they get loaded. With plain javascript this is not possible. Because it only is triggered too late to intervene in good time.

A: 

There are various ways to go about this in AJAX, I'm going to show the jQuery way for brevity as one option, though you could do this in vanilla JavaScript as well.

Instead of an <iframe> you can just use a container, let's say a <div> like this:

<div id="description_container"></div>

Then to load it:

$(function() {
  $("#get_description_button").click(function() {
    $("#description_container").load($("input").val() + " #description");
  });
});

This uses the .load() method which takes a string in this format: .load("url selector"), then takes that element in the page and places it's content inside the container you're loading, in this case #description_container.


This is just the jQuery route, mainly to illustrate that yes, you can do what you want, but you don't have to do it exactly like this, just showing the concept is getting what you want from an AJAX request, rather than in an <iframe>.

Nick Craver
Thank you Nick, but this will not work because, as I mentioned in my question, the html page is on my localhost and `.load()` does not work cross domain. That is why I reverted to using an `iframe` in the first place. But even if it did, I'm not sure anything prevented the linked resources from starting loading. I'm going to add more clarification to my question.
Majid
+2  A: 

The Same Origin Policy forbids any web page to access contents of any other web page in a different domain so basically you cannot do that.

However it seems that with some browsers it is allowed to access web pages content if you are trying to access it from a local web page which seems to be your case.

Safari, IE 6/7/8 are browser that allow a local web page to do so via XMLHttpRequest (source: Google Browser Security Handbook) so you may want to choose to use one of those browsers to do what you need (note that future versions of those browsers may not allow to do so anymore).

A part from this solution I only see two possibities:

  • If the web pages you need to fetch content from are somehow controlled by you, you can create a simpler interface to let other web pages to get the content you need (for example allowing JSONP requests).
  • If the web pages you need to fetch content from are not controlled by you the only solution I see is to fetch content server side logging in from the server directly (I know that you don't want to do so, but I don't see any other possibility if the previous I mentioned are not practicable)

Hope it helps.

Andrea Zilio
A: 

Actually I've seen Cross Domain jQuery .load request before, here: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

The author claims that codes like these found on that page

$('#container').load('http://google.com'); // SERIOUSLY!

$.ajax({
    url: 'http://news.bbc.co.uk',
    type: 'GET',
    success: function(res) {
        var headline = $(res.responseText).find('a.tsh').text();
        alert(headline);
    }
});

// Works with $.get too!

would work. (The BBC code might not work because of the recent redesign, but you get the idea)

Apparently it is using YQL wrapped into a jQuery plugin to do the trick. Now I cannot say I fully understand what he is doing there but it appears to work, and fits the bill. Once you load the data I suppose it is a simple matter of filtering out the data that you need.

If you prefer something that works at the browser level, may I suggest Mozilla's Jetpack framework for lightweight extensions. I've not yet read the documentations in its entirety but it should contain the APIs needed for this to work.

Yi Jiang
Note that the requested URL would be loaded from Yahoo servers and so that would not work with URLs that requires to be logged in.
Andrea Zilio
Hmmm... yes. Although I noticed one of the comments attached to that post that noted that POST data can also be used, which means that authentication can be done that way, no? Or is it that I'm not fully understanding how this approach works?
Yi Jiang
Wow, you're right! I would have never thought that it would have been possible! The only problem in this case is that credentials would be available to yahoo servers.
Andrea Zilio
A: 

Your description sounds like you are fetching pages from the same domain (you said that you need to be logged in and have session credentials) so have you tried to use async request via XMLHttpRequest? It might complain if the html on a page is particularly messed up but you chould still be able to get raw text via .responseText and extract what you need with a regex.

ZXX
XMLHttpRequest is what is used internally on IE when you use jQuery's `.get()`, so having tried jQuery means I have tried this path. Thank you for the suggestion though.
Majid
And it was still loading images and other non-text stuff? Or just gave you some error?
ZXX