友链数据缓存

This commit is contained in:
1152958806@qq.com 2023-09-27 13:44:45 +08:00
parent 0b390a54b7
commit 99e99cbedc
2 changed files with 77 additions and 49 deletions

View File

@ -73,14 +73,19 @@ var heo = {
footerRandomFriendsBtn.style.opacity = "0.2"; footerRandomFriendsBtn.style.opacity = "0.2";
footerRandomFriendsBtn.style.transitionDuration = "0.3s"; footerRandomFriendsBtn.style.transitionDuration = "0.3s";
footerRandomFriendsBtn.style.transform = "rotate(" + 360 * refreshNum++ + "deg)"; footerRandomFriendsBtn.style.transform = "rotate(" + 360 * refreshNum++ + "deg)";
function getLinks(){
const fetchUrl = "/apis/api.plugin.halo.run/v1alpha1/plugins/PluginLinks/links?keyword=&sort=priority,asc" const fetchUrl = "/apis/api.plugin.halo.run/v1alpha1/plugins/PluginLinks/links?keyword=&sort=priority,asc"
const linksUrl = GLOBAL_CONFIG.source.links.linksUrl
const num = GLOBAL_CONFIG.source.links.linksNum
fetch(fetchUrl) fetch(fetchUrl)
.then(res => res.json()) .then(res => res.json())
.then(json => { .then(json => {
var randomFriendLinks = getArrayItems(json.items, num); saveToLocal.set('links-data', JSON.stringify(json.items), 10 / (60 * 24))
renderer(json.items);
})
}
function renderer(data){
const linksUrl = GLOBAL_CONFIG.source.links.linksUrl
const num = GLOBAL_CONFIG.source.links.linksNum
var randomFriendLinks = getArrayItems(data, num);
var htmlText = ''; var htmlText = '';
for (let i = 0; i < randomFriendLinks.length; ++i) { for (let i = 0; i < randomFriendLinks.length; ++i) {
var item = randomFriendLinks[i] var item = randomFriendLinks[i]
@ -90,10 +95,19 @@ var heo = {
if(document.getElementById("friend-links-in-footer")){ if(document.getElementById("friend-links-in-footer")){
document.getElementById("friend-links-in-footer").innerHTML = htmlText; document.getElementById("friend-links-in-footer").innerHTML = htmlText;
} }
}) }
function friendLinksInFooterInit(){
const data = saveToLocal.get('links-data')
if (data) {
renderer(JSON.parse(data))
} else {
getLinks()
}
setTimeout(()=>{ setTimeout(()=>{
footerRandomFriendsBtn.style.opacity = "1"; footerRandomFriendsBtn.style.opacity = "1";
}, 300) }, 300)
}
friendLinksInFooterInit();
}, },
//禁止图片右键单击 //禁止图片右键单击

View File

@ -367,14 +367,20 @@ document.addEventListener('scroll', btf.throttle(function () {
//友链随机传送 //友链随机传送
function travelling() { function travelling() {
function getLinks() {
const links = "/apis/api.plugin.halo.run/v1alpha1/plugins/PluginLinks/links?keyword=&sort=priority,asc" const links = "/apis/api.plugin.halo.run/v1alpha1/plugins/PluginLinks/links?keyword=&sort=priority,asc"
fetch(links) fetch(links)
.then(res => res.json()) .then(res => res.json())
.then(json => { .then(json => {
var linksData = json.items saveToLocal.set('links-data', JSON.stringify(json.items), 10 / (60 * 24))
renderer(json.items);
})
}
function renderer(data){
var linksData = data
var name = '' var name = ''
var link = '' var link = ''
if(linksData.length>0){ if (linksData.length > 0) {
var randomFriendLinks = getArrayItems(linksData, 1); var randomFriendLinks = getArrayItems(linksData, 1);
name = randomFriendLinks[0].spec.displayName; name = randomFriendLinks[0].spec.displayName;
link = randomFriendLinks[0].spec.url; link = randomFriendLinks[0].spec.url;
@ -390,12 +396,20 @@ function travelling() {
pos: 'top-center', pos: 'top-center',
actionText: '前往', actionText: '前往',
onActionClick: function (element) { onActionClick: function (element) {
//Set opacity of element to 0 to close Snackbar
$(element).css('opacity', 0); $(element).css('opacity', 0);
window.open(link, '_blank'); window.open(link, '_blank');
} }
}); });
}) }
function init(){
const data = saveToLocal.get('links-data')
if (data) {
renderer(JSON.parse(data))
} else {
getLinks()
}
}
init()
} }
//前往黑洞 //前往黑洞