Skip to content

Commit b6aa824

Browse files
committed
fix: get image by XHR
1 parent 9ad2798 commit b6aa824

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

app/scripts/utils/fetches.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ export const fetchToGetImageBlob = async ({ imageUrl }: { imageUrl: string }): P
140140
return null
141141
}
142142

143+
export const fetchToGetImageBlobByXHR = async ({ imageUrl }: { imageUrl: string }): Promise<null | Blob> => {
144+
if (!imageUrl) return null
145+
try {
146+
const responseBlob = await new Promise<Blob | null>((resolve, reject) => {
147+
const xhr = new XMLHttpRequest()
148+
xhr.open('get', imageUrl)
149+
xhr.responseType = 'blob'
150+
xhr.onload = () => {
151+
resolve(xhr.response as Blob)
152+
}
153+
xhr.send()
154+
xhr.onerror = () => {
155+
resolve(null)
156+
}
157+
})
158+
await sleep(3 * Math.random())
159+
return responseBlob
160+
} catch (e) {
161+
console.log(`fetchToGetImageBlobByXHR`, e)
162+
}
163+
return null
164+
}
165+
143166
export const fetchToGetVideoBlob = async ({ videoUrl }: { videoUrl: string }): Promise<null | Blob> => {
144167
if (!videoUrl) return null
145168
try {
@@ -159,7 +182,7 @@ export const fetchToGetVideoBlob = async ({ videoUrl }: { videoUrl: string }): P
159182
await sleep(3 * Math.random())
160183
return respBlob
161184
} catch (e) {
162-
console.log(`fetchToGetImageBlob`, e)
185+
console.log(`fetchToGetVideoBlob`, e)
163186
}
164187
return null
165188
}
@@ -181,7 +204,7 @@ export const fetchToGetVideoBlobByXHR = async ({ videoUrl }: { videoUrl: string
181204
})
182205
return responseBlob
183206
} catch (e) {
184-
console.log(`fetchToGetImageBlob`, e)
207+
console.log(`fetchToGetVideoBlobByXHR`, e)
185208
}
186209
return null
187210
}
@@ -200,7 +223,7 @@ export const fetchToGetLongText = async ({ mblogId }: { mblogId?: string }) => {
200223
await sleep(3 * Math.random())
201224
return longTextContent || null
202225
} catch (e) {
203-
console.log(`fetchToGetImageBlob`, e)
226+
console.log(`fetchToGetLongText`, e)
204227
}
205228
return null
206229
}

app/scripts/utils/tools.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import JSZip from 'jszip'
22
const FileSaver = require('file-saver')
33
import _ from 'lodash'
4-
import { fetchToGetImageBlob, fetchToGetVideoBlobByXHR, fetchToGetLongText } from './fetches'
4+
import { fetchToGetImageBlob, fetchToGetImageBlobByXHR, fetchToGetVideoBlobByXHR, fetchToGetLongText } from './fetches'
55
import { favIcon32 } from './constants'
66

77
// watch Element by MutationObserver
@@ -208,7 +208,7 @@ const convertBlogList = async ({
208208
weiboCount: _count,
209209
weiboPicCount: _count_pic_count,
210210
})
211-
const picBlob = await fetchToGetImageBlob({ imageUrl: picShow?.url })
211+
const picBlob = await fetchToGetImageBlobByXHR({ imageUrl: picShow?.url })
212212
if (picBlob) {
213213
imageFolder?.file(picShow.picName, picBlob)
214214
}
@@ -256,7 +256,7 @@ const convertBlogList = async ({
256256
weiboCount: _count,
257257
weiboPicCount: _count_pic_count,
258258
})
259-
const picBlob = await fetchToGetImageBlob({ imageUrl: retweetPicShow?.url })
259+
const picBlob = await fetchToGetImageBlobByXHR({ imageUrl: retweetPicShow?.url })
260260
if (picBlob) {
261261
imageFolder?.file(retweetPicShow.picName, picBlob)
262262
}
@@ -382,7 +382,7 @@ const convertBlogList = async ({
382382
const userPicUrl = userInfo?.avatar_hd || userInfo?.profile_image_url || userInfo?.avatar_large || undefined
383383
if (userPicUrl) {
384384
userInfo.picShow = matchImageOrVideoFromUrl(userPicUrl)
385-
const picBlob = await fetchToGetImageBlob({ imageUrl: userPicUrl })
385+
const picBlob = await fetchToGetImageBlobByXHR({ imageUrl: userPicUrl })
386386
if (picBlob) {
387387
imageFolder?.file(userInfo.picShow, picBlob)
388388
}

0 commit comments

Comments
 (0)