Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pubsub #40

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions src/includes/emit.js

This file was deleted.

52 changes: 52 additions & 0 deletions src/includes/log.js
Original file line number Diff line number Diff line change
@@ -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;
95 changes: 26 additions & 69 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLFormElement>} */
const forms = document.querySelectorAll('[data-whc]');

Expand All @@ -22,37 +19,14 @@ import getSettings from './includes/get-settings';
*/
// @ts-ignore
w.whcWorkers = [];

/**
* @param {HTMLFormElement} form
* @param {number} i
*/
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 {
Expand All @@ -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);
}
}
Expand All @@ -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', `<input type="hidden" name="captcha_verification" value='${JSON.stringify(verification)}' />`);
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
Expand All @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { Verification, EncodedMessage, WorkerResponse };
interface eventInterface {
event: string;
difficulty?: number | string;
form?: HTMLFormElement;
verification?: Verification[];
progress?: number;
done?: boolean;
Expand Down