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

Conversation

juskek
Copy link
Contributor

@juskek juskek commented Jul 19, 2023

Previous accuracy with chrome.webRequests:

stackoverflow.com | -5.27% error
github.com | 17.81% error
theodo.co.uk | -30.43% error
youtube.com |  -120.29% error
Forge Platform | -43.36% error
Trello | -111.11% error

With chrome.debugger:

Stack overflow | super accurate
Github | super accurate
Theodo | -4% error 
Youtube | pretty accurate
Trello | -13% error

@juskek juskek force-pushed the 2023/07/18-Amount-of-data-transferred-in-DevTools-is-different-from-amount-recorded-by-extension branch from 5121cd1 to ef06ac5 Compare July 21, 2023 09:25
{ urls: ["<all_urls>"], tabId },
["responseHeaders"]
);
chrome.debugger.attach({ tabId: tabId }, "1.2", function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this debugger.attach do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Attaches the chrome debugger to a tab, allows the extension to listen to network requests for that tab :)

);
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

sendResponse(true);
}
return true;
});

chrome.debugger.onEvent.addListener(function (source, method, params) {
Copy link
Contributor

Choose a reason for hiding this comment

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

General question of the codebase, do we use the function keyword over arrow functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, arrow notation is preferred, will cleanup

Comment on lines 53 to 55
const { encodedDataLength } = params as {
encodedDataLength?: number;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Ew type casting - could you not just type the function at the top like so: function (source: unknown, method: string, params: { encodedDataLength?: number}) { ... }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch!

Comment on lines 13 to 14
} catch (e: unknown) {
if (e instanceof Error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

SImilar error question here

src/background/background.ts Outdated Show resolved Hide resolved
"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.`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Debugging is required as soon as you have an error, isn't it? We should maybe consider it the other way around "if popup is closed then the error is ok"

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 can catch this error by checking if (popup is closed && could not establish connection), but checking if the popup is closed requires chrome.runtime.getContexts()

  • This is a new api that recently got proposed, and the only example I have found where it is used is here
  • The example has "minimum_chrome_version": "116", while the current stable version is 115, so I think we can leave it for now and catch it properly when 116 is out image

@juskek juskek force-pushed the 2023/07/18-Amount-of-data-transferred-in-DevTools-is-different-from-amount-recorded-by-extension branch from ef06ac5 to 75dc4e1 Compare July 21, 2023 10:53
Copy link
Collaborator

@Lerri-Cofannos Lerri-Cofannos left a comment

Choose a reason for hiding this comment

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

Good to go!

Comment on lines 34 to 44
const error = e as Error;
if (
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just do inline type casting since you don't use error anywhere else: (e as Error).message === ...

@juskek juskek force-pushed the 2023/07/18-Amount-of-data-transferred-in-DevTools-is-different-from-amount-recorded-by-extension branch from d998849 to 69b6924 Compare July 21, 2023 13:14
@juskek juskek merged commit e3bb9a7 into main Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants