From 276c1c7629253d2c212ca9d6137c3bb8aa113d2d Mon Sep 17 00:00:00 2001 From: aryangupta701 Date: Thu, 20 Jul 2023 23:30:11 +0530 Subject: [PATCH] closes windowhandle Signed-off-by: aryangupta701 --- source/Background/index.ts | 37 +++++++++++++++++------- source/ContentScript/index.ts | 1 + source/types/zestScript/ZestScript.ts | 14 +++++++-- source/types/zestScript/ZestStatement.ts | 23 +++++++++++++++ 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/source/Background/index.ts b/source/Background/index.ts index eb56ce8..0f48f28 100644 --- a/source/Background/index.ts +++ b/source/Background/index.ts @@ -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 👋'); @@ -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, @@ -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; } diff --git a/source/ContentScript/index.ts b/source/ContentScript/index.ts index 58659b6..6cf2fda 100644 --- a/source/ContentScript/index.ts +++ b/source/ContentScript/index.ts @@ -256,6 +256,7 @@ Browser.runtime.onMessage.addListener( recordUserInteractions(); } else if (message.type === 'zapStopRecording') { stopRecordingUserInteractions(); + Browser.runtime.sendMessage({type: 'stopRecording'}); } } ); diff --git a/source/types/zestScript/ZestScript.ts b/source/types/zestScript/ZestScript.ts index 6703fde..a4488c3 100644 --- a/source/types/zestScript/ZestScript.ts +++ b/source/types/zestScript/ZestScript.ts @@ -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; @@ -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( { diff --git a/source/types/zestScript/ZestStatement.ts b/source/types/zestScript/ZestStatement.ts index 8b79616..1f3d7dc 100644 --- a/source/types/zestScript/ZestStatement.ts +++ b/source/types/zestScript/ZestStatement.ts @@ -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; @@ -150,4 +172,5 @@ export { ZestStatementElementClick, ZestStatementSwichToFrame, ZestStatementElementSendKeys, + ZestStatementWindowClose, };