Skip to content

Commit

Permalink
fix: add redundant api
Browse files Browse the repository at this point in the history
  • Loading branch information
XYShaoKang committed Feb 28, 2022
1 parent f24cc90 commit 115467f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
5 changes: 3 additions & 2 deletions public/manifest-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"default_popup": "popup.html"
},
"content_security_policy": {
"extension_pages": "default-src 'self' http://localhost:9100; script-src 'self' http://localhost:9100; script-src-elem 'self' http://localhost:9100; style-src 'self' 'unsafe-inline'; connect-src 'self' ws://localhost:9100 http://localhost:9100 https://leetcode-rating-predictor.herokuapp.com https://leetcode-cn.com"
"extension_pages": "default-src 'self' http://localhost:9100; script-src 'self' http://localhost:9100; script-src-elem 'self' http://localhost:9100; style-src 'self' 'unsafe-inline'; connect-src 'self' ws://localhost:9100 http://localhost:9100 https://leetcode-rating-predictor.herokuapp.com https://leetcode-predictor.herokuapp.com https://leetcode-cn.com"
},
"permissions": ["storage", "scripting", "background", "activeTab"],
"web_accessible_resources": [
Expand All @@ -24,7 +24,8 @@
"host_permissions": [
"https://leetcode-cn.com/*",
"http://localhost:9100/*",
"https://leetcode-rating-predictor.herokuapp.com/*"
"https://leetcode-rating-predictor.herokuapp.com/*",
"https://leetcode-predictor.herokuapp.com/*"
],
"content_scripts": [
{
Expand Down
7 changes: 4 additions & 3 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "LeetCode-exten",
"description": "LeetCode 网页扩展",
"version": "0.2.1",
"version": "0.0.0",
"manifest_version": 3,
"action": {
"default_popup": "popup.html"
},
"content_security_policy": {
"extension_pages": "default-src 'self'; script-src 'self'; script-src-elem 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https://leetcode-rating-predictor.herokuapp.com https://leetcode-cn.com"
"extension_pages": "default-src 'self'; script-src 'self'; script-src-elem 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https://leetcode-rating-predictor.herokuapp.com https://leetcode-predictor.herokuapp.com https://leetcode-cn.com"
},
"permissions": ["storage", "scripting", "background", "activeTab"],
"web_accessible_resources": [
Expand All @@ -18,7 +18,8 @@
],
"host_permissions": [
"https://leetcode-cn.com/*",
"https://leetcode-rating-predictor.herokuapp.com/*"
"https://leetcode-rating-predictor.herokuapp.com/*",
"https://leetcode-predictor.herokuapp.com/*"
],
"content_scripts": [
{
Expand Down
52 changes: 42 additions & 10 deletions src/background/lcrp.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
// Leetcode Rating Predictor

const LCRP_API =
'https://leetcode-rating-predictor.herokuapp.com/api/v1/predictions'
const RETRY_COUNT = 10
// @see: https://github.com/SysSn13/leetcode-rating-predictor/blob/4a7f4057bd5bf94727723b0bc02d781be573a3eb/chrome-extension/background.js#L26
const LCRP_API = [
'https://leetcode-rating-predictor.herokuapp.com/api/v1/predictions',
'https://leetcode-predictor.herokuapp.com/api/v1/predictions',
]
let API_INDEX = 0
const RETRY_COUNT = 5

async function api(url: string, retry = 1): Promise<any> {
const res = await fetch(url)
function sleep(time: number): Promise<void> {
return new Promise(function (resolve) {
setTimeout(resolve, time)
})
}

const changeApiIndex = (() => {
let enable = true
return () => {
if (enable) {
API_INDEX = (API_INDEX + 1) % LCRP_API.length

enable = false
setTimeout(() => {
enable = true
}, 2000)
}
}
})()

async function api(
{ contestId, handles }: { contestId: string; handles: string[] },
retry = 1
): Promise<any> {
const baseUrl = LCRP_API[API_INDEX]
const url = new URL(baseUrl)
url.searchParams.set('contestId', contestId)
url.searchParams.set('handles', handles.join(';'))
console.log(baseUrl, retry)
const res = await fetch(url.toString())
if (res.status === 200) {
return res.json()
} else if (retry < RETRY_COUNT) {
return api(url, retry + 1)
// 换一个 API 试试
changeApiIndex()
await sleep(2 ** retry * 100)
return api({ contestId, handles }, retry + 1)
} else {
throw new Error('获取数据失败')
}
Expand Down Expand Up @@ -92,11 +127,8 @@ async function getPrediction(
usernames = message.usernames
}

const url = new URL(LCRP_API)
url.searchParams.set('contestId', contestId)
url.searchParams.set('handles', usernames.join(';'))
try {
const data = (await api(url.toString())) as PredictorType
const data = (await api({ contestId, handles: usernames })) as PredictorType
const itemMap = new Map<string, PredictorType['items']['0']>()
for (const item of data.items) {
itemMap.set(item._id, item)
Expand Down

0 comments on commit 115467f

Please sign in to comment.