Skip to content

Commit

Permalink
Use Harvest State when Spawning New Captcha Window
Browse files Browse the repository at this point in the history
This commit updates the Captcha Window Manager to automatically start harvesting on a new window if the rest of the windows are in the harvesting state. This allows you to open/close all captcha windows, but have new window harvest if a captcha is still required.

Issue: #106
  • Loading branch information
pr1sm committed Feb 22, 2019
1 parent 52c8323 commit 37c4edc
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions packages/frontend/lib/_electron/captchaWindowManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ class CaptchaWindowManager {
/**
* Current Harvest State
*/
this._harvestState = HARVEST_STATE.IDLE;
this._harvestStatus = {
state: HARVEST_STATE.IDLE,
runnerId: null,
siteKey: null,
};

this.validateSender = this.validateSender.bind(this);

Expand Down Expand Up @@ -107,10 +111,14 @@ class CaptchaWindowManager {
* If no captcha windows are present, one is created
*/
startHarvesting(runnerId, siteKey) {
this._harvestState = HARVEST_STATE.ACTIVE;
this._harvestStatus = {
state: HARVEST_STATE.ACTIVE,
runnerId,
siteKey,
};
if (this._captchaWindows === 0) {
const win = this.spawnCaptchaWindow();
win.webContents.on('did-finish-load', () => {
win.webContents.once('did-finish-load', () => {
win.webContents.send(IPCKeys.StartHarvestCaptcha, runnerId, siteKey);
});
} else {
Expand All @@ -127,10 +135,14 @@ class CaptchaWindowManager {
* harvest state to 'idle'
*/
stopHarvesting(runnerId, siteKey) {
this._harvestStatus = {
state: HARVEST_STATE.IDLE,
runnerId: null,
siteKey: null,
};
this._captchaWindows.forEach(win => {
win.webContents.send(IPCKeys.StopHarvestCaptcha, runnerId, siteKey);
});
this._harvestState = HARVEST_STATE.IDLE;
}

/**
Expand Down Expand Up @@ -169,6 +181,14 @@ class CaptchaWindowManager {
win.show();
});

// If we are actively harvesting, start harvesting on the new window as well
if (this._harvestStatus.state === HARVEST_STATE.ACTIVE) {
const { runnerId, siteKey } = this._harvestStatus;
win.webContents.once('did-finish-load', () => {
win.webContents.send(IPCKeys.StartHarvestCaptcha, runnerId, siteKey);
});
}

const handleClose = () => {
if (nebulaEnv.isDevelopment()) {
console.log(`[DEBUG]: Window was closed, id = ${winId}`);
Expand Down

0 comments on commit 37c4edc

Please sign in to comment.