Skip to content

Commit

Permalink
chore: created background helpers for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
juskek committed Jul 27, 2023
1 parent e44ed4d commit b1fd535
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 47 deletions.
54 changes: 7 additions & 47 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -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") {
Expand All @@ -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;
});
Expand Down
67 changes: 67 additions & 0 deletions src/background/backgroundHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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<void> => {
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;
});
};

0 comments on commit b1fd535

Please sign in to comment.