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; diff --git a/src/index.js b/src/index.js index d2fc60c..593eaed 100644 --- a/src/index.js +++ b/src/index.js @@ -3,14 +3,11 @@ * WeHateCaptchas Self-Instantiating-Plugin * (c) 2020 Exceleron Designs, MIT License, https://excelerondesigns.com */ - -import emitter from './includes/emit'; +import Log from './includes/log'; import worker from './includes/worker'; import getSettings from './includes/get-settings'; (function (w) { - const e = emitter(); - /** @type {NodeListOf} */ const forms = document.querySelectorAll('[data-whc]'); @@ -22,7 +19,6 @@ import getSettings from './includes/get-settings'; */ // @ts-ignore w.whcWorkers = []; - /** * @param {HTMLFormElement} form * @param {number} i @@ -30,29 +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) { - /** - * @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, - verification: [], - progress: 0, - done: false, - }; - - /** @type { ( obj:import('./types').eventInterface ) => object } */ - const merge = obj => Object.assign(eventDefault, obj); - + const log = debug ? Log(form) : {}; /** @param {Function} fn */ function createWorker(fn) { try { @@ -61,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); } } @@ -76,48 +58,38 @@ import getSettings from './includes/get-settings'; difficulty, time, }); - e.run( - 'whc:Start#' + i, - merge({ - event: 'whc:Start#' + i, - }) - ); } - /** @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'); button.removeAttribute('disabled'); button.setAttribute('value', '' + finished); // @ts-ignore + log.info = { + title: 'Verified Form', + verification, + }; + // @ts-ignore w.whcWorkers[i].terminate(); } /** - * @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; form.dataset.progress = percent + '%'; - e.run( - 'whc:Progress#' + i, - merge({ - event: 'whc:Progress#' + i, - progress: +percent[0], - done: +percent[0] === 100, - }) - ); + // @ts-ignore + log.info = { + title: 'Progress Update', + percent: percent + '%', + }; } - - e.on('whc:Update#' + i, updatePercent); - e.on('whc:Complete#' + i, appendVerification); - /** * @this {Worker} * @param {object} param @@ -127,25 +99,10 @@ import getSettings from './includes/get-settings'; const { action, message, verification } = data; if (action === 'captchaSuccess') { - return e.run( - 'whc:Complete#' + i, - merge({ - event: 'whc:Complete#' + i, - verification, - done: true, - progress: 100, - }) - ); + return appendVerification(verification); } if (action === 'message') { - return e.run( - 'whc:Update#' + i, - merge({ - event: 'whc:Completed#' + i, - message, - progress: 0, - }) - ); + return updatePercent(message); } } 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;