SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    florida
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS Feed Manipulation

    Is it possible to manipulate a js feed? The feed is for an entire article... I would like to create an 2-5 line abstract for my home page from that feed?

    <SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT"
    SRC="http://www.tv-now.com/js/news.js"></SCRIPT>

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That js looks like this...
    Code:
    <!--
    var msg = '<center>\n';
    msg = msg + '<BR><p><i>Celebrity News: Monday, November 17</i>\n';
    msg = msg + '<p><b>Anthony Stewart Head:</b> The man who helped Buffy Summers survive\n';
    msg = msg + etc...
    document.write (msg);
    // -->
    I haven't actually tried this, it's just an idea:
    You could redefine the document's 'write' method with one of your own - then you can parse out what you want from the msg variable and display it. For example:
    Code:
    <div id='myNewsElement'>
    <script type='text/javascript'>
    var nativeDocWrite = document.write; // Save a ref to native method
    document.write = myDocWrite;
    function myDocWrite(msg) {
      // parse msg and display abstract
    }
    </script>
    <script type='text/javascript' src='...news.js'>
    // Bring in the news feed.
    // When it calls document.write, myDocWrite will be called
    // instead of the native document.write() method.
    </script>
    <script type='text/javascript'>
    document.write = nativeDocWrite; // Restore native method
    </script>
    </div> <!-- end myNewsElement -->

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    florida
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll try... Sounds like a longshot?

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WB Pete
    ... Sounds like a longshot?
    It worked for me in Opera7 and IE6. It should work in any browser as long as document.write is not read-only.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    florida
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should've said... It's a longshot for me! I'm not proficient in js, i'm more of a designer. Can anyone create this js for me... I can send you WB Posters/T-shirt or something.

    The feed is from a 3rd party I have no document/server control.

  6. #6
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster
    I haven't actually tried this, it's just an idea:
    You could redefine the document's 'write' method with one of your own - then you can parse out what you want from the msg variable and display it.
    Mike,

    That's ingenious! I would of never thought of something like that
    Inside the function its only a matter of splitting up the string and
    outputting abstracts using document.writeln().

    WB Pete, I'll see what I can come up with. Unless Mike wants to have
    a go at displaying the snippets!

    -xDev

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    florida
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great! I have some movie DVD's for whomever too!

  8. #8
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How's this?

    HTML Code:
    <html>
    <head>
    <title>TVNow NewsFeed Parser</title>
    <style type="text/css"></style>
    </head>
    <body>
    <!-- START myNewsElement -->
    <div style="font: 9pt verdana, sans-serif;">
    <script type="text/javascript">
    var nativeDocWrite = document.write; // Save a ref to native method
    document.write = myDocWrite;
    function myDocWrite(msg) {
      // parse msg and display abstract
      var msg = msg.split("<p>"),
       block = new Array(),
       header = "<a href=\"http://www.tv-now.com/daily/news.htm\""+
    	" target=\"_new\">"+msg[1]+"</a>";
      for(var i = 0, idx = 2; i < msg.length - 2; i++, idx++)
      {
    	block[i] = msg[idx].split("\n");
      	block[i] = "<p>"+block[i][0]+" ... </p>";
      }
      document.writeln(header+block.join(""));
    }
    </script>
    <script type="text/javascript"
    	src="http://www.tv-now.com/js/news.js">
    // Bring in the news feed.
    // When it calls document.write, myDocWrite will be called
    // instead of the native document.write() method.
    </script>
    <script type="text/javascript">
    document.write = nativeDocWrite; // Restore native method
    </script>
    	<div style="font: 7.5pt verdana, sans-serif";>
    	<strong>Copyright 2003: TVNow Syndicated News
    	</strong>
    	<br />Your source for complete entertainment
    	<br />Visit
    	<a href="http://www.tv-now.com/"
    	target="_new">TVNow</a> soon.
    	</div>
    </div>
    <!-- END myNewsElement -->
    </body>
    </html>
    Short and sweet

    Take everything between the start and end html comments and put it anywhere within your page.

    -xDev
    Last edited by xDev; Nov 18, 2003 at 07:14.

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    florida
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! Your da man! Can i be so bold to ask for a slight tweak? Can you trim this down to 3-5 lines with a "br" instead of a "p"?

    I'm going to email about specs for gifts!

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    florida
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang it! I forgot to ask if you can leave out the "Celebrity News: Tuesday...", i'm going to create my own html page within my site that uses the full js feed, i will hardcode a link to the html page near the NewsFeed absract (your code)

    Can you also delete... "Copyright 2003: TVNow Syndicated News Your source for complete entertainment Visit TVNow soon."?

  11. #11
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WB Pete,

    How bout this?
    HTML Code:
    <!-- START myNewsElement -->
    <div style="font: 9pt verdana, sans-serif;">
    <script type="text/javascript">
    var nativeDocWrite = document.write; // Save a ref to native method
    document.write = myDocWrite;
    function myDocWrite(msg) {
      // parse msg and display abstract
      var msg = msg.split("<p>"),
       block = new Array(),
       dia = "- ";
      for(var i = 0, idx = 2; i < msg.length - 2; i++, idx++)
      {
    	block[i] = msg[idx].split("\n");
      	block[i] = "<br>"+dia+block[i][0]+" ... ";
      }
      var str = block[0]+
      	block[1]+block[2]+
      	block[3]+block[4];
      document.writeln(str);
    }
    </script>
    <script type="text/javascript"
    	src="http://www.tv-now.com/js/news.js">
    // Bring in the news feed.
    // When it calls document.write, myDocWrite will be called
    // instead of the native document.write() method.
    </script>
    <script type="text/javascript">
    document.write = nativeDocWrite; // Restore native method
    </script>
    </div>
    <!-- END myNewsElement -->
    5 lines, starting at the top.

    -xDev

  12. #12
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks xDev.

    Looks like you did the majority of the work tho. Great job!

    At first I was really stumped, because I knew he could not insert code between the assignment to msg and the call to write... but after I got this idea I realised... that's exactly what we did!

    this is way too much fun

  13. #13
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was a very cool idea Mike... never seen it done before. Makes sense though when I think about it, you can assign your own function to just about anything in js. Can be used on a number of feeds that do the same thing. The one's that don't link to an xml file but just give you a js file to link to. I seen a couple of places like this and now I have a method to chop it up, take away their (sometimes stupid) markup and display the info how I like!

    It's a good thing there's 2 methods (3 or 4 if using the DOM) to write in js as well

    Maybe WB Pete wouldn't mind sending the 2 of us a DVD I don't usually take anything for helping in the forms but ... The Matrix Reloaded is out on dvd and a WB movie isn't it??

    -xDev

  14. #14
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    florida
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is perfect! You are too savy!

    I can hook you both up! Of course anything WB is easy... We just recieved "Finding Nemo" DVD's too!

  15. #15
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xDev
    It was a very cool idea Mike...
    I have to admit, even tho I thought of it, it is pretty slick

    Quote Originally Posted by xDev
    It's a good thing there's 2 methods (3 or 4 if using the DOM) to write in js as well
    Yes, but don't forget that you can still call nativeDocWrite().

    Quote Originally Posted by xDev
    Maybe WB Pete wouldn't mind sending the 2 of us a DVD I don't usually take anything for helping in the forms but ... The Matrix Reloaded is out on dvd and a WB movie isn't it??
    Yep, I'm a Matrix nut too . I don't know a programmer who isn't

    WB Pete, a gift isn't required, we are glad to help you. But if you are so inclined )) then send a PM to xDev and I. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •