Skip to content

Commit d58b677

Browse files
committed
Added CDN. Added clearDataLocal. Added caching turn off switch.
1 parent d208c59 commit d58b677

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

ghrepocards.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
async function processGHRepoCards() {
1+
async function processGHRepoCards(ENABLE_CACHING) {
22

33
const GITHUB_API_SETTINGS_GHREPOCARDS = {
44
method: 'GET',
@@ -9,8 +9,29 @@ async function processGHRepoCards() {
99
};
1010

1111
const GITHUB_API_ENDPOINT_GHREPOCARDS = "https://api.github.com/";
12+
13+
// Null as arg to clear all.
14+
function clearDataLocal(repoName) {
15+
if (!ENABLE_CACHING) return;
16+
let l = localStorage.length;
17+
for (let i = 0; i < l; i++) {
18+
const key = localStorage.key(i);
19+
const value = localStorage.getItem(key);
20+
try {
21+
const parsedValue = JSON.parse(value);
22+
if (parsedValue && typeof parsedValue === 'object') {
23+
if (parsedValue.expiry && parsedValue.repoData && (repoName === null || repoName === parsedValue.repoData.name))
24+
localStorage.removeItem(key);
25+
}
26+
} catch (error) {
27+
localStorage.removeItem(key);
28+
throw new Error(`Error in clearDataLocal(): ${error}.`);
29+
}
30+
}
31+
}
1232

1333
function saveDataLocal(repoData) {
34+
if (!ENABLE_CACHING) return;
1435
const now = new Date();
1536
let expirationHours = 1;
1637
const item = {
@@ -23,6 +44,7 @@ async function processGHRepoCards() {
2344

2445
function getDataLocal() {
2546
const items = {};
47+
if (!ENABLE_CACHING) return items;
2648
let l = localStorage.length;
2749
for (let i = 0; i < l; i++) {
2850
const key = localStorage.key(i);
@@ -251,14 +273,14 @@ async function processGHRepoCards() {
251273
cardTag.style.display = "block";
252274
}
253275

276+
// Export functions to call it directly outside if needed.
254277
processGHRepoCards.saveDataLocal = saveDataLocal;
255278
processGHRepoCards.getDataLocal = getDataLocal;
256279
processGHRepoCards.putData = putData;
257280
processGHRepoCards.getData = getData;
258281
processGHRepoCards.getAllRepos = getAllRepos;
259282
processGHRepoCards.getSingleRepo = getSingleRepo;
283+
processGHRepoCards.clearDataLocal = clearDataLocal;
260284

261285
}
262286

263-
processGHRepoCards();
264-

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ <h3>Repo's data:</h3>
101101

102102
</body>
103103

104-
<script src="ghrepocards.js"></script>
104+
<script src="https://cdn.jsdelivr.net/gh/juliusnixi/github-repo-webcards/ghrepocards.js"></script>
105+
<script>
106+
processGHRepoCards(true);
107+
</script>
105108

106109
</html>
107110

0 commit comments

Comments
 (0)