Skip to content

Commit

Permalink
closes windowhandle
Browse files Browse the repository at this point in the history
Signed-off-by: aryangupta701 <garyan447@gmail.com>
  • Loading branch information
aryangupta701 committed Jul 20, 2023
1 parent 0576461 commit 276c1c7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
37 changes: 26 additions & 11 deletions source/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'emoji-log';
import Browser, {Cookies, Runtime} from 'webextension-polyfill';
import {ReportedStorage} from '../types/ReportedModel';
import {ZestScript, ZestScriptMessage} from '../types/zestScript/ZestScript';
import {ZestStatementWindowClose} from '../types/zestScript/ZestStatement';

console.log('ZAP Service Worker 👋');

Expand Down Expand Up @@ -138,6 +139,24 @@ function reportCookies(
return true;
}

function sendZestScriptToZAP(
data: string,
zapkey: string,
zapurl: string
): void {
const body = `scriptJson=${encodeURIComponent(
data
)}&apikey=${encodeURIComponent(zapkey)}`;
console.log(`body = ${body}`);
fetch(zapApiUrl(zapurl, 'reportZestScript'), {
method: 'POST',
body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
}

function handleMessage(
request: MessageEvent,
zapurl: string,
Expand Down Expand Up @@ -198,21 +217,17 @@ function handleMessage(
});
} else if (request.type === 'zestScript') {
const data = zestScript.addStatement(request.data);
const body = `scriptJson=${encodeURIComponent(
data
)}&apikey=${encodeURIComponent(zapkey)}`;
console.log(`body = ${body}`);
fetch(zapApiUrl(zapurl, 'reportZestScript'), {
method: 'POST',
body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
sendZestScriptToZAP(data, zapkey, zapurl);
} else if (request.type === 'saveZestScript') {
return zestScript.getZestScript();
} else if (request.type === 'resetZestScript') {
zestScript.reset();
} else if (request.type === 'stopRecording') {
if (zestScript.getZestStatementCount() > 0) {
const stmt = new ZestStatementWindowClose(0);
const data = zestScript.addStatement(stmt.toJSON());
sendZestScriptToZAP(data, zapkey, zapurl);
}
}
return true;
}
Expand Down
1 change: 1 addition & 0 deletions source/ContentScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ Browser.runtime.onMessage.addListener(
recordUserInteractions();
} else if (message.type === 'zapStopRecording') {
stopRecordingUserInteractions();
Browser.runtime.sendMessage({type: 'stopRecording'});
}
}
);
Expand Down
14 changes: 11 additions & 3 deletions source/types/zestScript/ZestScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ interface ZestScriptMessage {
}

class ZestScript {
zestStatements: string[] = [];
private zestStatements: string[] = [];

curIndex = 1;
private curIndex = 1;

title: string;
private title: string;

constructor(title = '') {
this.title = title;
Expand All @@ -31,6 +31,14 @@ class ZestScript {
this.curIndex = 1;
}

getZestStatementCount(): number {
return this.zestStatements.length;
}

getTitle(): string {
return this.title;
}

toJSON(): string {
return JSON.stringify(
{
Expand Down
23 changes: 23 additions & 0 deletions source/types/zestScript/ZestStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ class ZestStatementElementSendKeys extends ZestStatementElement {
}
}

class ZestStatementWindowClose extends ZestStatement {
sleepInSeconds: number;

windowHandle: string;

constructor(sleepInSeconds: number, windowHandle = 'windowHandle1') {
super('ZestClientWindowClose');
this.sleepInSeconds = sleepInSeconds;
this.windowHandle = windowHandle;
}

toJSON(): string {
return JSON.stringify({
windowHandle: this.windowHandle,
index: this.index,
sleepInSeconds: this.sleepInSeconds,
enabled: true,
elementType: this.elementType,
});
}
}

class ZestStatementSwichToFrame extends ZestStatement {
frameIndex: number;

Expand Down Expand Up @@ -150,4 +172,5 @@ export {
ZestStatementElementClick,
ZestStatementSwichToFrame,
ZestStatementElementSendKeys,
ZestStatementWindowClose,
};

0 comments on commit 276c1c7

Please sign in to comment.