From cabc887814cc8fe5a88bb08def37d84d98299b05 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sun, 23 Feb 2025 22:52:43 +0100 Subject: [PATCH] fix: check if is color before changing colors --- main.ts | 7 +++++-- package.json | 2 ++ pnpm-lock.yaml | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index a3c5244..33b33b6 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,7 @@ import { domToPng } from 'modern-screenshot' +import hexColorRegex from 'hex-color-regex' + +const isColor = (color: string) => hexColorRegex().test(color) const params = new URL(document.location.href).searchParams @@ -62,7 +65,7 @@ if (textHide === 'hide') { const primaryColorParam = params.get('primary') -if (primaryColorParam) { +if (primaryColorParam && isColor(`#${primaryColorParam}`)) { document.documentElement.style.setProperty( '--primary-color', `#${primaryColorParam}` @@ -71,7 +74,7 @@ if (primaryColorParam) { const colorParam = params.get('color') -if (colorParam) { +if (colorParam && isColor(`#${colorParam}`)) { document.documentElement.style.setProperty('--color', `#${colorParam}`) } diff --git a/package.json b/package.json index de28b78..ea7e47f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@vueuse/core": "^12.2.0", "chart.xkcd": "^1.1.15", "comlink": "^4.4.2", + "hex-color-regex": "^1.1.0", "modern-screenshot": "^4.5.5", "pinia": "^2.3.1", "random-js": "^2.1.0", @@ -26,6 +27,7 @@ }, "devDependencies": { "@faker-js/faker": "^9.4.0", + "@types/hex-color-regex": "^1.1.3", "@types/node": "^22.10.2", "@vitejs/plugin-vue": "^5.2.1", "sass": "^1.85.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 47ce388..35fbc6a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: comlink: specifier: ^4.4.2 version: 4.4.2 + hex-color-regex: + specifier: ^1.1.0 + version: 1.1.0 modern-screenshot: specifier: ^4.5.5 version: 4.5.5 @@ -36,6 +39,9 @@ importers: '@faker-js/faker': specifier: ^9.4.0 version: 9.4.0 + '@types/hex-color-regex': + specifier: ^1.1.3 + version: 1.1.3 '@types/node': specifier: ^22.10.2 version: 22.10.2 @@ -647,6 +653,9 @@ packages: '@types/estree@1.0.6': resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} + '@types/hex-color-regex@1.1.3': + resolution: {integrity: sha512-xOvT3NwAinCRD0knfP5wRVWGCUmQVtYO6TDMzLeVlUckdDvNOSgNCNGWmSbFde/Ll42QM6YQ4qHkrOpdhU3aVg==} + '@types/node@22.10.2': resolution: {integrity: sha512-Xxr6BBRCAOQixvonOye19wnzyDiUtTeqldOOmj3CkeblonbccA12PFwlufvRdrpjXxqnmUaeiU5EOA+7s5diUQ==} @@ -862,6 +871,9 @@ packages: engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0} os: [darwin] + hex-color-regex@1.1.0: + resolution: {integrity: sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==} + immutable@5.0.3: resolution: {integrity: sha512-P8IdPQHq3lA1xVeBRi5VPqUm5HDgKnx0Ru51wZz5mjxHr5n3RWhjIpOFU7ybkUxfB+5IToy+OLaHYDBIWsv+uw==} @@ -1486,6 +1498,8 @@ snapshots: '@types/estree@1.0.6': {} + '@types/hex-color-regex@1.1.3': {} + '@types/node@22.10.2': dependencies: undici-types: 6.20.0 @@ -1787,6 +1801,8 @@ snapshots: fsevents@2.3.3: optional: true + hex-color-regex@1.1.0: {} + immutable@5.0.3: {} internmap@1.0.1: {}