Advertisement
mspotilas

Posts in chronological order from feed, alternate method

Feb 4th, 2013
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- place this code on a page in your blog -->
  2. <script type="text/javascript">
  3.  
  4. // Posts in chronological order from feed / MS-potilas 2013 yabtb.blogspot.com
  5. //
  6. // Modified slightly in 2017 to make it work with the quirks of the current Blogger editor.
  7. // The other "chronological view from feed" script stopped working in December 2016 when google dropped their feed API.
  8. //
  9. // Configuration:
  10.  
  11. var maxResults = 500;  // 1-500 (500 is max possible)
  12. var numPerPage = 7;    // post per page, number or "all"
  13.  
  14. var feedURI = "/feeds/posts/default";
  15.  
  16. // full url could be used, too (read from another blog):
  17. // var feedURI = "http://yabtb.blogspot.com/feeds/posts/default";
  18.  
  19. var theResult = null;
  20.  
  21. function doThings() {
  22.     var result = theResult;
  23.     if (!result.error) {
  24.         var container = document.getElementById("feedDiv");
  25.         container.innerHTML = "";
  26.         var strBuffer= "";
  27.         var j = 0;
  28.         var base = parseInt(window.location.hash.replace("#", ""));
  29.         if(isNaN(base) || !base) base = 0;
  30.         if(window.location.hash == "#all" || window.location.hash == "all" || numPerPage == "all")
  31.             numPerPage = result.feed.entry.length;
  32.         for (var i = result.feed.entry.length-1-base; j < numPerPage && i >= 0; i--) {
  33.             var entry = result.feed.entry[i];
  34.             var posturl = "";
  35.                 for (var k = 0; k < entry.link.length;k++) {
  36.                 if (entry.link[k].rel == 'alternate') {
  37.                     posturl = entry.link[k].href;break;
  38.                 }
  39.             }
  40.             var datePart = entry.published.$t.match(/\d+/g); // assume ISO 8601
  41.             var postDate = new Date(datePart[0],datePart[1]-1,datePart[2],datePart[3],datePart[4],datePart[5]);
  42.             strBuffer = strBuffer + "<"+"h2 class=\"date-header\">" + postDate.toDateString() + "<"+"/h2>";
  43.             strBuffer = strBuffer + "<"+"h3 class=\"post-title entry-title\"><a href=\"" + posturl + "\">"+ entry.title.$t + "<"+"/a></"+"h3><"+"br />";
  44.             strBuffer = strBuffer + entry.content.$t + "<"+"div class=\"post-footer-line post-footer-line-1\"><div class=\"post-footer\">";
  45.             strBuffer = strBuffer + "Posted by " + entry.author[0].name.$t + " at " + postDate.getHours() + ":" + (postDate.getMinutes() < 10 ? "0":"") + postDate.getMinutes();
  46.             var cmtCnt = entry.thr$total ? entry.thr$total.$t : 0;
  47.             strBuffer = strBuffer + " &nbsp; &nbsp; <a href=\"" + posturl + "#comments\">"+cmtCnt+(cmtCnt==1 ? " comment" : " comments")+"<"+"/a>";
  48.             if(entry.category && entry.category.length) {
  49.                 strBuffer = strBuffer + "<"+""+"br />Labels: ";
  50.                 for(var z = 0 ; z < entry.category.length ; z++) {
  51.                     if(z) strBuffer = strBuffer + ", ";
  52.                     strBuffer = strBuffer + "<"+"a href=\"/search/label/" + encodeURIComponent(entry.category[z].term) + "\">" + entry.category[z].term + "<"+"/a>";
  53.                 }
  54.             }
  55.             strBuffer = strBuffer + "<"+"/div></"+"div><"+"br /><"+"br />";
  56.             j++;
  57.         }
  58.         strBuffer = strBuffer + "<"+"div style=\"margin-bottom: -1.5em; text-align: center\"><a onclick=\"setTimeout(doThings,500)\" href=\"#0\">First Post</a></"+"div>";
  59.         strBuffer = strBuffer + "<"+"div>";
  60.         if(i>0) strBuffer = strBuffer + "<"+"a onclick=\"setTimeout(doThings,500)\" href=\"#" + (base + numPerPage) + "\">Newer Posts</a>";
  61.         if(base >= numPerPage) strBuffer = strBuffer + "<"+"a style=\"float:right\" onclick=\"setTimeout(doThings,500)\" href=\"#" + (base-numPerPage) + "\">Older Posts</a> ";
  62.         strBuffer = strBuffer + "<"+"/div><"+"br />";
  63.         strBuffer = strBuffer.replace(/<img width=.1. height=.1. [^>]+>/g, "");
  64.         container.innerHTML = strBuffer;
  65.     }
  66.     window.scroll(0,0);
  67. }
  68. function feedRead(result) {
  69.     theResult = result;
  70.     doThings();
  71. }
  72. function loadFeed() {
  73.     var hd = document.getElementsByTagName('head')[0];
  74.     var sc = document.createElement('script');
  75.     sc.type = 'text/javascript';
  76.     sc.src = feedURI + "?max-results="+maxResults+"&redirect=false&alt=json-in-script&callback=feedRead"+ "&_hash="+Math.random();
  77.     hd.appendChild(sc);
  78.     // clear page title field:
  79.     var elements = document.getElementsByTagName("*");
  80.     for(var i=0 ; i<elements.length ; i++)
  81.         if(/(^| )post-title( |$)/.test(elements[i].className))
  82.             elements[i].parentNode.removeChild(elements[i]);
  83. }
  84. window.addEventListener ?
  85. window.addEventListener("load",loadFeed,false) :
  86. window.attachEvent && window.attachEvent("onload",loadFeed);
  87. </script>
  88. <div id="feedDiv"><i>Loading, please wait...</i></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement