From f71e22106e7f4a1fa47fce9ea82310da84eaadff Mon Sep 17 00:00:00 2001 From: shaoyaoqian <115222128+shaoyaoqian@users.noreply.github.com> Date: Thu, 1 Dec 2022 20:33:04 +0800 Subject: [PATCH] . (#197) * . Update weibo.js Update weibo.js Update weibo.js Update weibo.js Update weibo.js Update weibo.js * recover --- _config.yml | 1 + source/js/plugins/weibo.js | 116 +++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 source/js/plugins/weibo.js diff --git a/_config.yml b/_config.yml index fc70a91..78f4be6 100755 --- a/_config.yml +++ b/_config.yml @@ -259,6 +259,7 @@ plugins: timeline: /js/plugins/timeline.js linkcard: /js/plugins/linkcard.js fcircle: /js/plugins/fcircle.js + weibo: /js/plugins/weibo.js marked: https://cdn.bootcdn.net/ajax/libs/marked/4.0.18/marked.min.js diff --git a/source/js/plugins/weibo.js b/source/js/plugins/weibo.js new file mode 100644 index 0000000..aacb8fd --- /dev/null +++ b/source/js/plugins/weibo.js @@ -0,0 +1,116 @@ +const weibojs = { + requestAPI: (url, callback, timeout) => { + let retryTimes = 5; + function request() { + return new Promise((resolve, reject) => { + let status = 0; // 0 等待 1 完成 2 超时 + let timer = setTimeout(() => { + if (status === 0) { + status = 2; + timer = null; + reject('请求超时'); + if (retryTimes == 0) { + timeout(); + } + } + }, 5000); + fetch(url).then(function(response) { + if (status !== 2) { + clearTimeout(timer); + resolve(response); + timer = null; + status = 1; + } + if (response.ok) { + return response.json(); + } + throw new Error('Network response was not ok.'); + }).then(function(data) { + retryTimes = 0; + callback(data); + }).catch(function(error) { + if (retryTimes > 0) { + retryTimes -= 1; + setTimeout(() => { + request(); + }, 5000); + } else { + timeout(); + } + }); + }); + } + request(); + }, + layoutDiv: (cfg) => { + const el = $(cfg.el)[0]; + $(el).append('
'); + weibojs.requestAPI(cfg.api, function(data) { + $(el).find('.loading-wrap').remove(); + const arr = data.tweets || []; + const limit = el.getAttribute('limit'); + arr.forEach((item, i) => { + if (limit && i >= limit) { + return; + } + var cell = '
'; + cell += '
'; + cell += ''; + cell += '

' + item.created_at + '

'; + cell += '
'; + cell += '
'; + cell += ''; + cell += item.content; + cell += ''; + // cell += '
'; + // 每条微博的右下角 转发 评论 点赞 + cell += ''; + // 右下角结束 + $(el).append(cell); + }); + }, function() { + $(el).find('.loading-wrap svg').remove(); + $(el).find('.loading-wrap').append(''); + $(el).find('.loading-wrap').addClass('error'); + }); + }, +} + +$(function () { + const els = document.getElementsByClassName('stellar-weibo-api'); + for (var i = 0; i < els.length; i++) { + const el = els[i]; + const api = el.getAttribute('api'); // 这个API可以返回微博的json文件 + if (api == null) { + continue; + } + var cfg = new Object(); + cfg.el = el; + cfg.api = api; + cfg.avatar = 'https://fastly.jsdelivr.net/gh/cdn-x/placeholder@1.0.1/avatar/round/3442075.svg'; + weibojs.layoutDiv(cfg); + } +});