import QRCode from 'qrcode' self.onmessage = async (e) => { const { id, text, ecc, isBgTransparent, bgColor, fgType, fgColor1, fgColor2 } = e.data if (!text) { self.postMessage({ id, svgContent: '' }) return } try { let svgContent = await QRCode.toString(text, { type: 'svg', errorCorrectionLevel: ecc, margin: 1, color: { dark: fgType === 'solid' ? fgColor1 : '#000000', light: isBgTransparent ? '#00000000' : bgColor } }) if (fgType !== 'solid') { const isLinear = fgType === 'linear' const defs = isLinear ? `` : `` svgContent = svgContent.replace('${defs}`) // qrcode outputs so it's safe to replace svgContent = svgContent.replace(/stroke="#000000"/g, 'stroke="url(#qr-grad)"') } self.postMessage({ id, svgContent }) } catch (err) { self.postMessage({ id, error: err.message }) } }