mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
966 B
34 lines
966 B
const prefetch = async (element) => {
|
|
const url = element.attr(`src`)
|
|
let response
|
|
try {
|
|
response = await fetch(url, { signal: AbortSignal.timeout(10000) })
|
|
} catch (err) {
|
|
WIKI.logger.warn(`Failed to prefetch ${url}`)
|
|
WIKI.logger.warn(err)
|
|
return
|
|
}
|
|
if (!response.ok) {
|
|
WIKI.logger.warn(`Failed to prefetch ${url}: HTTP ${response.status}`)
|
|
return
|
|
}
|
|
const contentType = response.headers.get('content-type')
|
|
if (!contentType) {
|
|
WIKI.logger.warn(`Failed to prefetch ${url}: missing content-type`)
|
|
return
|
|
}
|
|
const buffer = await response.arrayBuffer()
|
|
const image = Buffer.from(buffer).toString('base64')
|
|
element.attr('src', `data:${contentType};base64,${image}`)
|
|
element.removeClass('prefetch-candidate')
|
|
}
|
|
|
|
module.exports = {
|
|
async init($) {
|
|
const promises = $('img.prefetch-candidate').map((index, element) => {
|
|
return prefetch($(element))
|
|
}).toArray()
|
|
await Promise.all(promises)
|
|
}
|
|
}
|