mirror of https://github.com/requarks/wiki
parent
e03a80dccc
commit
77086a6e0a
@ -0,0 +1,8 @@
|
||||
key: htmlImagePrefetch
|
||||
title: Image Prefetch
|
||||
description: Prefetch remotely rendered images (korki/plantuml)
|
||||
author: requarks.io
|
||||
icon: mdi-cloud-download-outline
|
||||
enabledDefault: false
|
||||
dependsOn: htmlCore
|
||||
props: {}
|
@ -0,0 +1,30 @@
|
||||
const request = require('request-promise')
|
||||
|
||||
const prefetch = async (element) => {
|
||||
const url = element.attr(`src`)
|
||||
let response
|
||||
try {
|
||||
response = await request({
|
||||
method: `GET`,
|
||||
url,
|
||||
resolveWithFullResponse: true
|
||||
})
|
||||
} catch (err) {
|
||||
WIKI.logger.warn(`Failed to prefetch ${url}`)
|
||||
WIKI.logger.warn(err)
|
||||
return
|
||||
}
|
||||
const contentType = response.headers[`content-type`]
|
||||
const image = Buffer.from(response.body).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)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue