Skip to content

Commit

Permalink
added notify feature for each recorded action
Browse files Browse the repository at this point in the history
Signed-off-by: aryangupta701 <[email protected]>
  • Loading branch information
aryangupta701 committed Aug 7, 2023
1 parent 8e0611a commit 3a7fa5c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 13 deletions.
2 changes: 2 additions & 0 deletions source/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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';
import {raiseNotification} from '../utils/notify';

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

Expand Down Expand Up @@ -217,6 +218,7 @@ async function handleMessage(
},
});
} else if (request.type === 'zestScript') {
raiseNotification(request.data);
const stmt = JSON.parse(request.data);
if (stmt.elementType === 'ZestClientElementSendKeys') {
console.log(stmt);
Expand Down
1 change: 1 addition & 0 deletions source/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"short_name": "ZAP",

"permissions": [
"notifications",
"tabs",
"cookies",
"storage"
Expand Down
43 changes: 30 additions & 13 deletions source/types/zestScript/ZestStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
DEFAULT_WINDOW_HANDLE,
ZEST_CLIENT_ELEMENT_CLEAR,
ZEST_CLIENT_ELEMENT_CLICK,
ZEST_CLIENT_ELEMENT_MOUSE_OVER,
ZEST_CLIENT_ELEMENT_SEND_KEYS,
ZEST_CLIENT_LAUNCH,
ZEST_CLIENT_SWITCH_TO_FRAME,
ZEST_CLIENT_WINDOW_CLOSE,
} from '../../utils/constants';

class ElementLocator {
type: string;

Expand Down Expand Up @@ -58,9 +69,9 @@ class ZestStatementLaunchBrowser extends ZestStatement {
constructor(
browserType: string,
url: string,
windowHandle = 'windowHandle1'
windowHandle = DEFAULT_WINDOW_HANDLE
) {
super('ZestClientLaunch');
super(ZEST_CLIENT_LAUNCH);
this.windowHandle = windowHandle;
this.browserType = browserType;
this.url = url;
Expand Down Expand Up @@ -92,8 +103,11 @@ abstract class ZestStatementElement extends ZestStatement {
}

class ZestStatementElementClick extends ZestStatementElement {
constructor(elementLocator: ElementLocator, windowHandle = 'windowHandle1') {
super('ZestClientElementClick', elementLocator);
constructor(
elementLocator: ElementLocator,
windowHandle = DEFAULT_WINDOW_HANDLE
) {
super(ZEST_CLIENT_ELEMENT_CLICK, elementLocator);
this.windowHandle = windowHandle;
}

Expand All @@ -114,9 +128,9 @@ class ZestStatementElementSendKeys extends ZestStatementElement {
constructor(
elementLocator: ElementLocator,
keys: string,
windowHandle = 'windowHandle1'
windowHandle = DEFAULT_WINDOW_HANDLE
) {
super('ZestClientElementSendKeys', elementLocator);
super(ZEST_CLIENT_ELEMENT_SEND_KEYS, elementLocator);
this.keys = keys;
this.windowHandle = windowHandle;
}
Expand All @@ -134,8 +148,11 @@ class ZestStatementElementSendKeys extends ZestStatementElement {
}

class ZestStatementElementClear extends ZestStatementElement {
constructor(elementLocator: ElementLocator, windowHandle = 'windowHandle1') {
super('ZestClientElementClear', elementLocator);
constructor(
elementLocator: ElementLocator,
windowHandle = DEFAULT_WINDOW_HANDLE
) {
super(ZEST_CLIENT_ELEMENT_CLEAR, elementLocator);
this.windowHandle = windowHandle;
}

Expand All @@ -155,8 +172,8 @@ class ZestStatementWindowClose extends ZestStatement {

windowHandle: string;

constructor(sleepInSeconds: number, windowHandle = 'windowHandle1') {
super('ZestClientWindowClose');
constructor(sleepInSeconds: number, windowHandle = DEFAULT_WINDOW_HANDLE) {
super(ZEST_CLIENT_WINDOW_CLOSE);
this.sleepInSeconds = sleepInSeconds;
this.windowHandle = windowHandle;
}
Expand All @@ -182,9 +199,9 @@ class ZestStatementSwichToFrame extends ZestStatement {
constructor(
frameIndex: number,
frameName = '',
windowHandle = 'windowHandle1'
windowHandle = DEFAULT_WINDOW_HANDLE
) {
super('ZestClientSwitchToFrame');
super(ZEST_CLIENT_SWITCH_TO_FRAME);
this.frameIndex = frameIndex;
this.frameName = frameName;
this.windowHandle = windowHandle;
Expand All @@ -205,7 +222,7 @@ class ZestStatementSwichToFrame extends ZestStatement {

class ZestStatementElementMouseOver extends ZestStatementElement {
constructor(elementLocator: ElementLocator) {
super('ZestMouseOverElement', elementLocator);
super(ZEST_CLIENT_ELEMENT_MOUSE_OVER, elementLocator);
}

toJSON(): string {
Expand Down
8 changes: 8 additions & 0 deletions source/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const ZEST_CLIENT_SWITCH_TO_FRAME = 'ZestClientSwitchToFrame';
export const ZEST_CLIENT_ELEMENT_CLICK = 'ZestClientElementClick';
export const ZEST_CLIENT_ELEMENT_SEND_KEYS = 'ZestClientElementSendKeys';
export const ZEST_CLIENT_LAUNCH = 'ZestClientLaunch';
export const ZEST_CLIENT_ELEMENT_CLEAR = 'ZestClientElementClear';
export const ZEST_CLIENT_WINDOW_CLOSE = 'ZestClientWindowClose';
export const ZEST_CLIENT_ELEMENT_MOUSE_OVER = 'ZestClientMouseOverElement';
export const DEFAULT_WINDOW_HANDLE = 'windowHandle1';
48 changes: 48 additions & 0 deletions source/utils/notify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Browser, {Notifications} from 'webextension-polyfill';
import {
ZEST_CLIENT_ELEMENT_CLICK,
ZEST_CLIENT_ELEMENT_SEND_KEYS,
ZEST_CLIENT_SWITCH_TO_FRAME,
} from './constants';

function clearNotification(notifyId: string): void {
setTimeout(() => {
Browser.notifications.clear(notifyId);
}, 2000);
}

export const raiseNotification = async (data: string): Promise<void> => {
try {
let title: string;
let message: string;
const statement = JSON.parse(data);
switch (statement.elementType) {
case ZEST_CLIENT_ELEMENT_CLICK:
title = 'Click Recorded';
message = `Path Type: ${statement.type} Element: ${statement.element}`;
break;
case ZEST_CLIENT_ELEMENT_SEND_KEYS:
title = `Input Recorded -> ${statement.value}`;
message = `Path Type: ${statement.type} Element: ${statement.element}`;
break;
case ZEST_CLIENT_SWITCH_TO_FRAME:
title = 'Frame Switch Recorded';
message = `Frame Index: ${statement.frameIndex}`;
break;
default:
// Don't raise notifcation
return;
}
const options: Notifications.CreateNotificationOptions = {
type: 'basic',
title,
iconUrl: '../assets/icons/zap32x32.png',
message,
};

const notifyId = await Browser.notifications.create(options);
clearNotification(notifyId);
} catch (e) {
console.log(e);
}
};

0 comments on commit 3a7fa5c

Please sign in to comment.