Twitter JSON URL, Hashtag & User Parser


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2.  
  3. String.prototype.parseURL = function() {
  4. return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(url) {
  5. return url.link(url);
  6. });
  7. };
  8.  
  9. String.prototype.parseUsername = function() {
  10. return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
  11. var username = u.replace("@","")
  12. return u.link("http://twitter.com/"+username);
  13. });
  14. };
  15.  
  16. String.prototype.parseHashtag = function() {
  17. return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {
  18. var tag = t.replace("#","%23")
  19. return t.link("http://search.twitter.com/search?q="+tag);
  20. });
  21. };
  22.  
  23. function twitterCallback2(obj) {
  24. var twitters = obj;
  25. var statusHTML = "";
  26. var username = "";
  27. for (var i=0; i<twitters.length; i++){
  28. username = twitters[i].user.screen_name;
  29. statusHTML += ('<li>'+twitters[i].text+'</li>');
  30. }
  31. document.getElementById('tweets').innerHTML = statusHTML.parseURL().parseUsername().parseHashtag();
  32. }
  33.  
  34. </script>
  35. <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/YOURUSERNAME.json?callback=twitterCallback2&count=10"></script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.