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() }) } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter