2020-07-01

Blogger:投稿一覧を表示する(更新順、ラベルごと)

Bloggerから投稿一覧情報を取得して、各ページにリンクを貼った目次を作成する。
取得できる情報は今のところ以下の通り。
 タイトル
 リンクのアドレス
 作成日
 更新日
 ラベル

以下のコードをBloggerのHTML編集モードで貼りつける。
貼りつけた後、編集すると不具合が発生する場合があるので、編集する場合は別のエディタで編集してから貼りつけて、そのまま公開ボタンをクリックする。
以下の例では、ラベルに"javascript"がついた投稿を作成日順にソートして表示する。

コード

  1. <!-- これを全部コピーして貼り付けること。bloggerのhtmlエディタで編集するとエラーが出る -->
  2. <div style='margin:0.5rem;'>
  3. <a href="https://s104bntmp.blogspot.com/2015/10/javascript-javascript.html">javascript とは</a>
  4. </div>
  5. <div style='margin:0.5rem;'>
  6. <a href="https://s104bntmp.blogspot.com/2020/04/javascript-jquery.html">基本形(jQueryを用いたスクリプトの基本形)</a>
  7. </div>
  8. <div style='margin:0.5rem;'>
  9. <a href="https://s104bntmp.blogspot.com/2013/10/javascripttips.html">TIPS</a>
  10. </div>
  11. <div style='margin:0.5rem;'>
  12. <a href="http://s104bntmp.blogspot.jp/2013/10/jquerytips.html">jQuery:TIPS</a>
  13. </div>
  14. <script type="text/javascript">
  15.  
  16. function loadtoc(data) {
  17.  
  18. let entry = [];
  19.  
  20. for (i = 0; i < data.feed.entry.length; i++) {
  21. // ラベルの探索
  22. let label = [];
  23. if (data.feed.entry[i].category) {
  24. for (let j=0; j<data.feed.entry[i].category.length; j++) {
  25. label.push(data.feed.entry[i].category[j].term);
  26. }
  27. }
  28. // リンク先の探索
  29. let href;
  30. for (j = 0; j < data.feed.entry[i].link.length; j++) {
  31. if (data.feed.entry[i].link[j].title == data.feed.entry[i].title.$t) {
  32. href = data.feed.entry[i].link[j].href
  33. }
  34. }
  35.  
  36. entry[i] = {
  37. title: data.feed.entry[i].title.$t + " (" + data.feed.entry[i].published.$t.slice( 0, 10 ) + " 作成)",
  38. href: href,
  39. label: label,
  40. published: data.feed.entry[i].published.$t,
  41. updated: data.feed.entry[i].updated.$t,
  42. summary: data.feed.entry[i].summary.$t
  43. };
  44. }
  45.  
  46. // 並べ替え(作成順)
  47. entry.sort(function(a,b) {
  48. return (a.published < b.published ? 1 : -1);
  49. });
  50.  
  51. for (i = 0; i < data.feed.entry.length; i++) {
  52. if ( entry[i].label.includes("javascript") ) {
  53. document.writeln("<div style='margin:0.5rem;'><a href=\"" + entry[i].href + "\">" +
  54. escapeHTML(entry[i].title) + "</a><br /></div>");
  55. }
  56. }
  57.  
  58. // テキストをエスケープ処理する
  59. function escapeHTML(html) {
  60.  
  61. var div = document.createElement("div");
  62. if (div.innerText !== void 0) div.innerText = html; // innerText が定義されていれば innerText へ設定
  63. else div.textContent = html; // Firefox のように innerText がないブラウザ向け
  64.  
  65. return div.innerHTML;
  66. }
  67. }
  68.  
  69. </script>
  70. <script type="text/javascript" src="https://s104bntmp.blogspot.com/feeds/posts/summary?alt=json-in-script&callback=loadtoc&max-results=500&redirect=false"></script>

以下の部分で投稿情報を取得して、loadtocにコールバックしている。
"s104bntmp.blogspot.com"を自分のブログに変更してください。
scriptタグの前のdivタグは関係ない(このようにhtmlと組み合わせることも可能)。
  1. <script type="text/javascript" src="https://s104bntmp.blogspot.com/feeds/posts/summary?alt=json-in-script&callback=loadtoc&max-results=500&redirect=false"></script>

参考:
Blogger:投稿一覧を作成する-最もシンプル
Blogger の フィード URL 構成 と パラメタ― まとめ


以上