Ramifications of using chrome.scripting.executeScript ?

149 views
Skip to first unread message

Bogdan Nazaruk

unread,
Dec 30, 2023, 5:40:01 PM12/30/23
to Chromium Extensions
First, it's amazing that it's there. It solved A LOT of my problems. I use it every time I need to access tab's js context to print something to the console, get window.performance numbers or add listeners there.

I ended up doing it about twenty times. 

I just noticed this: 
1.jpg
The list goes on like that for a few pages...

Looks like every time I use it, it creates a new frame?
Thaaaat's... not healthy, right? 

uh... So a way to avoid it would be what? inject all my tab functions via the content-script by modifying dom. Deploy a listener there for my extension to send messages to the tab, then instead of running functions directly, run them through this awkward construct?

Gosh this sounds like such a drag. I don't wanna do it. Why don't they have an api simplifying it. This whole awkward logic could definitely be consumed and optimized by chrome. 

It's a lot of work to do it at this time. And the benefit seems to be miniscule. What do you think? Maybe there are better ways of mitigate this or maybe no mitigation is required. I don't like to see all these frames for sure.

wOxxOm

unread,
Dec 31, 2023, 4:27:20 AM12/31/23
to Chromium Extensions, Bogdan Nazaruk
No, these contexts are added every time you reload the extension, not due to executeScript, which runs in one (current) context. These are "orphaned" content scripts which still run but can't connect to the new extension. If they have listeners other than for `chrome` events you need to unregister them as explained in https://stackoverflow.com/a/57471345 or you can just add a check for chrome.runtime.id inside every such listener and unregister when it's falsy:

window.addEventListener('click', function onClick(evt) {
  if (!chrome.runtime.id) return window.removeEventListener('click', onClick);
  ..................
});

There's a bug in devtools: it doesn't remove the contexts even after reloading the tab. The workaround is to terminate the tab in Chrome's Task manager, then reload it.

Bogdan Nazaruk

unread,
Dec 31, 2023, 11:14:22 AM12/31/23
to Chromium Extensions, wOxxOm, Bogdan Nazaruk
What a relief!

Thanks a lot! I'll try unregistering them.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages