3

I would like to show random posts to my blogger.

I got a javascript from googling and tried it, but the number of random posts are not correct (should be 10 but sometimes 4, sometimes 2, etc). I tried to check what's happening and found out that json.feed.entry [500] throws error.

Here is the javascript that I used

<script type="text/javascript">

function randomposts(json) {
  var randarray = new Array();
  var l=0;
  var flag;
  var numofpost=10;

  var total = parseInt(json.feed.openSearch$totalResults.$t,10);

  for(var i=0;i < numofpost;) {
    flag=0;
    randarray.length=numofpost;
    l=Math.floor(Math.random()*total);
    for(j in randarray){
      if(l==randarray[j]){
        flag=1;}
    }
    if(flag==0&&l!=0){
      randarray[i++]=l;
    }
  }
  // correct output
  // alert(randarray);

  document.write('<ul>');

  // dummy for testing 500 limit 
  //for (var x = 0; x < numofpost; x++) {
  //  randarray[x]= 495 + x;
  //}

  for(var n in randarray){
    var p=randarray[n];
    var entry=json.feed.entry[p-1];
    var posttitle = entry.title.$t;
    for(var k=0; k < entry.link.length; k++){
      if(entry.link[k].rel=='alternate'){
        document.write('<li> ' + posttitle.link(entry.link[k].href) + '</li>');

      }
    }
  }
  document.write('</ul>');
}
</script>
<script src="/feeds/posts/default?alt=json-in-script&start-index=1&max-results=1000&callback=randomposts" type="text/javascript"></script>

Currently I set var total = 500; so that the random works only for first 500 posts.

How to solve this issue?

UPDATE: I added try catch block and the error is TypeError: Cannot read property 'title' of undefined

UPDATE 2: The following picture is snapshot of console. The json.feed.entry 500 is undefined. snapshot of console

4

4 回答 4

2

所以看起来随机索引处的提要条目为空,在这种情况下添加另一个条件以确保随机索引中的每个条目都不为空。

代替:

if(flag==0&&l!=0){

和:

if(flag==0&&l!=0&&json.feed.entry[l-1]!=null){
  randarray[i++]=l;
}

我希望这能解决你的问题。

问候,

聚丙烯

编辑:然后您可以调用两次提要网址,这是实现此目的的快速而肮脏的方法:

<script type="text/javascript">
var a=0;
var b=0;
var entries = new Array();
function randomposts(json) {
  for (var i in json.feed.entry) {
    var entry = json.feed.entry[i];
    if (entry != null) {
        entries[b++] = entry;
    }
  }
  a++;
  if (a < 2) return;
  var randarray = new Array();
  var l=0;
  var flag;
  var numofpost=10;

  var total = entries.length;

  for(var i=0;i < numofpost;) {
    flag=0;
    randarray.length=numofpost;
    l=Math.floor(Math.random()*total);
    for(j in randarray){
      if(l==randarray[j]){
        flag=1;}
    }
    if(flag==0&&l!=0){
      randarray[i++]=l;
      //alert(l);
    }
  }
  // correct output
  // alert(randarray);

  document.write('<ul>');

  // dummy for testing 500 limit 
  //for (var x = 0; x < numofpost; x++) {
  //  randarray[x]= 495 + x;
  //}

  for(var n in randarray){
    var p=randarray[n];
    var entry=entries[p-1];
    var posttitle = entry.title.$t;
    for(var k=0; k < entry.link.length; k++){
      if(entry.link[k].rel=='alternate'){
        document.write('<li> ' + posttitle.link(entry.link[k].href) + '</li>');

      }
    }
  }
  document.write('</ul>');
}
</script>

<script src="/feeds/posts/default?alt=json-in-script&start-index=1&max-results=500&callback=randomposts" type="text/javascript"></script>
<script src="/feeds/posts/default?alt=json-in-script&start-index=501&max-results=500&callback=randomposts" type="text/javascript"></script>
于 2013-06-09T20:28:52.883 回答
1

当您将 设置max-results为 1000 时,从服务器返回的条目将被限制为 500。但是,它仍将返回正确的条目总数,如果您访问json.feed.openSearch$totalResults.$t.

资源

于 2013-06-09T15:12:27.327 回答
1

我建议使用Google JavaScript Client Library - Blogger API来检索博客的帖子。

请参见以下示例:

  <script>
  function renderResults(response) {
    if (response.items) {
      for (var i = 0; i < response.items.length; i++) {
        //do whatever you want with the posts of your blog
      }      
    }
    if(response.nextPageToken) {
      var blogId = 'XXX Your blogId XXX';
      var request = gapi.client.blogger.posts.list({
        'blogId': blogId,
        'pageToken': response.nextPageToken,
        'maxResults': 100,
      });
      request.execute(renderResults);
    }
  }
  function init() {
    gapi.client.setApiKey('XXX Get your API Key from https://code.google.com/apis/console XXX');
    gapi.client.load('blogger', 'v3', function() {
        var blogId = 'XXX Your blogId XXX';
        var request = gapi.client.blogger.posts.list({
          'blogId': blogId,
          'maxResults': 100,
        });
        request.execute(renderResults);        
    });
  }
  </script>
  <script src="https://apis.google.com/js/client.js?onload=init"></script>
于 2016-04-12T10:49:10.027 回答
0

Blogger 将每个请求的最大条目数限制为 500,尽管没有记录,但显然是这样。start-index如果您想获取所有条目,则必须通过块设置属性重新构建您的提要块: /feeds/posts/default?start-index=501. 在这里您可以找到更多信息:http ://too-clever-by-half.blogspot.ru/2011/12/blog-feed-500-post-limit-for-more-than.html

于 2013-06-16T08:49:03.560 回答