Skip to content

Commit

Permalink
fix NaNundefined and remove unnecessary caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-kokabi committed Oct 3, 2023
1 parent fd5be65 commit abacccc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 76 deletions.
39 changes: 3 additions & 36 deletions src/chrome/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,9 @@ async function inject(selector, githubToken) {
}

async function injectStars(link) {
let stars;
try {
stars = await getStars(link.href);
const stars = await getStars(link.href);
if (!stars) return;

} catch (error) {
return;
}
const strong = document.createElement("strong");
strong.id = "github-stars-14151312";
strong.setAttribute("stars", stars);
Expand All @@ -151,22 +147,13 @@ async function inject(selector, githubToken) {
async function getStars(githubRepoURL) {
const repoName = githubRepoURL.match(/github\.com\/([^/]+\/[^/]+)/)[1];

const cache = await chrome.storage.local.get(repoName);
const repoStars = cache?.[repoName]?.stars;

if (repoStars) {
return repoStars;
}

const response = await fetch(`https://api.github.com/repos/${repoName}`, {
headers: { Authorization: `Token ${githubToken}` },
});

const data = await response.json();
const stars = data.stargazers_count;

await chrome.storage.local.set({ [repoName]: { stars, time: new Date().getTime() } });

return stars;
}

Expand All @@ -183,24 +170,4 @@ async function inject(selector, githubToken) {
return `${formattedNumber}${suffixes[suffixIndex]}`;
}

}

chrome.runtime.onInstalled.addListener(async (details) => {
await chrome.alarms.create("clear-expired-stars", {
periodInMinutes: 60
});
});

chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === "clear-expired-stars") {
const allSavedData = await chrome.storage.local.get();
for (let key in allSavedData) {
const time = allSavedData[key].time;

const currentTime = new Date().getTime();
if (currentTime - time < 24 * 60 * 60 * 1000) return

await chrome.storage.local.remove(key);
}
}
});
}
4 changes: 2 additions & 2 deletions src/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Github Sorter",
"short_name": "GithubSorter",
"version": "1.0.0",
"version": "1.0.1",
"description": "Sort github repos by stars",
"author": "Ayub Kokabi",
"homepage_url": "https://github.com/sir-kokabi/github-sorter",
Expand All @@ -28,6 +28,6 @@
"128": "images/icon-128.png"
}
},
"permissions": ["webNavigation", "storage", "scripting", "alarms", "tabs", "activeTab"],
"permissions": ["storage", "scripting", "tabs", "activeTab"],
"host_permissions": ["https://github.com/*"]
}
39 changes: 3 additions & 36 deletions src/firefox/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,9 @@ async function inject(selector, githubToken) {
}

async function injectStars(link) {
let stars;
try {
stars = await getStars(link.href);
const stars = await getStars(link.href);
if (!stars) return;

} catch (error) {
return;
}
const strong = document.createElement("strong");
strong.id = "github-stars-14151312";
strong.setAttribute("stars", stars);
Expand All @@ -152,22 +148,13 @@ async function inject(selector, githubToken) {
async function getStars(githubRepoURL) {
const repoName = githubRepoURL.match(/github\.com\/([^/]+\/[^/]+)/)[1];

const cache = await chrome.storage.local.get(repoName);
const repoStars = cache?.[repoName]?.stars;

if (repoStars) {
return repoStars;
}

const response = await fetch(`https://api.github.com/repos/${repoName}`, {
headers: { Authorization: `Token ${githubToken}` },
});

const data = await response.json();
const stars = data.stargazers_count;

await chrome.storage.local.set({ [repoName]: { stars, time: new Date().getTime() } });

return stars;
}

Expand All @@ -184,24 +171,4 @@ async function inject(selector, githubToken) {
return `${formattedNumber}${suffixes[suffixIndex]}`;
}

}

chrome.runtime.onInstalled.addListener(async (details) => {
await chrome.alarms.create("clear-expired-stars", {
periodInMinutes: 60
});
});

chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === "clear-expired-stars") {
const allSavedData = await chrome.storage.local.get();
for (let key in allSavedData) {
const time = allSavedData[key].time;

const currentTime = new Date().getTime();
if (currentTime - time < 24 * 60 * 60 * 1000) return

await chrome.storage.local.remove(key);
}
}
});
}
4 changes: 2 additions & 2 deletions src/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Github Sorter",
"short_name": "GithubSorter",
"version": "1.0.0",
"version": "1.0.1",
"description": "Sort github repos by stars",
"author": "Ayub Kokabi",
"homepage_url": "https://github.com/sir-kokabi/github-sorter",
Expand All @@ -27,7 +27,7 @@
"128": "images/icon-128.png"
}
},
"permissions": ["webNavigation", "storage", "scripting", "alarms", "tabs", "activeTab"],
"permissions": ["storage", "scripting", "tabs", "activeTab"],
"host_permissions": ["https://github.com/*"],

"browser_specific_settings": {
Expand Down

0 comments on commit abacccc

Please sign in to comment.