Skip to content

Commit

Permalink
click to refresh quotable / 点击一言刷新
Browse files Browse the repository at this point in the history
  • Loading branch information
Cp0204 committed May 1, 2024
1 parent 8695e82 commit 2d86489
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/mod/add-widget-quotable/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>
<div class="columns is-mobile is-multiline pt-2">
<div class="column is-full pb-0">
<P id="quotable">${quotable}</P>
<P id="quotable" style="cursor: pointer;">${quotable}</P>
</div>
</div>
</div>
Expand All @@ -45,19 +45,19 @@
}
} catch (error) {
console.error('Failed to get Quotable', error);
return false
}
}

async function cacheQuotable() {
async function cacheQuotable(force = false) {
const cacheKey = 'quotable';
// 缓存过期时间:1小时
const cacheExpiry = 60 * 60 * 1000;
// 尝试从 localStorage 获取缓存数据
const cachedData = localStorage.getItem(cacheKey);
if (cachedData) {
if (cachedData && force == false) {
const { quotable, timestamp } = JSON.parse(cachedData);
const now = Date.now();
if (now - timestamp < cacheExpiry) {
if (Date.now() - timestamp < cacheExpiry) {
return quotable;
}
}
Expand All @@ -69,7 +69,11 @@
}));
return newQuotable;
}

document.querySelector('#quotable').innerHTML = await cacheQuotable();
document.querySelector('#quotable').addEventListener('click', async function () {
document.querySelector('#quotable').innerHTML = await cacheQuotable(true);
});
}


Expand Down

0 comments on commit 2d86489

Please # to comment.