From c2fa9d3031df33aafc32062a4e817c5138342a4d Mon Sep 17 00:00:00 2001 From: He4eT Date: Thu, 15 Aug 2024 04:48:46 +0200 Subject: [PATCH] milje.js: demo page support --- content_scripts/milje.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/content_scripts/milje.js b/content_scripts/milje.js index 703d038..20ad0e4 100644 --- a/content_scripts/milje.js +++ b/content_scripts/milje.js @@ -1,16 +1,31 @@ -/** - * Sorry, you are not allowed to use import in the content_script :c - * Copy'n'paste here! - */ -const storage = (browser ?? window.chrome)['storage'] +start() /* */ -storage.local.get(['params']) - .then(({params}) => params) - .catch(() => {}) - .then(assureParams) - .then(run) +function start () { + Promise.resolve() + .then(getParamsFromStorage) + .catch((e) => { + console.error(e) + console.log('An error occurred. Milje will use the default params.') + return {} + }) + .then(assureParams) + .then(run) +} + +/* Params */ + +function getParamsFromStorage () { + /** + * Sorry, you are not allowed to use import in the content_script :c + * Copy'n'paste of storage here! + */ + const storage = (browser ?? window.chrome)['storage'] + + return storage.local.get(['params']) + .then(({params}) => params) +} function assureParams(params) { const assureNumber = (rawValue, defaultValue, min, max) => { @@ -22,6 +37,7 @@ function assureParams(params) { return value } + const randomSeed = assureNumber(params.randomSeed, 0, 0, Infinity) return { @@ -91,6 +107,7 @@ function generatePattern(halfPatternSize, gridSize, randomSeed) { } } } + return matrix }