JavaScript only RSS parser?
June 17, 2005 6:31 AM   Subscribe

I wasted a lot of time the other day trying to reinvent the wheel. I wanted to write a web-based RSS parser.

I was trying to write it as a JavaScript-only parser. I ran into a lot of frustrations with the security restrictions built into client-side JavaScript, so I thought I'd ask: Can one open and parse a remote file with DOM navigation using only JavaScript, or am I barking up the wrong tree?

Thanks!
posted by jpburns to Computers & Internet (9 answers total)
 
I don't believe there's any way to do this with the normal security settings. If you find a way, please let me know.
posted by null terminated at 6:39 AM on June 17, 2005


i don't know what the restrictions are on javascript, but would the following be possible (i don't think it's a good solution, but it's just an "in principle" argument):

- the page is divided into two frames
- javascript makes the browser open the rss feed into the "other" frame
- browser loads rss file to frame
- javascript parses dom in the "other" frame
- javascript prints output to "own" frame
- repeat
(i'm assuming that javascript itself can't load files from hosts other than the source host, but that it can request that the browser do so).
posted by andrew cooke at 7:11 AM on June 17, 2005


Nope; javascript can't in any way access a frame that was loaded from a different domain. Deliberate security measure built into the browser. (And a good thing, too, inconvenient though it may be when you're working for a client whose application is spread across three different domains...)
posted by ook at 7:50 AM on June 17, 2005


You could use XMLHttpRequest/ActiveXObject("Microsoft.XMLHTTP"), but in Firefox you can only request files from the same host as the JavaScript is being served from (though I'm not sure exactly how this would work with a local file not being served from anywhere). It would work in IE, though.

Andrew Cooke's idea is a good one as well. You could use a hidden IFRAME which would be transparent to the user, also. This page would seem to indicate that it's possible to do what you're trying to without too much pain.
posted by IshmaelGraves at 7:51 AM on June 17, 2005


javascript parses dom in the "other" frame

This is where you'll run into problems. While Javascript can "tell" a frame or iframe to load content from another site, it can't parse information about the HTML page in the other frame.

The only way one frame (or window, for that matter) would be allowed access to another frame's DOM would be if both pages resided on the same server.
posted by alan at 7:55 AM on June 17, 2005


ok, so is the following a bug i should report? i was playing with svg, which has embedded javascript. the javascript was registered with the dom "onload" event and was being called for any page loaded in the browser (not just the page with the svg graphics). i had to add code that tested for the right page.

(this was a beta version of firefox with svg support, so it may well have bugs).
posted by andrew cooke at 8:13 AM on June 17, 2005


Andrew:

- the page is divided into two frames
- javascript makes the browser open the rss feed into the "other" frame
- browser loads rss file to frame
- javascript parses dom in the "other" frame
- javascript prints output to "own" frame
- repeat

That was the logic I was following, but ran into permissions problems, thus my question.

I think that the answer is you can't, but would love to be proven wrong...
posted by jpburns at 3:33 PM on June 17, 2005


yeah, sorry. that's how it is with java applets (or used to be, at least, back in the day). i still don't understand why my onload function was called for other pages though. perhaps it's different if the javascript is loaded locally?

you can always put a proxy on you server and make a request something like http://you.server.com/proxy?target=other.server.com/path/page.html (but of course that doesn't scale)
posted by andrew cooke at 4:16 PM on June 17, 2005


If both domains are controlled by your client/employer/whatever, can you get them to use a hostname?

e.g. You want the javascript on www.firstdomain.com to access www.seconddomain.com, so you make bob.firstdomain.com an alias for www.seconddomain.com

(This is just off the top of my head so you might want to research/test that this would actually work before trying to implement it)
posted by winston at 10:12 AM on June 18, 2005


« Older Emergency Statistics Help   |   Simple BMP Smoother/Res'er Newer »
This thread is closed to new comments.