RSS as js variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slyi

    RSS as js variable

    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

  • fox

    #2
    Re: RSS as js variable



    slyi wrote:[color=blue]
    > 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.[/color]

    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($_REQU EST) && isset($_REQUEST["url"]))
    {
    $contents = file_get_conten ts($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",$conten ts));?>";

    .....

    </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:


    [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);
    }


    </offtopic>[color=blue]
    >
    > 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.[/color]

    .... bandwidth issues ... better to do it yourself[color=blue]
    >
    > thanks for your help
    >
    > slyi
    >[/color]

    Comment

    • slyi

      #3
      Re: RSS as js variable

      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.

      Comment

      • fox

        #4
        Re: RSS as js variable



        slyi wrote:[color=blue]
        > 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.
        >[/color]

        if you have the bandwidth for it -- or want to get involved in
        "subscripti ons" 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.ph p... just
        as easily without messing with htaccess]

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

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

        <?

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

        ?>

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

        // -------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

        Comment

        Working...