RSS as js variable

S

slyi

Hi All,
I was playing with the idea of getting an RSS feed as js
variable from one server http://www.geocities.com/sly_i/rssvar.js and
then parsing it via js on another server
http://www.myjavaserver.com/~slyi/myrss.htm. But i cant find any site
that provide the raw RSS as a js variable.

I asked the guys who make the great feed2js
http://jade.mcli.dist.maricopa.edu/feed/ but they dont believe the idea
of providing rss in a raw format in worth pursuing.

Do you know of any site that would provide this?
I dont mind if it contains google ads as long as you can specify the
rss url and recieve it as a js variable.

thanks for your help

slyi
 
F

fox

slyi said:
Hi All,
I was playing with the idea of getting an RSS feed as js
variable from one server http://www.geocities.com/sly_i/rssvar.js and
then parsing it via js on another server
http://www.myjavaserver.com/~slyi/myrss.htm. But i cant find any site
that provide the raw RSS as a js variable.

I asked the guys who make the great feed2js
http://jade.mcli.dist.maricopa.edu/feed/ but they dont believe the idea
of providing rss in a raw format in worth pursuing.

there's no real advantage... many newer mail and news clients will read
rss files.

If you want the feeds available to JS for development purposes, with a
very little bit of help from php you can simply:


<offtopic>

<?php //4.30 or better

if(isset($_REQUEST) && isset($_REQUEST["url"]))
{
$contents = file_get_contents($url);

// remove all return and linefeed characters!
$contents = preg_replace("/\r|\f/","",$contents);

}
?>

<script ...>

// convert newlines to literal \n so they can be restored
// and add slashes to quotes
var rss = "<?=addslashes(preg_replace("/\n/","\\n",$contents));?>";

.....

</script>

If you do not need to preserve newlines, then you can remove them with
the returns and linefeeds (regex: "/\n|\r|\f/") and simply use
<?=addslashes($contents);?> above. However, you might find it easier if
the rss is broken down into chunks (you can rss.split("\\n")...). For
displaying in the textarea, i used rss.split("\\n").join("\n");

This script gets into serious trouble very quickly if the content is not
rss/xml... your script should control which feeds are available to the user.

I have an example at:
http://fxmahoney.com/demo/rss_to_js.htm?url=http://nytimes.com/services/xml/rss/nyt/Arts.xml

[if your reader wraps lines -- make sure you copy and paste the entire
url in the location bar]

it loads the contents of the rss/xml into a javascript variable which is
loaded into a form textarea upon onload. (View Source)

As far as i can tell, RSS 2.0 (0.91 tested also) feeds should do well
enough with this script. HTML will not!! [not ANY site i tested]

If all you have access to is php 3.x, I don't think you'll be able to do
this (fopen_wrappers required) -- if php >=4.0 and < 4.3, then use:

if($f = fopen($url, "r"))
{
while(!feof($f))
$contents .= fread($f, 8192);

fclose($f);
}


Do you know of any site that would provide this?
I dont mind if it contains google ads as long as you can specify the
rss url and recieve it as a js variable.

.... bandwidth issues ... better to do it yourself
 
S

slyi

Thanks fox. Your answer was most useful, i do know a bit of php and
have the server access
But i was hoping that someone knew of a service available on the web
for this
As this could be a workaround for the x-site scripting restrictions
client side RSS.
 
F

fox

slyi said:
Thanks fox. Your answer was most useful, i do know a bit of php and
have the server access
But i was hoping that someone knew of a service available on the web
for this
As this could be a workaround for the x-site scripting restrictions
client side RSS.

if you have the bandwidth for it -- or want to get involved in
"subscriptions" for the service (or free if the demand is not high),
then the easiest thing to do is use the php to generate a *.js file from
the rss feed as illustrated.

One way is to set *.js files to be pre-processed by php by adding the
file type to .htaccess:

AddType application/x-httpd-php .js

users would directly access the php disguised as js served from your domain.

[**i'm pretty sure you can simply use <script src=myscript.php... just
as easily without messing with htaccess]

<script
src="http://yourdomain.com/scripts/rss.js?feed=http://somedomain.com/rss.xml"
type=text/javascript></script>

//-------rss.js:

<?

if(isset($_REQUEST) && isset($_REQUEST["feed"]))
{
$contents = file_get_contents($feed);
$contents = preg_replace("/\r|\f/","",$contents);
}

?>

var rss = "<?=addslashes(preg_replace("/\n/","\\n",$contents));?>";

// -------end rss.js

[ a lot of people don't realize that .js files can be passed
location.search (aka, GET) data just like any other URL ]

Add some parsing functions and your ready to serve RSS via JS yourself.


I'm not familiar with x-site, so I really couldn't make any further
recommendations about possible workarounds.

fox
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top