From 12219dddd435557d5ecf258990bd206d32c42089 Mon Sep 17 00:00:00 2001 From: frankie-tech <26461046+frankie-tech@users.noreply.github.com> Date: Wed, 21 Oct 2020 15:03:16 -0400 Subject: [PATCH 1/7] removes the update and complete events also simplifies the parameters for the updatePercent and appendVerification function for now the old event emitter functions are commented out --- src/index.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index d2fc60c..63db6ba 100644 --- a/src/index.js +++ b/src/index.js @@ -45,6 +45,7 @@ import getSettings from './includes/get-settings'; const eventDefault = { event: 'whc:Update#' + i, difficulty, + form, verification: [], progress: 0, done: false, @@ -84,8 +85,8 @@ import getSettings from './includes/get-settings'; ); } - /** @type { (event: import('./types').eventInterface) => void } */ - function appendVerification({ verification }) { + /** @type { (verification: import('./types').Verification[]) => void } */ + function appendVerification(verification) { // prettier-ignore form.insertAdjacentHTML('beforeend', ``); button.classList.add('done'); @@ -96,11 +97,9 @@ import getSettings from './includes/get-settings'; } /** - * @param {object} param - * @param {HTMLButtonElement} param.button - * @param {string} param.message + * @param {string} message */ - function updatePercent({ message }) { + function updatePercent(message) { const percent = message.match(/\d{2,3}/); if (!percent) return; @@ -115,8 +114,8 @@ import getSettings from './includes/get-settings'; ); } - e.on('whc:Update#' + i, updatePercent); - e.on('whc:Complete#' + i, appendVerification); + // e.on('whc:Update#' + i, updatePercent); + // e.on('whc:Complete#' + i, appendVerification); /** * @this {Worker} @@ -127,6 +126,8 @@ import getSettings from './includes/get-settings'; const { action, message, verification } = data; if (action === 'captchaSuccess') { + return appendVerification(verification); + /* return e.run( 'whc:Complete#' + i, merge({ @@ -136,8 +137,11 @@ import getSettings from './includes/get-settings'; progress: 100, }) ); + */ } if (action === 'message') { + return updatePercent(message); + /* return e.run( 'whc:Update#' + i, merge({ @@ -146,6 +150,7 @@ import getSettings from './includes/get-settings'; progress: 0, }) ); + */ } } From b6bc0ca01a927b350a9ecd3ebf3ed52dbb8a88bd Mon Sep 17 00:00:00 2001 From: frankie-tech <26461046+frankie-tech@users.noreply.github.com> Date: Wed, 21 Oct 2020 15:03:58 -0400 Subject: [PATCH 2/7] comments out the Start event its extraneous so its not necessary anyways --- src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.js b/src/index.js index 63db6ba..9915dee 100644 --- a/src/index.js +++ b/src/index.js @@ -77,12 +77,14 @@ import getSettings from './includes/get-settings'; difficulty, time, }); + /* e.run( 'whc:Start#' + i, merge({ event: 'whc:Start#' + i, }) ); + */ } /** @type { (verification: import('./types').Verification[]) => void } */ From aee03fb571907aba47a8bc39be7d0658814bb89d Mon Sep 17 00:00:00 2001 From: frankie-tech <26461046+frankie-tech@users.noreply.github.com> Date: Wed, 21 Oct 2020 15:04:40 -0400 Subject: [PATCH 3/7] fixes eventInterface form missing --- src/types.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types.d.ts b/src/types.d.ts index 91b719c..5083531 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -22,6 +22,7 @@ export { Verification, EncodedMessage, WorkerResponse }; interface eventInterface { event: string; difficulty?: number | string; + form?: HTMLFormElement; verification?: Verification[]; progress?: number; done?: boolean; From f21650281800876a2b0bedc8845b3a836202b859 Mon Sep 17 00:00:00 2001 From: frankie-tech <26461046+frankie-tech@users.noreply.github.com> Date: Wed, 21 Oct 2020 16:22:11 -0400 Subject: [PATCH 4/7] commented out the last of the pub/sub All that's left is to test each of the pages --- src/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 9915dee..52958ec 100644 --- a/src/index.js +++ b/src/index.js @@ -4,12 +4,12 @@ * (c) 2020 Exceleron Designs, MIT License, https://excelerondesigns.com */ -import emitter from './includes/emit'; +// import emitter from './includes/emit'; import worker from './includes/worker'; import getSettings from './includes/get-settings'; (function (w) { - const e = emitter(); + // const e = emitter(); /** @type {NodeListOf} */ const forms = document.querySelectorAll('[data-whc]'); @@ -36,12 +36,15 @@ import getSettings from './includes/get-settings'; * @param {string} type * @param {object} detail */ + /* const allEmit = (type, detail) => detail.form.dispatchEvent(new CustomEvent(type, { detail })); // TODO: Change this so that it doesn't do ALL forms, just the ones that have debug e.on('*', allEmit); + */ } /** @type {import('./types').eventInterface} */ + /* const eventDefault = { event: 'whc:Update#' + i, difficulty, @@ -50,9 +53,10 @@ import getSettings from './includes/get-settings'; progress: 0, done: false, }; + */ /** @type { ( obj:import('./types').eventInterface ) => object } */ - const merge = obj => Object.assign(eventDefault, obj); + // const merge = obj => Object.assign(eventDefault, obj); /** @param {Function} fn */ function createWorker(fn) { @@ -106,6 +110,7 @@ import getSettings from './includes/get-settings'; if (!percent) return; form.dataset.progress = percent + '%'; + /* e.run( 'whc:Progress#' + i, merge({ @@ -114,6 +119,7 @@ import getSettings from './includes/get-settings'; done: +percent[0] === 100, }) ); + */ } // e.on('whc:Update#' + i, updatePercent); From df833cb6b0d99da7dfc71164b662d48b06f2c3c2 Mon Sep 17 00:00:00 2001 From: frankie-tech <26461046+frankie-tech@users.noreply.github.com> Date: Wed, 21 Oct 2020 16:27:03 -0400 Subject: [PATCH 5/7] removes all references to pubsub TODO: create a new debug solution --- src/index.js | 72 +--------------------------------------------------- 1 file changed, 1 insertion(+), 71 deletions(-) diff --git a/src/index.js b/src/index.js index 52958ec..528b1b1 100644 --- a/src/index.js +++ b/src/index.js @@ -3,14 +3,10 @@ * WeHateCaptchas Self-Instantiating-Plugin * (c) 2020 Exceleron Designs, MIT License, https://excelerondesigns.com */ - -// import emitter from './includes/emit'; import worker from './includes/worker'; import getSettings from './includes/get-settings'; (function (w) { - // const e = emitter(); - /** @type {NodeListOf} */ const forms = document.querySelectorAll('[data-whc]'); @@ -32,31 +28,8 @@ import getSettings from './includes/get-settings'; const { button, difficulty, finished, debug } = getSettings(form); if (debug) { - /** - * @param {string} type - * @param {object} detail - */ - /* - const allEmit = (type, detail) => - detail.form.dispatchEvent(new CustomEvent(type, { detail })); - // TODO: Change this so that it doesn't do ALL forms, just the ones that have debug - e.on('*', allEmit); - */ + // TODO: Add a debug solution her } - /** @type {import('./types').eventInterface} */ - /* - const eventDefault = { - event: 'whc:Update#' + i, - difficulty, - form, - verification: [], - progress: 0, - done: false, - }; - */ - - /** @type { ( obj:import('./types').eventInterface ) => object } */ - // const merge = obj => Object.assign(eventDefault, obj); /** @param {Function} fn */ function createWorker(fn) { @@ -81,14 +54,6 @@ import getSettings from './includes/get-settings'; difficulty, time, }); - /* - e.run( - 'whc:Start#' + i, - merge({ - event: 'whc:Start#' + i, - }) - ); - */ } /** @type { (verification: import('./types').Verification[]) => void } */ @@ -110,21 +75,7 @@ import getSettings from './includes/get-settings'; if (!percent) return; form.dataset.progress = percent + '%'; - /* - e.run( - 'whc:Progress#' + i, - merge({ - event: 'whc:Progress#' + i, - progress: +percent[0], - done: +percent[0] === 100, - }) - ); - */ } - - // e.on('whc:Update#' + i, updatePercent); - // e.on('whc:Complete#' + i, appendVerification); - /** * @this {Worker} * @param {object} param @@ -135,30 +86,9 @@ import getSettings from './includes/get-settings'; if (action === 'captchaSuccess') { return appendVerification(verification); - /* - return e.run( - 'whc:Complete#' + i, - merge({ - event: 'whc:Complete#' + i, - verification, - done: true, - progress: 100, - }) - ); - */ } if (action === 'message') { return updatePercent(message); - /* - return e.run( - 'whc:Update#' + i, - merge({ - event: 'whc:Completed#' + i, - message, - progress: 0, - }) - ); - */ } } From 3577c2d0f19b2d7826621aa5fc7d418950dbbb3a Mon Sep 17 00:00:00 2001 From: frankie-tech <26461046+frankie-tech@users.noreply.github.com> Date: Thu, 22 Oct 2020 09:29:51 -0400 Subject: [PATCH 6/7] swaps out the pubsub for a debug logger debug logger uses WeakMap to track forms and the history of the logged messages if the debug attribute is not on the form, it is not added to the WeakMap A proxy is used so that whenever a message is added to a specific log type: log, info, error, warn it will be saved as such too It might be nice to add a callback argument, but if that happens, it is time to break it out into its own package --- src/includes/emit.js | 37 ------------------------------- src/includes/log.js | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 37 deletions(-) delete mode 100644 src/includes/emit.js create mode 100644 src/includes/log.js diff --git a/src/includes/emit.js b/src/includes/emit.js deleted file mode 100644 index fd0280c..0000000 --- a/src/includes/emit.js +++ /dev/null @@ -1,37 +0,0 @@ -// @ts-check -export default function () { - const all = new Map(); - return { - /** - * @param {string} e - event type - * @param {Function} fn - function to run when the event is called, should accept an object - */ - on(e, fn) { - const handlers = all.get(e); - const added = handlers && handlers.push(fn); - if (!added) { - all.set(e, [fn]); - } - }, - // https://github.com/developit/mitt/blob/master/src/index.ts#L56 - /** - * @param {string} e - event type - * @param {Function} fn - function to run when the event is called, should accept an object - */ - off(e, fn) { - const handlers = all.get(e); - if (handlers) { - handlers.splice(handlers.indexOf(fn) >>> 0, 1); - } - }, - /** - * - * @param {string} e - event type - * @param {import('../types').eventInterface} obj - Arguments used for event handlers - */ - run(e, obj) { - (all.get(e) || []).forEach(fn => fn(obj)); - (all.get('*') || []).forEach(fn => fn(e, obj)); - }, - }; -} diff --git a/src/includes/log.js b/src/includes/log.js new file mode 100644 index 0000000..c11da37 --- /dev/null +++ b/src/includes/log.js @@ -0,0 +1,52 @@ +// @ts-check +const history = new WeakMap(); +/** + * @param {HTMLFormElement} form + */ +let Log = form => { + const id = '#' + form.getAttribute('id'), + eventName = 'whc' + id; + + return new Proxy( + { + set history(value) { + history.has(form) + ? history.set(form, [...history.get(form), value]) + : history.set(form, [value]); + }, + get history() { + return history.get(form); + }, + /** + * @param {object} detail + */ + set event(detail) { + document.dispatchEvent(new CustomEvent(eventName, { detail })); + }, + }, + { + /** + * + * @param {object} target + * @param {string} key + * @param {object} value + */ + set(target, key, value) { + if (key === 'history') return target.history; + const entry = Object.assign( + { + form: id, + type: key, + timestamp: performance.now(), + }, + value + ); + target.history = entry; + target.event = entry; + return true; + }, + } + ); +}; + +export default Log; From b46815c5ae375b008a15d932816c4a756f84771b Mon Sep 17 00:00:00 2001 From: frankie-tech <26461046+frankie-tech@users.noreply.github.com> Date: Thu, 22 Oct 2020 09:31:45 -0400 Subject: [PATCH 7/7] removes last of emitter, implements Log adds new logger util to manage debugging and events no more pubsub for handling inner function communications instead just going to use regular old function to function style programming call me old fasioned You can search for `log.` to see how it is used. It is also a likely candidate for a separate package in its own right --- src/index.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 528b1b1..593eaed 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ * WeHateCaptchas Self-Instantiating-Plugin * (c) 2020 Exceleron Designs, MIT License, https://excelerondesigns.com */ +import Log from './includes/log'; import worker from './includes/worker'; import getSettings from './includes/get-settings'; @@ -18,7 +19,6 @@ import getSettings from './includes/get-settings'; */ // @ts-ignore w.whcWorkers = []; - /** * @param {HTMLFormElement} form * @param {number} i @@ -26,11 +26,7 @@ import getSettings from './includes/get-settings'; var Constructor = function (form, i) { // TODO: implement the eventName into the pubsub system const { button, difficulty, finished, debug } = getSettings(form); - - if (debug) { - // TODO: Add a debug solution her - } - + const log = debug ? Log(form) : {}; /** @param {Function} fn */ function createWorker(fn) { try { @@ -39,8 +35,16 @@ import getSettings from './includes/get-settings'; type: 'application/javascript', }); const blobUrl = URL.createObjectURL(blob); + log.info = { + title: 'Worker Created', + }; return new Worker(blobUrl); } catch (e) { + // @ts-ignore + log.error = { + title: 'Unknown Error', + error: e, + }; throw new Error('Unknown Error: ' + e); } } @@ -64,6 +68,11 @@ import getSettings from './includes/get-settings'; button.removeAttribute('disabled'); button.setAttribute('value', '' + finished); // @ts-ignore + log.info = { + title: 'Verified Form', + verification, + }; + // @ts-ignore w.whcWorkers[i].terminate(); } @@ -75,6 +84,11 @@ import getSettings from './includes/get-settings'; if (!percent) return; form.dataset.progress = percent + '%'; + // @ts-ignore + log.info = { + title: 'Progress Update', + percent: percent + '%', + }; } /** * @this {Worker}