diff --git a/app/extension/chrome/src/background/index.ts b/app/extension/chrome/src/background/index.ts index 025b775..b731665 100644 --- a/app/extension/chrome/src/background/index.ts +++ b/app/extension/chrome/src/background/index.ts @@ -25,7 +25,9 @@ let syncMarksSuccessCount = 0 let syncMarksFailCount = 0 let syncGithubSuccessCount = 0 let syncGithubFailCount = 0 +let fetchGithubStarredEnd = true const maxConcurrent = 3 // openai rate limit +const maxGithubPerPage = 100 async function sendSavedStatus(res: SwResponse) { chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => { @@ -121,7 +123,7 @@ function syncMarksToDB() { function sendSyncGithubStatus() { chrome.runtime.sendMessage({ action: 'syncGithubStatus', - syncStatus: { index: githubIdx, count: githubCount, state: stopSyncGithub, + syncStatus: { index: githubIdx, count: githubCount, state: stopSyncGithub, fetchEnd: fetchGithubStarredEnd, isEnd: syncGithubEnd, successCount: syncGithubSuccessCount, failCount: syncGithubFailCount } }) } @@ -134,7 +136,7 @@ function sendSyncGithubStatusToContent() { if (tabs[0].id) { chrome.tabs.sendMessage(tabs[0].id, { action: 'syncGithubStatus', - syncStatus: { index: githubIdx, count: githubCount, state: stopSyncGithub, + syncStatus: { index: githubIdx, count: githubCount, state: stopSyncGithub, fetchEnd: fetchGithubStarredEnd, isEnd: syncGithubEnd, successCount: syncGithubSuccessCount, failCount: syncGithubFailCount } }) } @@ -248,15 +250,46 @@ chrome.runtime.onMessage.addListener(async (request: ContentRequest, sender, sen } } else if (action === 'syncGithubStarred') { - if (request.syncData) { - githubStarred = request.syncData - githubCount = githubStarred.length + if (request.fetchGithubData) { + const { user: githubUser, token: githubToken } = request.fetchGithubData + githubStarred = [] githubIdx = 0 + githubCount = 0 syncGithubCount = 0 syncGithubSuccessCount = 0 syncGithubFailCount = 0 syncGithubEnd = false stopSyncGithub = false + fetchGithubStarredEnd = false + + let api = '' + let page = 1 + let starredCountOfPage = 0 + + do { + if (githubUser) + api = `https://api.github.com/users/LarchLiu/starred?per_page=${maxGithubPerPage}&page=${page}` + else + api = `https://api.github.com/user/starred?per_page=${maxGithubPerPage}&page=${page}` + const res = await fetch(api, { + headers: { + Accept: 'application/vnd.github+json', + Authorization: `Bearer ${githubToken}`, + 'X-GitHub-Api-Version': '2022-11-28' + } + }) + if (res.status === 200) { + const starred = await res.json() + starredCountOfPage = starred.length + githubCount += starredCountOfPage + githubStarred = githubStarred.concat(starred.map((item: any) => item.html_url)) + } + page++ + } while(starredCountOfPage === maxGithubPerPage) + fetchGithubStarredEnd = true + githubCount = githubStarred.length + + sendSyncGithubStatus() syncGithubToDB() sendResponse({ message: 'handling save to DB' }) } diff --git a/app/extension/chrome/src/popup/App.vue b/app/extension/chrome/src/popup/App.vue index fad8f09..0e846fd 100644 --- a/app/extension/chrome/src/popup/App.vue +++ b/app/extension/chrome/src/popup/App.vue @@ -30,10 +30,8 @@ const syncGithubSuccessCount = ref(0) const syncGithubFailCount = ref(0) const syncMarksEnd = ref(false) const syncGithubEnd = ref(false) -const fetchStarredEnd = ref(true) -const maxGithubPerPage = 100 +const fetchGithubStarredEnd = ref(true) let bookmarks: string[] = [] -let githubStarred: string[] = [] let starTimer: NodeJS.Timeout let bubblyTimer: NodeJS.Timeout const colorPreset = [ @@ -185,38 +183,14 @@ async function onGithubSync() { if (githubToken.value) { githubCount.value = 0 githubIdx.value = 0 - fetchStarredEnd.value = false - githubStarred = [] - - let api = '' - let page = 1 - let starredCountOfPage = 0 - - do { - if (githubUser.value) - api = `https://api.github.com/users/LarchLiu/starred?per_page=${maxGithubPerPage}&page=${page}` - else - api = `https://api.github.com/user/starred?per_page=${maxGithubPerPage}&page=${page}` - const res = await fetch(api, { - headers: { - Accept: 'application/vnd.github+json', - Authorization: `Bearer ${githubToken.value}`, - 'X-GitHub-Api-Version': '2022-11-28' - } - }) - if (res.status === 200) { - const starred = await res.json() - starredCountOfPage = starred.length - githubCount.value += starredCountOfPage - githubStarred = githubStarred.concat(starred.map((item: any) => item.html_url)) - } - page++ - } while(starredCountOfPage === maxGithubPerPage) - fetchStarredEnd.value = true + fetchGithubStarredEnd.value = false chrome.runtime.sendMessage( { action: 'syncGithubStarred', - syncData: githubStarred, + fetchGithubData: { + token: githubToken.value, + user: githubUser.value, + } }, ) } @@ -307,6 +281,7 @@ onMounted(() => { syncGithubSuccessCount.value = status.successCount syncGithubFailCount.value = status.failCount syncGithubEnd.value = status.isEnd + fetchGithubStarredEnd.value = status.fetchEnd } } }) @@ -456,7 +431,7 @@ onMounted(() => {
-
+
@@ -466,7 +441,7 @@ onMounted(() => {
-
+
{
-
+
void