- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 42
feat(#161): refactor string literals to typed enums #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 10 commits
60114ba
3d6fef1
2234bcf
d46f0af
8a5e8fc
4dd6a8b
686d425
42149d8
6a5af94
59caa47
a7e15e6
c46874f
17c871a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
**/dist | ||
**/build | ||
**/dist-zip | ||
chrome-extension/manifest.js | ||
chrome-extension | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @naaa760, |
||
chrome-extension/pre-build.tsconfig.tsbuildinfo | ||
tsconfig.tsbuildinfo | ||
|
||
|
@@ -19,7 +19,7 @@ tsconfig.tsbuildinfo | |
# etc | ||
.DS_Store | ||
.idea | ||
**/.turbo | ||
|
||
|
||
# compiled | ||
**/tailwind-output.css |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @naaa760, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but i have not modify the .turbo folder at all. |
||
"is_task_list_visible": true, | ||
"active_task": null | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import { deepRedactSensitiveInfo } from '@extension/shared'; | ||
import { deepRedactSensitiveInfo, RecordType } from '@extension/shared'; | ||
|
||
const restricted = ['https://api.briehq.com']; // 'extend.iife', 'kbmbnelnoppneadncmmkfikbcgmilbao' Note: it blocks the logs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see some differences between current changes in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @naaa760, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @naaa760, |
||
const invalidRecord = (entity: string) => restricted.some(word => entity.includes(word)); | ||
|
||
const tabRecordsMap = new Map<number, Map<string, any>>(); | ||
|
||
export type RecordType = 'events' | 'network' | 'console' | 'cookies'; | ||
|
||
export interface Record { | ||
recordType: RecordType; | ||
url?: string; | ||
|
@@ -50,7 +48,7 @@ export const addOrMergeRecords = async (tabId: number, record: Record | any): Pr | |
const uuid = uuidv4(); | ||
|
||
try { | ||
if (record.recordType !== 'network') { | ||
if (record.recordType !== RecordType.NETWORK) { | ||
recordsMap.set(uuid, { uuid, ...deepRedactSensitiveInfo(record, tabUrl) }); | ||
return; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum CaptureState { | ||
IDLE = 'idle', | ||
CAPTURING = 'capturing', | ||
UNSAVED = 'unsaved', | ||
} | ||
|
||
export enum CaptureType { | ||
AREA = 'area', | ||
FULL_PAGE = 'full-page', | ||
VIEWPORT = 'viewport', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export enum InstallReason { | ||
INSTALL = 'install', | ||
UPDATE = 'update', | ||
} | ||
|
||
export enum ContextType { | ||
ALL = 'all', | ||
} | ||
|
||
export enum ContextMenuId { | ||
CAPTURE_PARENT = 'capture_parent', | ||
} | ||
|
||
export enum ResponseStatus { | ||
SUCCESS = 'success', | ||
ERROR = 'error', | ||
} | ||
|
||
export enum ImageFormat { | ||
JPEG = 'jpeg', | ||
PNG = 'png', | ||
} | ||
|
||
export enum RequestProperty { | ||
REQUEST_BODY = 'requestBody', | ||
REQUEST_HEADERS = 'requestHeaders', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export enum EventType { | ||
NAVIGATE = 'Navigate', | ||
DOM_CONTENT_LOADED = 'DOMContentLoaded', | ||
PAGE_LOADED = 'PageLoaded', | ||
TAB_HIDDEN = 'TabHidden', | ||
TAB_VISIBLE = 'TabVisible', | ||
INPUT_CHANGE = 'InputChange', | ||
SELECT_OPTION = 'SelectOption', | ||
RESIZE = 'Resize', | ||
CAPTURE = 'capture', | ||
} | ||
|
||
export enum CustomEventType { | ||
METADATA = 'metadata', | ||
CLOSE_MODAL = 'CLOSE_MODAL', | ||
DISPLAY_MODAL = 'DISPLAY_MODAL', | ||
} | ||
|
||
export enum EventDomain { | ||
SCREENSHOT = 'screenshot', | ||
} | ||
|
||
export enum NavigationMethod { | ||
PUSH_STATE = 'pushState', | ||
REPLACE_STATE = 'replaceState', | ||
POP_STATE = 'popstate', | ||
} | ||
|
||
export enum VisibilityState { | ||
HIDDEN = 'hidden', | ||
VISIBLE = 'visible', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export enum MessageType { | ||
EXIT_CAPTURE = 'EXIT_CAPTURE', | ||
ADD_RECORD = 'ADD_RECORD', | ||
GET_RECORDS = 'GET_RECORDS', | ||
} | ||
|
||
export enum MessageAction { | ||
CHECK_NATIVE_CAPTURE = 'checkNativeCapture', | ||
CAPTURE_VISIBLE_TAB = 'captureVisibleTab', | ||
START_SCREENSHOT = 'START_SCREENSHOT', | ||
EXIT_CAPTURE = 'EXIT_CAPTURE', | ||
CLOSE_MODAL = 'CLOSE_MODAL', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export enum RecordType { | ||
EVENTS = 'events', | ||
NETWORK = 'network', | ||
CONSOLE = 'console', | ||
COOKIES = 'cookies', | ||
LOCAL_STORAGE = 'local-storage', | ||
SESSION_STORAGE = 'session-storage', | ||
} | ||
|
||
export enum RecordSource { | ||
BACKGROUND = 'background', | ||
CONTENT_SCRIPT = 'content-script', | ||
CLIENT = 'client', | ||
} | ||
|
||
export enum LogMethod { | ||
ERROR = 'error', | ||
WARN = 'warn', | ||
INFO = 'info', | ||
LOG = 'log', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
import { safePostMessage } from '@extension/shared'; | ||
import { safePostMessage, MessageType, RecordType, RecordSource } from '@extension/shared'; | ||
|
||
// Get all localStorage data | ||
export const interceptLocalStorage = () => { | ||
const timestamp = Date.now(); | ||
const localStorageData = []; | ||
try { | ||
const timestamp = Date.now(); | ||
const localStorageData: Record<string, string> = {}; | ||
|
||
for (let i = 0; i < localStorage.length; i++) { | ||
const key = localStorage.key(i); | ||
if (!key) continue; // Skip null keys | ||
// Get all keys from localStorage | ||
for (let i = 0; i < localStorage.length; i++) { | ||
const key = localStorage.key(i); | ||
if (key) { | ||
localStorageData[key] = localStorage.getItem(key) || ''; | ||
} | ||
} | ||
|
||
const value = localStorage.getItem(key); | ||
localStorageData.push({ | ||
key, | ||
value, | ||
// post message to background/content | ||
safePostMessage(MessageType.ADD_RECORD, { | ||
timestamp, | ||
recordType: RecordType.LOCAL_STORAGE, | ||
source: RecordSource.CLIENT, | ||
items: localStorageData, | ||
}); | ||
} catch (error) { | ||
console.error('Error accessing localStorage:', error); | ||
} | ||
|
||
safePostMessage('ADD_RECORD', { timestamp, recordType: 'local-storage', source: 'client', items: localStorageData }); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
import { safePostMessage } from '@extension/shared'; | ||
import { safePostMessage, MessageType, RecordType, RecordSource } from '@extension/shared'; | ||
|
||
// Get all sessionStorage data | ||
export const interceptSessionStorage = () => { | ||
const timestamp = Date.now(); | ||
const sessionStorageData = []; | ||
try { | ||
const timestamp = Date.now(); | ||
const sessionStorageData: Record<string, string> = {}; | ||
|
||
for (let i = 0; i < sessionStorage.length; i++) { | ||
const key = sessionStorage.key(i); | ||
if (!key) continue; // Skip null keys | ||
// Get all keys from sessionStorage | ||
for (let i = 0; i < sessionStorage.length; i++) { | ||
const key = sessionStorage.key(i); | ||
if (key) { | ||
sessionStorageData[key] = sessionStorage.getItem(key) || ''; | ||
} | ||
} | ||
|
||
const value = sessionStorage.getItem(key); | ||
sessionStorageData.push({ | ||
key, | ||
value, | ||
// post message to background/content | ||
safePostMessage(MessageType.ADD_RECORD, { | ||
timestamp, | ||
recordType: RecordType.SESSION_STORAGE, | ||
source: RecordSource.CLIENT, | ||
items: sessionStorageData, | ||
}); | ||
} catch (error) { | ||
console.error('Error accessing sessionStorage:', error); | ||
} | ||
|
||
safePostMessage('ADD_RECORD', { | ||
timestamp, | ||
recordType: 'session-storage', | ||
source: 'client', | ||
items: sessionStorageData, | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { safePostMessage } from '@extension/shared'; | ||
import { safePostMessage, MessageType, RecordType, RecordSource, LogMethod } from '@extension/shared'; | ||
|
||
// Define interfaces for request details and payload | ||
interface RequestDetails { | ||
|
@@ -23,15 +23,17 @@ export const interceptXHR = (): void => { | |
this: ExtendedXMLHttpRequest, | ||
method: string, | ||
url: string | URL, | ||
...rest: any[] | ||
async: boolean = true, | ||
username?: string | null, | ||
password?: string | null, | ||
): void { | ||
this._requestDetails = { | ||
method, | ||
url: url.toString(), | ||
requestStart: new Date().toISOString(), | ||
requestBody: null, | ||
}; | ||
originalOpen.apply(this, [method, url, ...rest]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @naaa760, can you please elaborate a bit on this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change refactors the XHR interceptor to use the new enums (RecordType, RecordSource, LogMethod) instead of hardcoded strings like 'network', 'client', and 'error' for better type safety. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @naaa760 |
||
originalOpen.apply(this, [method, url, async, username, password]); | ||
}; | ||
|
||
// Intercept XMLHttpRequest send method | ||
|
@@ -45,7 +47,7 @@ export const interceptXHR = (): void => { | |
|
||
const originalOnReadyStateChange = this.onreadystatechange; | ||
|
||
this.onreadystatechange = function (this: ExtendedXMLHttpRequest, ...args: any[]): void { | ||
this.onreadystatechange = function (this: ExtendedXMLHttpRequest, ev: Event): void { | ||
if (this.readyState === 4 && this._requestDetails) { | ||
// Request completed | ||
const endTime = new Date().toISOString(); | ||
|
@@ -93,20 +95,20 @@ export const interceptXHR = (): void => { | |
responseBody, | ||
}; | ||
|
||
safePostMessage('ADD_RECORD', { | ||
safePostMessage(MessageType.ADD_RECORD, { | ||
recordType: RecordType.NETWORK, | ||
source: RecordSource.CLIENT, | ||
timestamp, | ||
recordType: 'network', | ||
source: 'client', | ||
...payload, | ||
}); | ||
|
||
if (this.status >= 400) { | ||
safePostMessage('ADD_RECORD', { | ||
type: 'log', | ||
recordType: 'console', | ||
source: 'client', | ||
method: 'error', | ||
timestamp: Date.now(), | ||
safePostMessage(MessageType.ADD_RECORD, { | ||
timestamp, | ||
type: RecordType.CONSOLE, | ||
recordType: RecordType.CONSOLE, | ||
source: RecordSource.CLIENT, | ||
method: LogMethod.ERROR, | ||
args: [ | ||
`[XHR] ${this._requestDetails.method} ${this._requestDetails.url} responded with status ${this.status}`, | ||
payload, | ||
|
@@ -128,7 +130,7 @@ export const interceptXHR = (): void => { | |
|
||
// Call the original onreadystatechange handler if defined | ||
if (originalOnReadyStateChange) { | ||
originalOnReadyStateChange.apply(this, args); | ||
originalOnReadyStateChange.call(this, ev); | ||
} | ||
}; | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.