From abaccccc10c600e827b59f015c8dc3cc4a8b9304 Mon Sep 17 00:00:00 2001 From: Ayub Date: Tue, 3 Oct 2023 10:51:16 +0330 Subject: [PATCH] fix `NaNundefined` and remove unnecessary caching --- src/chrome/background.js | 39 +++------------------------------------ src/chrome/manifest.json | 4 ++-- src/firefox/background.js | 39 +++------------------------------------ src/firefox/manifest.json | 4 ++-- 4 files changed, 10 insertions(+), 76 deletions(-) diff --git a/src/chrome/background.js b/src/chrome/background.js index 8752bc5..e53acd6 100644 --- a/src/chrome/background.js +++ b/src/chrome/background.js @@ -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); @@ -151,13 +147,6 @@ 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}` }, }); @@ -165,8 +154,6 @@ async function inject(selector, githubToken) { const data = await response.json(); const stars = data.stargazers_count; - await chrome.storage.local.set({ [repoName]: { stars, time: new Date().getTime() } }); - return stars; } @@ -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); - } - } -}); \ No newline at end of file +} \ No newline at end of file diff --git a/src/chrome/manifest.json b/src/chrome/manifest.json index fe584ba..75a1105 100644 --- a/src/chrome/manifest.json +++ b/src/chrome/manifest.json @@ -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", @@ -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/*"] } diff --git a/src/firefox/background.js b/src/firefox/background.js index 2850ef9..01661c8 100644 --- a/src/firefox/background.js +++ b/src/firefox/background.js @@ -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); @@ -152,13 +148,6 @@ 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}` }, }); @@ -166,8 +155,6 @@ async function inject(selector, githubToken) { const data = await response.json(); const stars = data.stargazers_count; - await chrome.storage.local.set({ [repoName]: { stars, time: new Date().getTime() } }); - return stars; } @@ -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); - } - } -}); \ No newline at end of file +} \ No newline at end of file diff --git a/src/firefox/manifest.json b/src/firefox/manifest.json index 855947c..171e1fd 100644 --- a/src/firefox/manifest.json +++ b/src/firefox/manifest.json @@ -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", @@ -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": {