Skip to content
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

Added chrome.debugger to monitor network requests #44

4 changes: 2 additions & 2 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"default_popup": "popup.html"
},
"permissions": [
"webRequest",
"tabs",
"storage"
"storage",
"debugger"
],
"host_permissions": [
"*://*/*/"
Expand Down
76 changes: 48 additions & 28 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { IBytesRepository } from "../data/bytes/IBytesRepository";
import {
catchPostRequestBodySize,
catchRequestHeaderSize,
catchResponseSize,
} from "./webRequestListeners";
import { addBytesTransferred } from "./backgroundHelpers";

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
if (message === "getBytesTransferred") {
sendResponse(IBytesRepository.instance.getBytesTransferred());
}
Expand All @@ -15,33 +11,57 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.command === "startRecordingBytesTransferred") {
IBytesRepository.instance.clearBytesTransferred();

chrome.webRequest.onBeforeRequest.addListener(
catchPostRequestBodySize,
{ urls: ["<all_urls>"], tabId },
["requestBody"]
);
chrome.webRequest.onBeforeSendHeaders.addListener(
catchRequestHeaderSize,
{ urls: ["<all_urls>"], tabId },
["requestHeaders"]
);
chrome.webRequest.onCompleted.addListener(
catchResponseSize,
{ urls: ["<all_urls>"], tabId },
["responseHeaders"]
);
chrome.debugger.attach({ tabId: tabId }, "1.2", () => {
chrome.debugger.sendCommand(
{ tabId: tabId },
"Network.enable",
{},
() => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
}
);
});

sendResponse(true);
}

if (message.command === "stopRecordingBytesTransferred") {
chrome.webRequest.onCompleted.removeListener(catchResponseSize);
chrome.webRequest.onBeforeRequest.removeListener(
catchPostRequestBodySize
);
chrome.webRequest.onBeforeSendHeaders.removeListener(
catchRequestHeaderSize
);
try {
await chrome.debugger.detach({ tabId: tabId });
} catch (e: unknown) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is e not an Error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is always going to be an error -
It's typed as unknown because I don't think typing in catch clauses are allowed yet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I'd probably remove the if condition and just type cast it as an Error

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;
}
sendResponse(true);
}
return true;
});

type NetworkParamsType = {
encodedDataLength?: number;
};

chrome.debugger.onEvent.addListener(
(source, method: string, params: NetworkParamsType | undefined) => {
switch (method) {
case "Network.loadingFinished": {
if (params?.encodedDataLength && params.encodedDataLength > 0) {
addBytesTransferred(params.encodedDataLength);
}
break;
}
default:
break;
}
}
);
25 changes: 25 additions & 0 deletions src/background/backgroundHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IBytesRepository } from "../data/bytes/IBytesRepository";

export const addBytesTransferred = async (bytes: number) => {
IBytesRepository.instance.addBytesTransferred(bytes);

try {
await chrome.runtime.sendMessage({
command: {
bytesTransferredChanged:
IBytesRepository.instance.getBytesTransferred(),
},
});
} catch (e: unknown) {
if (
(e as Error).message ===
"Could not establish connection. Receiving end does not exist."
) {
console.warn(
`Error Caught: ${e}\nIf popup is open and this error is seen in the console, debugging is required.`
);
} else {
throw e;
}
}
};
48 changes: 0 additions & 48 deletions src/background/getWebRequestSizeHelpers.ts

This file was deleted.

65 changes: 0 additions & 65 deletions src/background/webRequestListeners.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/view/popup/usePopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export const usePopup = () => {
}
try {
await calculationsRepository.setOngoingCalculation(true);
backgroundStopRecordingBytes();
} catch (e: unknown) {
if (e instanceof Error) {
setError(e.message);
Expand Down