Replace CDN React/ReactDOM/Babel with local libs; remove Babel and inline scripts Build Tailwind locally, add safelist; switch to assets/tailwind.css Self-host Font Awesome and Inter (CSS + woff2); remove external font CDNs Implement strict CSP (no unsafe-inline/eval; scripts/styles/fonts from self) Extract inline handlers; move PWA scripts to external files Add local QR code generation (qrcode lib) and remove api.qrserver.com Improve SessionTypeSelector visual selection (highlighted background and ring) Keep PWA working with service worker and offline assets Refs: CSP hardening, offline-first, no external dependencies
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
const Utils = require('./utils')
|
|
|
|
function clearCanvas (ctx, canvas, size) {
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
|
|
|
if (!canvas.style) canvas.style = {}
|
|
canvas.height = size
|
|
canvas.width = size
|
|
canvas.style.height = size + 'px'
|
|
canvas.style.width = size + 'px'
|
|
}
|
|
|
|
function getCanvasElement () {
|
|
try {
|
|
return document.createElement('canvas')
|
|
} catch (e) {
|
|
throw new Error('You need to specify a canvas element')
|
|
}
|
|
}
|
|
|
|
exports.render = function render (qrData, canvas, options) {
|
|
let opts = options
|
|
let canvasEl = canvas
|
|
|
|
if (typeof opts === 'undefined' && (!canvas || !canvas.getContext)) {
|
|
opts = canvas
|
|
canvas = undefined
|
|
}
|
|
|
|
if (!canvas) {
|
|
canvasEl = getCanvasElement()
|
|
}
|
|
|
|
opts = Utils.getOptions(opts)
|
|
const size = Utils.getImageWidth(qrData.modules.size, opts)
|
|
|
|
const ctx = canvasEl.getContext('2d')
|
|
const image = ctx.createImageData(size, size)
|
|
Utils.qrToImageData(image.data, qrData, opts)
|
|
|
|
clearCanvas(ctx, canvasEl, size)
|
|
ctx.putImageData(image, 0, 0)
|
|
|
|
return canvasEl
|
|
}
|
|
|
|
exports.renderToDataURL = function renderToDataURL (qrData, canvas, options) {
|
|
let opts = options
|
|
|
|
if (typeof opts === 'undefined' && (!canvas || !canvas.getContext)) {
|
|
opts = canvas
|
|
canvas = undefined
|
|
}
|
|
|
|
if (!opts) opts = {}
|
|
|
|
const canvasEl = exports.render(qrData, canvas, opts)
|
|
|
|
const type = opts.type || 'image/png'
|
|
const rendererOpts = opts.rendererOpts || {}
|
|
|
|
return canvasEl.toDataURL(type, rendererOpts.quality)
|
|
}
|