-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.js
26 lines (23 loc) · 847 Bytes
/
resource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { BASIC_EXTENSION_MAP } from '@dr-js/core/module/common/module/MIME.js'
import { createElement } from '@dr-js/core/module/browser/DOM.js'
const { document, fetch } = window
// TODO: check if is needed, or simplify
const loadText = async (uri) => (await fetch(uri)).text()
const loadImage = (uri) => new Promise((resolve, reject) => createElement('img', {
src: uri,
onerror: reject,
onload: (event) => resolve(event.currentTarget)
}))
// TODO: document.body can be null if script is running from <head> tag and page is not fully loaded
const loadScript = (uri) => new Promise((resolve, reject) => document.body.appendChild(createElement('script', {
src: uri,
async: false,
type: BASIC_EXTENSION_MAP.js,
onerror: reject,
onload: (event) => resolve(event.currentTarget)
})))
export {
loadText,
loadImage,
loadScript
}