如何组合JSON代码以显示评论数

huangapple go评论112阅读模式
英文:

How to combine json code to show comment number

问题

最近我为我的博客或博客网站创建了一个简单的代码,用来显示作者的数据,比如作者的名字、URL、图片和他们发布的帖子数量。

然后我尝试显示作者的评论数量。通过这个URL https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&max-results=500&callback=handleJsonpData ,我们可以获取博客网站的所有评论。在这里,我们可以获取所有评论以及作者的姓名、评论消息等。

所以我尝试着显示作者的评论数量,并将其显示在他们的名字旁边。我编写了这段代码,但它没有起作用。我的博客网站有三位作者,其中任何一位都在网站上有一些评论。

我的代码分为两个部分。我想将两段代码组合起来,如果作者有任何评论,就显示作者的评论数量。

这是第一部分用于获取评论。在这里,我手动输入了一个名字,并检查了该名字是否有评论,并显示了评论数量。

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<div id="comment-count"></div>
<div id="iacman-comment-count"></div>

<script>
  function displayCommentCount(data) {
    var totalCommentCount = 0;
    var iacmanCommentCount = 0;
    if ('entry' in data.feed) {
      totalCommentCount = data.feed.entry.length;
      for (var i = 0; i < data.feed.entry.length; i++) {
        var authorName = data.feed.entry[i].author[0].name.$t;
        if (authorName === 'Iacman') {
          iacmanCommentCount++;
        }
      }
    }
    document.getElementById('comment-count').innerHTML = '<h2>Total Comments: ' + totalCommentCount + '</h2>';
    document.getElementById('iacman-comment-count').innerHTML = '<h2>Comments by Iacman: ' + iacmanCommentCount + '</h2>';
  }
  function handleJsonpData(data) {
    displayCommentCount(data);
  }
  var script = document.createElement('script');
  script.src = 'https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&amp;max-results=500&amp;callback=handleJsonpData';
  document.body.appendChild(script);
</script>

这是第二部分用于获取博客作者的详细信息

<link href='https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css' rel='stylesheet'/>
<script crossorigin='anonymous' src='https://kit.fontawesome.com/7dfc182d96.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<style>.author-image {cursor: pointer;}</style>

<div class="mb-0 mt-12">
  <h4 class="mb-0 text-black dark:text-gray-300"><i class="fa-solid fa-user-vneck-hair"></i> Authors and Writers </h4>
</div>
<div class="tbt_all_authors-list mt-0 mb-16 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5"></div>

<script>
  let feedURL = "https://tailwindbt.blogspot.com/feeds/posts/default?alt=json-in-script&amp;callback=?&amp;max-results=500";
  $.getJSON(feedURL, function(data) {
    let authors = [];
    $.each(data.feed.entry, function(index, entry) {
      if (entry.author) {
        let authorName = entry.author[0].name.$t;
        let authorImage = entry.author[0].gd$image.src;
        let authorProfileUrl = entry.author[0].uri.$t; // 提取作者的个人资料URL
        let authorExists = false;
        let authorIndex;
        $.each(authors, function(index, author) {
          if (author.name === authorName) {
            authorExists = true;
            authorIndex = index;
          }
        });
        if (authorExists) {
          authors[authorIndex].count++;
        } else {
          authors.push({ name: authorName, image: authorImage, count: 1, profileUrl: authorProfileUrl });
        }
      }
    });
    authors.sort(function(a, b) {
      return b.count - a.count;
    });
    $.each(authors, function(index, author) {
      let html = '<div class="flex bg-white dark:bg-gray-700 dark:text-gray-300 shadow rounded">' +
        '<a href="' + author.profileUrl + '" target="_blank" class="author-image flex items-start px-3 py-3">' +
        '<div class="w-20 h-20 rounded-full object-cover mr-4 shadow" style="background-image: url(' + author.image + '); background-repeat: no-repeat; background-position: center; background-size: cover;"></div>' +
        '<div class="">' +
        '<div class="flex items-center justify-between">' +
        '<div class="text-md font-semibold text-gray-900 dark:text-gray-300 mt-3">' + author.name + '</div>' +
        '</div>' +
        '<div class="text-gray-700 dark:text-gray-400">Posts: ' + author.count + ' </div>' +
        '</div>' +
        '</a>' +
        '</div>';
      $('.tbt_all_authors-list').append(html);
    });
  });
</script>

我想要将这两段代码合并,以查找作者的评论数量。如果作者有任何评论,它应该自动显示出来。我不知道如何将这两段代码合并,以显示作者的评论数量,如果作者有任何评论。有人可以帮助我找出解决方法吗?我已经尝试了很多方法,但都没有成功。

英文:

Recently I have created a simple code for my blogger or blogspot website to show Author's Data, like author's name with URL, image and their published Posts number.

Then I tried to show the comments number of the Author's. By this url https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&amp;max-results=500&amp;callback=handleJsonpData we can get a blogger websites all comments. Here we can get All comments with the Name, Comments Messages and many.

So what I tried, to show the number of comments of the Author's alongside their name. I have write this code, but It's not working. There are three author in my blog site, any one of them have some comments in the site.

There are two sectors of my code. I want to make a combination of both code to show comments number of the authors if the authors have any comment.

This is 1st part to get comments. Here I manually applied a name and checked if the name have comments, and show the number.

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;comment-count&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;iacman-comment-count&quot;&gt;&lt;/div&gt;
&lt;script&gt;
function displayCommentCount(data) {
var totalCommentCount = 0;
var iacmanCommentCount = 0;
if (&#39;entry&#39; in data.feed) {
totalCommentCount = data.feed.entry.length;
for (var i = 0; i &lt; data.feed.entry.length; i++) {
var authorName = data.feed.entry[i].author[0].name.$t;
if (authorName === &#39;Iacman&#39;) {
iacmanCommentCount++;
}
}
}
document.getElementById(&#39;comment-count&#39;).innerHTML = &#39;&lt;h2&gt;Total Comments: &#39; + totalCommentCount + &#39;&lt;/h2&gt;&#39;;
document.getElementById(&#39;iacman-comment-count&#39;).innerHTML = &#39;&lt;h2&gt;Comments by Iacman: &#39; + iacmanCommentCount + &#39;&lt;/h2&gt;&#39;;
}
function handleJsonpData(data) {
displayCommentCount(data);
}
var script = document.createElement(&#39;script&#39;);
script.src = &#39;https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&amp;max-results=500&amp;callback=handleJsonpData&#39;;
document.body.appendChild(script);
&lt;/script&gt;

This is the second part to get The Blog Author's Details.

&lt;link href=&#39;https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css&#39; rel=&#39;stylesheet&#39;/&gt;
&lt;script crossorigin=&#39;anonymous&#39; src=&#39;https://kit.fontawesome.com/7dfc182d96.js&#39;&gt;&lt;/script&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;style&gt;.author-image {cursor: pointer;}&lt;/style&gt;
&lt;div class=&quot;mb-0 mt-12&quot;&gt;
&lt;h4 class=&quot;mb-0 text-black dark:text-gray-300&quot;&gt;&lt;i class=&quot;fa-solid fa-user-vneck-hair&quot;&gt;&lt;/i&gt; Authors and Writers &lt;/h4&gt;
&lt;/div&gt;
&lt;div class=&quot;tbt_all_authors-list mt-0 mb-16 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5&quot;&gt;&lt;/div&gt;
&lt;script&gt;
let feedURL = &quot;https://tailwindbt.blogspot.com/feeds/posts/default?alt=json-in-script&amp;callback=?&amp;max-results=500&quot;;
$.getJSON(feedURL, function(data) {
let authors = [];
$.each(data.feed.entry, function(index, entry) {
if (entry.author) {
let authorName = entry.author[0].name.$t;
let authorImage = entry.author[0].gd$image.src;
let authorProfileUrl = entry.author[0].uri.$t; // Extract the author profile URL
let authorExists = false;
let authorIndex;
$.each(authors, function(index, author) {
if (author.name === authorName) {
authorExists = true;
authorIndex = index;
}
});
if (authorExists) {
authors[authorIndex].count++;
} else {
authors.push({ name: authorName, image: authorImage, count: 1, profileUrl: authorProfileUrl });
}
}
});
authors.sort(function(a, b) {
return b.count - a.count;
});
$.each(authors, function(index, author) {
let html = &#39;&lt;div class=&quot;flex bg-white dark:bg-gray-700 dark:text-gray-300 shadow rounded&quot;&gt;&#39; +
&#39;&lt;a href=&quot;&#39; + author.profileUrl + &#39;&quot; target=&quot;_blank&quot; class=&quot;author-image flex items-start px-3 py-3&quot;&gt;&#39; +
&#39;&lt;div class=&quot;w-20 h-20 rounded-full object-cover mr-4 shadow&quot; style=&quot;background-image: url(&#39; + author.image + &#39;); background-repeat: no-repeat; background-position: center; background-size: cover;&quot;&gt;&lt;/div&gt;&#39; +
&#39;&lt;div class=&quot;&quot;&gt;&#39; +
&#39;&lt;div class=&quot;flex items-center justify-between&quot;&gt;&#39; +
&#39;&lt;div class=&quot;text-md font-semibold text-gray-900 dark:text-gray-300 mt-3&quot;&gt;&#39; + author.name + &#39;&lt;/div&gt;&#39; +
&#39;&lt;/div&gt;&#39; +
&#39;&lt;div class=&quot;text-gray-700 dark:text-gray-400&quot;&gt;Posts: &#39; + author.count + &#39; &lt;/div&gt;&#39; +
&#39;&lt;/div&gt;&#39; +
&#39;&lt;/a&gt;&#39; +
&#39;&lt;/div&gt;&#39;;
$(&#39;.tbt_all_authors-list&#39;).append(html);
});
});
&lt;/script&gt;

I want to combine both codes to find the comment number of the Author's. If Authors have any comment, it should show the number automatically. I don't know how to combine both codes to show the comments number of author if Authors have any comment. Somebody please help me to find out. I tried in many ways, but not working.

答案1

得分: 2

你的代码中主要问题是在displayCommentCount函数中尝试访问author.name,但是在该函数的作用域中未定义author。author是在$.getJSON回调函数中定义的,并且不是全局可访问的。

要解决这个问题,你需要更新displayCommentCount函数,将authorName作为参数传递进去,然后将其与每个评论的作者名称进行比较。

以下是你代码的已更正版本:

<link href='https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css' rel='stylesheet'/>
<script crossorigin='anonymous' src='https://kit.fontawesome.com/7dfc182d96.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style>.author-image {cursor: pointer;}</style>

<div class="mb-0 mt-12">
  <h4 class="mb-0 text-black dark:text-gray-300"><i class="fa-solid fa-user-vneck-hair"></i> Authors and Writers </h4>
</div>
<div class="tbt_all_authors-list mt-0 mb-16 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5"></div>

<script>
  var commentData;

  function getCommentCount(authorName) {
    var sfwCommentCount = 0;
    if ('entry' in commentData.feed) {
      for (var i = 0; i < commentData.feed.entry.length; i++) {
        var commentAuthorName = commentData.feed.entry[i].author[0].name.$t;
        if (commentAuthorName === authorName) {
          sfwCommentCount++;
        }
      }
    }
    return sfwCommentCount;
  }

  function handleJsonpData(data) {
    commentData = data;
  }

  var script = document.createElement('script');
  script.src = 'https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&amp;max-results=500&amp;callback=handleJsonpData';
  document.body.appendChild(script);

  let feedURL = "https://tailwindbt.blogspot.com/feeds/posts/default?alt=json-in-script&amp;callback=?&amp;max-results=500";
  $.getJSON(feedURL, function(data) {
    let authors = [];
    $.each(data.feed.entry, function(index, entry) {
      if (entry.author) {
        let authorName = entry.author[0].name.$t;
        let authorImage = entry.author[0].gd$image.src;
        let authorAbout = '';
        if (entry.author[0].gd$about) {
          authorAbout = entry.author[0].gd$about.$t;
        }
        let authorProfileUrl = entry.author[0].uri.$t; // Extract the author profile URL
        let authorExists = false;
        let authorIndex;
        $.each(authors, function(index, author) {
          if (author.name === authorName) {
            authorExists = true;
            authorIndex = index;
          }
        });
        if (authorExists) {
          authors[authorIndex].count++;
        } else {
          authors.push({ name: authorName, image: authorImage, count: 1, about: authorAbout, profileUrl: authorProfileUrl });
        }
      }
    });

    authors.sort(function(a, b) {
      return b.count - a.count;
    });

    $.each(authors, function(index, author) {
      let authorCommentCount = getCommentCount(author.name);
      let html = '<div class="flex bg-white dark:bg-gray-700 dark:text-gray-300 shadow rounded">' +
        '<a href="' + author.profileUrl + '" target="_blank" class="author-image flex items-start px-3 py-3">' +
        '<div class="w-20 h-20 rounded-full object-cover mr-4 shadow" style="background-image: url(' + author.image + '); background-repeat: no-repeat; background-position: center; background-size: cover;"></div>' +
        '<div class="">' +
        '<div class="flex items-center justify-between">' +
        '<div class="text-md font-semibold text-gray-900 dark:text-gray-300 mt-3">' + author.name + '</div>' +
        '</div>' +
        '<div class="text-gray-700 dark:text-gray-400">Posts: ' + author.count + ' </div>' +
        '<div class="text-gray-700 dark:text-gray-400">Comments: ' + authorCommentCount + ' </div>' +
        '</div>' +
        '</a>' +
        '</div>';

      $('.tbt_all_authors-list').append(html);
    });
  });
</script>

希望这可以帮助你解决问题!

英文:

The main problem in your code is that you're trying to access author.name in the displayCommentCount function, but author is not defined within the scope of that function. author is defined in the $.getJSON callback function and it is not globally accessible.

To fix the issue, you need to update your displayCommentCount function to accept authorName as a parameter, then compare it with each comment author's name.

Here's the corrected version of your code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link href=&#39;https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css&#39; rel=&#39;stylesheet&#39;/&gt;
&lt;script crossorigin=&#39;anonymous&#39; src=&#39;https://kit.fontawesome.com/7dfc182d96.js&#39;&gt;&lt;/script&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;style&gt;.author-image {cursor: pointer;}&lt;/style&gt;
&lt;div class=&quot;mb-0 mt-12&quot;&gt;
&lt;h4 class=&quot;mb-0 text-black dark:text-gray-300&quot;&gt;&lt;i class=&quot;fa-solid fa-user-vneck-hair&quot;&gt;&lt;/i&gt; Authors and Writers &lt;/h4&gt;
&lt;/div&gt;
&lt;div class=&quot;tbt_all_authors-list mt-0 mb-16 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5&quot;&gt;&lt;/div&gt;
&lt;script&gt;
var commentData;
function getCommentCount(authorName) {
var sfwCommentCount = 0;
if (&#39;entry&#39; in commentData.feed) {
for (var i = 0; i &lt; commentData.feed.entry.length; i++) {
var commentAuthorName = commentData.feed.entry[i].author[0].name.$t;
if (commentAuthorName === authorName) {
sfwCommentCount++;
}
}
}
return sfwCommentCount;
}
function handleJsonpData(data) {
commentData = data;
}
var script = document.createElement(&#39;script&#39;);
script.src = &#39;https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&amp;max-results=500&amp;callback=handleJsonpData&#39;;
document.body.appendChild(script);
let feedURL = &quot;https://tailwindbt.blogspot.com/feeds/posts/default?alt=json-in-script&amp;callback=?&amp;max-results=500&quot;;
$.getJSON(feedURL, function(data) {
let authors = [];
$.each(data.feed.entry, function(index, entry) {
if (entry.author) {
let authorName = entry.author[0].name.$t;
let authorImage = entry.author[0].gd$image.src;
let authorAbout = &#39;&#39;;
if (entry.author[0].gd$about) {
authorAbout = entry.author[0].gd$about.$t;
}
let authorProfileUrl = entry.author[0].uri.$t; // Extract the author profile URL
let authorExists = false;
let authorIndex;
$.each(authors, function(index, author) {
if (author.name === authorName) {
authorExists = true;
authorIndex = index;
}
});
if (authorExists) {
authors[authorIndex].count++;
} else {
authors.push({ name: authorName, image: authorImage, count: 1, about: authorAbout, profileUrl: authorProfileUrl });
}
}
});
authors.sort(function(a, b) {
return b.count - a.count;
});
$.each(authors, function(index, author) {
let authorCommentCount = getCommentCount(author.name);
let html = &#39;&lt;div class=&quot;flex bg-white dark:bg-gray-700 dark:text-gray-300 shadow rounded&quot;&gt;&#39; +
&#39;&lt;a href=&quot;&#39; + author.profileUrl + &#39;&quot; target=&quot;_blank&quot; class=&quot;author-image flex items-start px-3 py-3&quot;&gt;&#39; +
&#39;&lt;div class=&quot;w-20 h-20 rounded-full object-cover mr-4 shadow&quot; style=&quot;background-image: url(&#39; + author.image + &#39;); background-repeat: no-repeat; background-position: center; background-size: cover;&quot;&gt;&lt;/div&gt;&#39; +
&#39;&lt;div class=&quot;&quot;&gt;&#39; +
&#39;&lt;div class=&quot;flex items-center justify-between&quot;&gt;&#39; +
&#39;&lt;div class=&quot;text-md font-semibold text-gray-900 dark:text-gray-300 mt-3&quot;&gt;&#39; + author.name + &#39;&lt;/div&gt;&#39; +
&#39;&lt;/div&gt;&#39; +
&#39;&lt;div class=&quot;text-gray-700 dark:text-gray-400&quot;&gt;Posts: &#39; + author.count + &#39; &lt;/div&gt;&#39; +
&#39;&lt;div class=&quot;text-gray-700 dark:text-gray-400&quot;&gt;Comments: &#39; + authorCommentCount + &#39; &lt;/div&gt;&#39; +
&#39;&lt;/div&gt;&#39; +
&#39;&lt;/a&gt;&#39; +
&#39;&lt;/div&gt;&#39;;
$(&#39;.tbt_all_authors-list&#39;).append(html);
});
});
&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 0

你在HTML元素中使用了sfwCommentCount变量,但超出了其作用域,这就是为什么它不起作用的原因。

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-html -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet" />
    <script crossorigin="anonymous" src="https://kit.fontawesome.com/7dfc182d96.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <style>
        .author-image {
            cursor: pointer;
        }
    </style>

    <div class="mb-0 mt-12">
        <h4 class="mb-0 text-black dark:text-gray-300"><i class="fa-solid fa-user-vneck-hair"></i> Authors and Writers</h4>
    </div>
    <div
        class="tbt_all_authors-list mt-0 mb-16 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5"></div>

    <script>
        let feedURL = "https://tailwindbt.blogspot.com/feeds/posts/default?alt=json-in-script&callback=?&max-results=10";
        let authors = [];
        $.getJSON(feedURL, function (data) {
            $.each(data.feed.entry, function (index, entry) {
                if (entry.author) {
                    let authorName = entry.author[0].name.$t;
                    let authorImage = entry.author[0].gd$image.src;
                    let authorAbout = "";
                    if (entry.author[0].gd$about) {
                        authorAbout = entry.author[0].gd$about.$t;
                    }
                    let authorProfileUrl = entry.author[0].uri.$t; // 提取作者个人资料链接
                    let authorExists = false;
                    let authorIndex;
                    $.each(authors, function (index, author) {
                        if (author.name === authorName) {
                            authorExists = true;
                            authorIndex = index;
                        }
                    });
                    if (authorExists) {
                        authors[authorIndex].count++;
                    } else {
                        authors.push({
                            name: authorName,
                            image: authorImage,
                            count: 1,
                            about: authorAbout,
                            profileUrl: authorProfileUrl,
                            comment: 0,
                        });
                    }
                }
            });

            authors.sort(function (a, b) {
                return b.count - a.count;
            });

            $.each(authors, function (index, author) {
                let html =
                    '<div class="flex bg-white dark:bg-gray-700 dark:text-gray-300 shadow rounded">' +
                    '<a href="' +
                    author.profileUrl +
                    '" target="_blank" class="author-image flex items-start px-3 py-3">' +
                    '<div class="w-20 h-20 rounded-full object-cover mr-4 shadow" style="background-image: url(' +
                    author.image +
                    '); background-repeat: no-repeat; background-position: center; background-size: cover;"></div>' +
                    '<div class="">' +
                    '<div class="flex items-center justify-between">' +
                    '<div class="text-md font-semibold text-gray-900 dark:text-gray-300 mt-3">' +
                    author.name +
                    "</div>" +
                    "</div>" +
                    '<div class="text-gray-700 dark:text-gray-400">Posts: ' +
                    author.count +
                    " </div>" +
                    '<div class="text-gray-700 dark:text-gray-400" id="' + author.name + '-comment">Comments: ' +
                    author.comment +
                    " </div>" +
                    "</div>" +
                    "</a>" +
                    "</div>";

                $(".tbt_all_authors-list").append(html);
            });
        });

        function displayCommentCount(data) {
            if ("entry" in data.feed) {
                totalCommentCount = data.feed.entry.length;
                for (var i = 0; i < data.feed.entry.length; i++) {
                    var authorName = data.feed.entry[i].author[0].name.$t;
                    console.log(authors.filter((author) => author.name === authorName).length > 0);

                    if (authors.filter((author) => author.name === authorName).length > 0) {
                        authors.forEach((author) => {
                            if (author.name === authorName) author.comment++;
                        });
                    }
                }
            }
            authors.forEach((author) => {
                document.getElementById(`${author.name}-comment`).innerText = `Comments: ${author.comment}`;
            });
        }

        function handleJsonpData(data) {
            displayCommentCount(data);
        }
        var script = document.createElement("script");
        script.src =
            "https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&max-results=10&callback=handleJsonpData";
        document.body.appendChild(script);
    </script>

<!-- 结束代码片段 -->
英文:

You somehow used sfwCommentCount variable in html element that was out of that scope, that's why its not working

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link href=&quot;https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script crossorigin=&quot;anonymous&quot; src=&quot;https://kit.fontawesome.com/7dfc182d96.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;style&gt;
.author-image {
cursor: pointer;
}
&lt;/style&gt;
&lt;div class=&quot;mb-0 mt-12&quot;&gt;
&lt;h4 class=&quot;mb-0 text-black dark:text-gray-300&quot;&gt;&lt;i class=&quot;fa-solid fa-user-vneck-hair&quot;&gt;&lt;/i&gt; Authors and Writers&lt;/h4&gt;
&lt;/div&gt;
&lt;div
class=&quot;tbt_all_authors-list mt-0 mb-16 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5&quot;&gt;&lt;/div&gt;
&lt;script&gt;
let feedURL = &quot;https://tailwindbt.blogspot.com/feeds/posts/default?alt=json-in-script&amp;callback=?&amp;max-results=10&quot;;
let authors = [];
$.getJSON(feedURL, function (data) {
$.each(data.feed.entry, function (index, entry) {
if (entry.author) {
let authorName = entry.author[0].name.$t;
let authorImage = entry.author[0].gd$image.src;
let authorAbout = &quot;&quot;;
if (entry.author[0].gd$about) {
authorAbout = entry.author[0].gd$about.$t;
}
let authorProfileUrl = entry.author[0].uri.$t; // Extract the author profile URL
let authorExists = false;
let authorIndex;
$.each(authors, function (index, author) {
if (author.name === authorName) {
authorExists = true;
authorIndex = index;
}
});
if (authorExists) {
authors[authorIndex].count++;
} else {
authors.push({
name: authorName,
image: authorImage,
count: 1,
about: authorAbout,
profileUrl: authorProfileUrl,
comment: 0,
});
}
}
});
authors.sort(function (a, b) {
return b.count - a.count;
});
$.each(authors, function (index, author) {
let html =
&#39;&lt;div class=&quot;flex bg-white dark:bg-gray-700 dark:text-gray-300 shadow rounded&quot;&gt;&#39; +
&#39;&lt;a href=&quot;&#39; +
author.profileUrl +
&#39;&quot; target=&quot;_blank&quot; class=&quot;author-image flex items-start px-3 py-3&quot;&gt;&#39; +
&#39;&lt;div class=&quot;w-20 h-20 rounded-full object-cover mr-4 shadow&quot; style=&quot;background-image: url(&#39; +
author.image +
&#39;); background-repeat: no-repeat; background-position: center; background-size: cover;&quot;&gt;&lt;/div&gt;&#39; +
&#39;&lt;div class=&quot;&quot;&gt;&#39; +
&#39;&lt;div class=&quot;flex items-center justify-between&quot;&gt;&#39; +
&#39;&lt;div class=&quot;text-md font-semibold text-gray-900 dark:text-gray-300 mt-3&quot;&gt;&#39; +
author.name +
&quot;&lt;/div&gt;&quot; +
&quot;&lt;/div&gt;&quot; +
&#39;&lt;div class=&quot;text-gray-700 dark:text-gray-400&quot;&gt;Posts: &#39; +
author.count +
&quot; &lt;/div&gt;&quot; +
`&lt;div class=&quot;text-gray-700 dark:text-gray-400&quot; id=&quot;${author.name}-comment&quot;&gt;Comments: ` +
author.comment +
&quot; &lt;/div&gt;&quot; +
&quot;&lt;/div&gt;&quot; +
&quot;&lt;/a&gt;&quot; +
&quot;&lt;/div&gt;&quot;;
$(&quot;.tbt_all_authors-list&quot;).append(html);
});
});
function displayCommentCount(data) {
if (&quot;entry&quot; in data.feed) {
totalCommentCount = data.feed.entry.length;
for (var i = 0; i &lt; data.feed.entry.length; i++) {
var authorName = data.feed.entry[i].author[0].name.$t;
console.log(authors.filter((author) =&gt; author.name === authorName).length &gt; 0);
if (authors.filter((author) =&gt; author.name === authorName).length &gt; 0) {
authors.forEach((author) =&gt; {
if (author.name === authorName) author.comment++;
});
}
}
}
authors.forEach((author) =&gt; {
// $(`#${author.name}-comment`)[0].innerText =`Comments: ${author.comment}`;
document.getElementById(`${author.name}-comment`).innerText = `Comments: ${author.comment}`;
});
}
function handleJsonpData(data) {
displayCommentCount(data);
}
var script = document.createElement(&quot;script&quot;);
script.src =
&quot;https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&amp;max-results=10&amp;callback=handleJsonpData&quot;;
document.body.appendChild(script);
&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月4日 02:07:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定