DataviewUtils
Tue Aug 05 2025 15:15:32 GMT+0000 (Coordinated Universal Time)
Saved by
@touchSort
class DataviewUtils {
static async loadScript(source, args={}){
const promise = (this._isURL(source)) ?
this._fetch(source) : app.vault.adapter.read(source)
return promise.then(str => {
return new Promise((resolve, reject) => {
try {
const argNames = Object.keys(args)
const argValues = Object.values(args)
const fn = new Function(...argNames, str)
fn(...argValues)
resolve()
} catch (error) {
reject(error)
}
})
})
}
static async loadCSS(source) {
const promise = (this._isURL(source)) ?
this._fetch(source) : app.vault.adapter.read(source)
return promise.then(str => {
return new Promise((resolve) => {
const elm = document.createElement('style')
elm.textContent = str
dv.container.appendChild(elm)
resolve()
})
})
}
static _isURL(str) {
return /^https?:\/\/[^\s/$.?#].[^\s]*$/i.test(str)
}
static async _fetch(url){
return fetch(url).then(response => {
if (!response.ok)
throw new Error(`Failed to fetch file from ${url}: ${response.statusText}`)
console.log(`loaded: ${url}`)
return response.text()
})
}
}
content_copyCOPY
Comments