Skip to content

Commit

Permalink
Extract constants from string literals (#65)
Browse files Browse the repository at this point in the history
Extract constants from string literals.

Signed-off-by: aryangupta701 <[email protected]>
  • Loading branch information
aryangupta701 authored Aug 23, 2023
1 parent f1dd455 commit f4bd972
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 52 deletions.
41 changes: 27 additions & 14 deletions source/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ 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 {
LOCAL_STORAGE,
REPORT_EVENT,
REPORT_OBJECT,
RESET_ZEST_SCRIPT,
SAVE_ZEST_SCRIPT,
SESSION_STORAGE,
SET_SAVE_SCRIPT_ENABLE,
STOP_RECORDING,
ZEST_CLIENT_ELEMENT_CLEAR,
ZEST_CLIENT_ELEMENT_SEND_KEYS,
ZEST_SCRIPT,
} from '../utils/constants';

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

Expand Down Expand Up @@ -121,7 +134,7 @@ function reportCookies(
repStorage.toString()
)}&apikey=${encodeURIComponent(zapkey)}`;

fetch(zapApiUrl(zapurl, 'reportObject'), {
fetch(zapApiUrl(zapurl, REPORT_OBJECT), {
method: 'POST',
body,
headers: {
Expand Down Expand Up @@ -175,14 +188,14 @@ async function handleMessage(
}

console.log(`ZAP Service worker calling ZAP on ${zapurl}`);
console.log(zapApiUrl(zapurl, 'reportObject'));
console.log(zapApiUrl(zapurl, REPORT_OBJECT));
console.log(encodeURIComponent(zapkey));
console.log(`Type: ${request.type}`);
console.log(`Data: ${request.data}`);

if (request.type === 'reportObject') {
if (request.type === REPORT_OBJECT) {
const repObj = JSON.parse(request.data);
if (repObj.type === 'localStorage' || repObj.type === 'sessionStorage') {
if (repObj.type === LOCAL_STORAGE || repObj.type === SESSION_STORAGE) {
// Check to see if we have already reported this storage object
const repStorage = new ReportedStorage('', '', '', '', '');
Object.assign(repStorage, repObj);
Expand All @@ -197,41 +210,41 @@ async function handleMessage(
request.data
)}&apikey=${encodeURIComponent(zapkey)}`;
console.log(`body = ${body}`);
fetch(zapApiUrl(zapurl, 'reportObject'), {
fetch(zapApiUrl(zapurl, REPORT_OBJECT), {
method: 'POST',
body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
} else if (request.type === 'reportEvent') {
} else if (request.type === REPORT_EVENT) {
const body = `eventJson=${encodeURIComponent(
request.data
)}&apikey=${encodeURIComponent(zapkey)}`;
console.log(`body = ${body}`);
fetch(zapApiUrl(zapurl, 'reportEvent'), {
fetch(zapApiUrl(zapurl, REPORT_EVENT), {
method: 'POST',
body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
} else if (request.type === 'zestScript') {
} else if (request.type === ZEST_SCRIPT) {
const stmt = JSON.parse(request.data);
if (stmt.elementType === 'ZestClientElementSendKeys') {
if (stmt.elementType === ZEST_CLIENT_ELEMENT_SEND_KEYS) {
console.log(stmt);
stmt.elementType = 'ZestClientElementClear';
stmt.elementType = ZEST_CLIENT_ELEMENT_CLEAR;
delete stmt.value;
const cleardata = zestScript.addStatement(JSON.stringify(stmt));
sendZestScriptToZAP(cleardata, zapkey, zapurl);
}
const data = zestScript.addStatement(request.data);
sendZestScriptToZAP(data, zapkey, zapurl);
} else if (request.type === 'saveZestScript') {
} else if (request.type === SAVE_ZEST_SCRIPT) {
return zestScript.getZestScript();
} else if (request.type === 'resetZestScript') {
} else if (request.type === RESET_ZEST_SCRIPT) {
zestScript.reset();
} else if (request.type === 'stopRecording') {
} else if (request.type === STOP_RECORDING) {
if (zestScript.getZestStatementCount() > 0) {
const {zapclosewindowhandle} = await Browser.storage.sync.get({
zapclosewindowhandle: false,
Expand All @@ -242,7 +255,7 @@ async function handleMessage(
sendZestScriptToZAP(data, zapkey, zapurl);
}
}
} else if (request.type === 'setSaveScriptEnable') {
} else if (request.type === SET_SAVE_SCRIPT_ENABLE) {
Browser.storage.sync.set({
zapenablesavescript: zestScript.getZestStatementCount() > 0,
});
Expand Down
34 changes: 22 additions & 12 deletions source/ContentScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ import {
ReportedEvent,
} from '../types/ReportedModel';
import Recorder from './recorder';
import {
LOCAL_STORAGE,
LOCAL_ZAP_ENABLE,
LOCAL_ZAP_URL,
REPORT_EVENT,
REPORT_OBJECT,
SESSION_STORAGE,
ZAP_START_RECORDING,
ZAP_STOP_RECORDING,
} from '../utils/constants';

const reportedObjects = new Set<string>();

Expand All @@ -44,14 +54,14 @@ function reportStorage(

async function sendEventToZAP(obj: ReportedEvent): Promise<number> {
return Browser.runtime.sendMessage({
type: 'reportEvent',
type: REPORT_EVENT,
data: obj.toString(),
});
}

async function sendObjectToZAP(obj: ReportedObject): Promise<number> {
return Browser.runtime.sendMessage({
type: 'reportObject',
type: REPORT_OBJECT,
data: obj.toString(),
});
}
Expand All @@ -65,8 +75,8 @@ function reportObject(repObj: ReportedObject): void {
}

function reportAllStorage(): void {
reportStorage('localStorage', localStorage, reportObject);
reportStorage('sessionStorage', sessionStorage, reportObject);
reportStorage(LOCAL_STORAGE, localStorage, reportObject);
reportStorage(SESSION_STORAGE, sessionStorage, reportObject);
}

function withZapEnableSetting(fn: () => void): void {
Expand All @@ -88,7 +98,7 @@ function withZapRecordingActive(fn: () => void): void {
function reportPageUnloaded(): void {
withZapEnableSetting(() => {
Browser.runtime.sendMessage({
type: 'reportEvent',
type: REPORT_EVENT,
data: new ReportedEvent('pageUnload').toString(),
});
for (const value of Object.values(reportedEvents)) {
Expand Down Expand Up @@ -173,16 +183,16 @@ function reportPageLoaded(
}

Browser.runtime.sendMessage({
type: 'reportEvent',
type: REPORT_EVENT,
data: new ReportedEvent('pageLoad').toString(),
});

reportPageLinks(doc, fn);
reportPageForms(doc, fn);
reportElements(doc.getElementsByTagName('input'), fn);
reportElements(doc.getElementsByTagName('button'), fn);
reportStorage('localStorage', localStorage, fn);
reportStorage('sessionStorage', sessionStorage, fn);
reportStorage(LOCAL_STORAGE, localStorage, fn);
reportStorage(SESSION_STORAGE, sessionStorage, fn);
}

const domMutated = function domMutation(
Expand Down Expand Up @@ -225,8 +235,8 @@ function enableExtension(): void {
}

function configureExtension(): void {
const localzapurl = localStorage.getItem('localzapurl');
const localzapenable = localStorage.getItem('localzapenable') || true;
const localzapurl = localStorage.getItem(LOCAL_ZAP_URL);
const localzapenable = localStorage.getItem(LOCAL_ZAP_ENABLE) || true;
if (localzapurl) {
Browser.storage.sync.set({
zapurl: localzapurl,
Expand All @@ -253,11 +263,11 @@ injectScript();

Browser.runtime.onMessage.addListener(
(message: MessageEvent, _sender: Runtime.MessageSender) => {
if (message.type === 'zapStartRecording') {
if (message.type === ZAP_START_RECORDING) {
configureExtension();
recorder.initializationScript();
recorder.recordUserInteractions();
} else if (message.type === 'zapStopRecording') {
} else if (message.type === ZAP_STOP_RECORDING) {
recorder.stopRecordingUserInteractions();
}
}
Expand Down
3 changes: 2 additions & 1 deletion source/ContentScript/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ZestStatementSwichToFrame,
} from '../types/zestScript/ZestStatement';
import {getPath} from './util';
import {ZEST_SCRIPT} from '../utils/constants';

class Recorder {
previousDOMState: string;
Expand All @@ -41,7 +42,7 @@ class Recorder {

async sendZestScriptToZAP(zestStatement: ZestStatement): Promise<number> {
return Browser.runtime.sendMessage({
type: 'zestScript',
type: ZEST_SCRIPT,
data: zestStatement.toJSON(),
});
}
Expand Down
5 changes: 1 addition & 4 deletions source/Options/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
* limitations under the License.
*/
import Browser from 'webextension-polyfill';
import {ZAP_ENABLE, ZAP_KEY, ZAP_URL} from '../utils/constants';

console.log('Options loading');

const ZAP_URL = 'zapurl';
const ZAP_KEY = 'zapkey';
const ZAP_ENABLE = 'zapenable';

// Saves options to chrome.storage
function saveOptions(): void {
console.log('Options save_options');
Expand Down
25 changes: 17 additions & 8 deletions source/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
import Browser from 'webextension-polyfill';
import './styles.scss';
import i18n from './i18n';
import {
RESET_ZEST_SCRIPT,
SAVE_ZEST_SCRIPT,
SET_SAVE_SCRIPT_ENABLE,
STOP_RECORDING,
UPDATE_TITLE,
ZAP_START_RECORDING,
ZAP_STOP_RECORDING,
} from '../utils/constants';

const STOP = i18n.t('stop');
const START = i18n.t('start');
Expand Down Expand Up @@ -73,7 +82,7 @@ function startedAnimation(): void {

async function restoreState(): Promise<void> {
console.log('Restore state');
await Browser.runtime.sendMessage({type: 'setSaveScriptEnable'});
await Browser.runtime.sendMessage({type: SET_SAVE_SCRIPT_ENABLE});
optionsIcon.title = OPTIONS;
downloadIcon.title = DOWNLOAD;
Browser.storage.sync
Expand Down Expand Up @@ -111,17 +120,17 @@ function closePopup(): void {
function stopRecording(): void {
console.log('Recording stopped ...');
stoppedAnimation();
sendMessageToContentScript('zapStopRecording');
Browser.runtime.sendMessage({type: 'stopRecording'});
sendMessageToContentScript(ZAP_STOP_RECORDING);
Browser.runtime.sendMessage({type: STOP_RECORDING});
Browser.storage.sync.set({
zaprecordingactive: false,
});
}

function startRecording(): void {
startedAnimation();
sendMessageToContentScript('zapStartRecording');
Browser.runtime.sendMessage({type: 'resetZestScript'});
sendMessageToContentScript(ZAP_START_RECORDING);
Browser.runtime.sendMessage({type: RESET_ZEST_SCRIPT});
Browser.storage.sync.set({
zaprecordingactive: true,
});
Expand Down Expand Up @@ -165,15 +174,15 @@ function downloadZestScript(zestScriptJSON: string, title: string): void {
document.body.removeChild(link);

URL.revokeObjectURL(url);
Browser.runtime.sendMessage({type: 'resetZestScript'});
Browser.runtime.sendMessage({type: RESET_ZEST_SCRIPT});
Browser.storage.sync.set({
zaprecordingactive: false,
});
closePopup();
}

function handleSaveScript(): void {
Browser.runtime.sendMessage({type: 'saveZestScript'}).then((items) => {
Browser.runtime.sendMessage({type: SAVE_ZEST_SCRIPT}).then((items) => {
downloadZestScript(items.script, items.title);
});
}
Expand All @@ -183,7 +192,7 @@ function handleScriptNameChange(e: Event): void {
Browser.storage.sync.set({
zapscriptname: value,
});
sendMessageToContentScript('updateTitle', value);
sendMessageToContentScript(UPDATE_TITLE, value);
}

document.addEventListener('DOMContentLoaded', restoreState);
Expand Down
Loading

0 comments on commit f4bd972

Please sign in to comment.