How to use chrome.scripting.removeCSS method in manifest version 3?

307 views
Skip to first unread message

Tejas Hegde

unread,
Jun 12, 2021, 5:39:19 AM6/12/21
to Chromium Extensions
I am inject a content script upon clicking the extension icon, and when it is clicked again, I do not want to reinject the script. I also inject a CSS file. When the icon is clicked again, I want to remove the CSS file that I previously inserted. I read that you can use the method chrome.scripting.removeCSS. However, I cannot get it to work. This is the code that I have in my background script:

RemoveCSS.jpg

Any advice on how to solve this problem? Thanks!


Deco

unread,
Jun 12, 2021, 5:43:02 AM6/12/21
to Tejas Hegde, Chromium Extensions
I'm not sure I entirely understand what you mean, presumably when an icon is clicked, a redirect occurs. In this situation using removeCSS is not necessary as Chrome will automatically unload the injected resources.

Thanks,
Deco

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/dda98168-b8e0-4a64-ad60-7a3468ce92e9n%40chromium.org.

Tejas Hegde

unread,
Jun 12, 2021, 5:50:10 AM6/12/21
to Chromium Extensions, decklin...@gmail.com, Chromium Extensions, Tejas Hegde
Hi Deco,

When the icon is clicked, the script foreground.js is injected. After that the CSS file foreground_styles.css is injected. When the extension icon is clicked again, I want to check if the script has already been injected, and if it has, do not inject again. I also want to remove the css file that was inserted. This does not happen automatically.

Thanks

Deco

unread,
Jun 12, 2021, 5:54:35 AM6/12/21
to Tejas Hegde, Chromium Extensions
Ah I see your issue, it is not possible to call removeCSS before insertCSS is called - the Promise for Manifest V3 (removeCSS) is dependent on it first being injected, in this manner, rework your file to inject the CSS script first, with the removal taking place after which should fix your problem.

Thanks,
Deco
Message has been deleted
Message has been deleted

Jackie Han

unread,
Jun 12, 2021, 2:02:36 PM6/12/21
to Tejas Hegde, Chromium Extensions

Tejas Hegde

unread,
Jun 13, 2021, 3:27:34 AM6/13/21
to Chromium Extensions, Jackie Han, Chromium Extensions, Tejas Hegde
Thanks Jackie (and Deco) for your help! I got it to work!
Reply all
Reply to author
Forward
0 new messages