From b1fd53543775bbc001255835036fa136a04848a0 Mon Sep 17 00:00:00 2001 From: Justin <57725347+juskek@users.noreply.github.com> Date: Thu, 27 Jul 2023 09:56:18 +0100 Subject: [PATCH] chore: created background helpers for readability --- src/background/background.ts | 54 +++-------------------- src/background/backgroundHelpers.ts | 67 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 47 deletions(-) diff --git a/src/background/background.ts b/src/background/background.ts index fc895c5d..46ec8ec4 100644 --- a/src/background/background.ts +++ b/src/background/background.ts @@ -1,5 +1,9 @@ import { IBytesRepository } from "../data/bytes/IBytesRepository"; -import { addBytesTransferred } from "./backgroundHelpers"; +import { + addBytesTransferred, + startRecordingBytesTransferred, + stopRecordingBytesTransferred, +} from "./backgroundHelpers"; chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message === "getBytesTransferred") { @@ -9,55 +13,11 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { const { tabId } = message; if (message.command === "startRecordingBytesTransferred") { - IBytesRepository.instance.clearBytesTransferred(); - - chrome.tabs.get(tabId).then((tab) => { - if (tab.url && tab.url.startsWith("chrome://")) { - sendResponse({ - success: false, - message: - "Cannot calculate emissions for a chrome:// URL, e.g. manage extensions page. Please try again on a valid webpage.", - }); - } else { - chrome.debugger.attach({ tabId: tabId }, "1.2").then(() => { - chrome.debugger.sendCommand( - { tabId: tabId }, - "Network.enable", - {}, - () => { - if (chrome.runtime.lastError) { - console.error(chrome.runtime.lastError); - } - sendResponse({ - success: true, - message: - "Successfully started recording bytes transferred.", - }); - } - ); - }); - } - }); + startRecordingBytesTransferred(tabId, sendResponse); } if (message.command === "stopRecordingBytesTransferred") { - chrome.debugger - .detach({ tabId: tabId }) - .then(() => { - sendResponse(true); - }) - .catch((e: unknown) => { - if ( - (e as Error).message === - `Debugger is not attached to the tab with id: ${tabId}.` - ) { - console.warn( - `Tried to detach debugger from tab (tabId: ${tabId}) when there was none attached. ` - ); - return; - } - throw e; - }); + stopRecordingBytesTransferred(tabId, sendResponse); } return true; }); diff --git a/src/background/backgroundHelpers.ts b/src/background/backgroundHelpers.ts index 7061dcae..b67f0b7c 100644 --- a/src/background/backgroundHelpers.ts +++ b/src/background/backgroundHelpers.ts @@ -23,3 +23,70 @@ export const addBytesTransferred = async (bytes: number) => { } } }; + +export type StartRecordingBytesTransferredReturnType = { + success: boolean; + message: string; +}; + +export const startRecordingBytesTransferred = async ( + tabId: number, + sendResponse: (response: StartRecordingBytesTransferredReturnType) => void +): Promise => { + IBytesRepository.instance.clearBytesTransferred(); + + chrome.tabs.get(tabId).then((tab) => { + if (tab.url && tab.url.startsWith("chrome://")) { + sendResponse({ + success: false, + message: + "Cannot calculate emissions for a chrome:// URL, e.g. manage extensions page. Please try again on a valid webpage.", + }); + return; + } else { + chrome.debugger.attach({ tabId }, "1.2").then(() => { + chrome.debugger.sendCommand( + { tabId }, + "Network.enable", + {}, + () => { + if (chrome.runtime.lastError) { + console.error(chrome.runtime.lastError); + } + sendResponse({ + success: true, + message: + "Successfully started recording bytes transferred.", + }); + return; + } + ); + }); + } + }); +}; + +export const stopRecordingBytesTransferred = async ( + tabId: number, + sendResponse: (response: boolean) => void +): Promise => { + chrome.debugger + .detach({ tabId }) + .then(() => { + sendResponse(true); + return; + }) + .catch((e: unknown) => { + if ( + (e as Error).message === + `Debugger is not attached to the tab with id: ${tabId}.` + ) { + console.warn( + `Tried to detach debugger from tab (tabId: ${tabId}) when there was none attached. ` + ); + sendResponse(false); + return; + } + throw e; + }); +};