Question about timing for Extension loading in Chromebooks

110 views
Skip to first unread message

kinoko koutya

unread,
Apr 20, 2023, 7:04:39 AM4/20/23
to Chromium Extensions
I need help with timings of Extension loading in Chromebooks.

Trying to make an Extension that logs all requests sent from Chrome, but when the setting [On startup > Continue where you left off] is set and you log-in into Chromebook, the first request seems to be sent before the extension loads, and therefore I cannot log the request.

Does anyone know if this is the case?
If so, is there any way to work around this issue? For example, delay the request until the extension loads?

Deco

unread,
Apr 20, 2023, 11:15:02 AM4/20/23
to kinoko koutya, Chromium Extensions
Yes this is possible, chrome.webRequest can be used to intercept the request, and you can use chrome.webRequest.onBeforeRequest to intercept all incoming requests including the first request that is sent before the browser is loaded. It's as simple as checking if the browser is loaded, and if not delay the request:
chrome.webRequest.onBeforeRequest.addListener( function(details) { // Check if the extension is loaded if (!extensionIsLoaded) { // Delay the request by cancelling it and resending it after a short delay return {cancel: true};
is an example of how this is done.  
From then on simply do what you were intending, e.g. sending a request:
setTimeout(function() { chrome.webRequest.onBeforeRequest.removeListener(requestListener); sendRequest(details.url);}

In terms of implementing the extension is loaded segment, you can do this by listening for the onDOMContent:
let extensionIsLoaded = false; // Listen for the onDOMContentLoaded event to know when the extension has finished loading document.addEventListener("DOMContentLoaded", function() { extensionIsLoaded = true; });

Thanks,
Deco

wOxxOm

unread,
Apr 20, 2023, 11:55:00 AM4/20/23
to Chromium Extensions, Deco, Chromium Extensions, kinoko koutya
> Yes this is possible, chrome.webRequest can be used to intercept the request

No, not like this, because the problem is caused by a different thing, see below.

>  the first request seems to be sent before the extension loads, and therefore I cannot log the request

Indeed, Chrome intentionally delays the extensions and starts them only after the active tab starts loading, so there's no way to intercept those first requests.

There is no solution, but there are workarounds:
  1. Block all main_frame requests by default via declarativeNetRequest (actually redirect them to your own dummy html and save the original URL via regexFilter, example), and then use chrome.tabs.query + chrome.tabs.update on all such tabs when your extension finally starts.
  2. Add "permissions": ["background"] to keep your extension running even after the last browser window is closed. The user can still close your extension from the system tray. The user must allow "Continue running background apps when Google Chrome is closed" option in browser settings. Not sure Chromebook supports this feature though.

Deco

unread,
Apr 20, 2023, 12:11:39 PM4/20/23
to wOxxOm, Chromium Extensions, kinoko koutya
Ah yeah thanks for the clarification, oversight on my part.

kinoko koutya

unread,
Apr 20, 2023, 10:14:36 PM4/20/23
to Chromium Extensions, Deco, Chromium Extensions, kinoko koutya
thanks for the reply.
sorry for not being specific enough. i am using chrome.webRequest.onBeforeRequest.addListener, but was not able to capture the first request.
was wondering if there was something i could do even before onBeforeRequest to block the access.

2023年4月21日金曜日 0:15:02 UTC+9 Deco:

kinoko koutya

unread,
Apr 20, 2023, 10:19:32 PM4/20/23
to Chromium Extensions, wOxxOm, Deco, Chromium Extensions, kinoko koutya
i did not know that chrome was purposely delaying the loading of extensions till after the first load.  that makes sense why it was only the first load that i was not able to capture.

those two workarounds are something i did not know about. i do believe they will do what i need.
will give it a try.

thanks a lot, wOxxOm!

2023年4月21日金曜日 0:55:00 UTC+9 wOxxOm:

Taro Crown

unread,
Jun 21, 2023, 6:31:28 AM6/21/23
to Chromium Extensions, kinoko koutya, wOxxOm, Deco, Chromium Extensions
Hello. I am also facing this problem.

I am developing an extension that controls requests using webRequestBlocking.
I am also unable to get the first request onBeforeRequest when launched on a Chromebook.

I tried wOxxOm's method below, but ran into issues.
>1. Block all main_frame requests by default via declarativeNetRequest (actually redirect them to your own dummy html and save the original URL via regexFilter, example), and then use chrome.tabs.query + chrome.tabs.update on all such tabs when your extension finally starts.
By setting the default rule for declarativeNetRequest to block all main_frames, I can block the first request.
But then I need to disable the default rule in order to browse as normal,
and I also need to enable the rule just before browsing is complete (logging out of the Chromebook or shutting down the Chromebook).

I do not know how to re-enable the rule when I am done using the Chromebook.
I have considered chrome.runtime.onSuspend() but it seems not suitable as I am using background scripts.

Am I missing something?
I would like to know if there is a way to enable the declarativeNetRequest rule when the Chromebook usage is complete.
Or I would like to know how to control all requests from the time the Chromebook is started until it is finished.

Thanks.

2023年4月21日金曜日 11:19:32 UTC+9 kinoko koutya:

wOxxOm

unread,
Jun 21, 2023, 7:35:11 AM6/21/23
to Chromium Extensions, Taro Crown, kinoko koutya, wOxxOm, Deco, Chromium Extensions
I think it should be possible by adding an allow rule with higher priority in chrome.declarativeNetRequest.updateSessionRules

Taro Crown

unread,
Jun 22, 2023, 7:18:43 AM6/22/23
to Chromium Extensions, wOxxOm, Taro Crown, kinoko koutya, Deco, Chromium Extensions
I used chrome.declarativeNetRequest.updateSessionRules and it worked.
Thanks!
2023年6月21日水曜日 20:35:11 UTC+9 wOxxOm:
Reply all
Reply to author
Forward
0 new messages