Chrome extensions with background scripts instead of background pages?

I recently started exploring Chrome Extensions and looked at sample code to understand how all the different parts fit together. I see that the logic of extensions is processed mainly on the background pages, but looking at the following code example, I noticed something peculiar.

{ "name": "Notification Demo", "version": "1", "description": "Shows off desktop notifications, which are \"toast\" windows that pop up on the desktop.", "icons": {"16": "16.png", "48": "48.png", "128": "128.png"}, "permissions": ["tabs", "notifications"], "options_page": "options.html", "background": { "scripts": ["background.js"] }, "content_security_policy": "script-src 'self'; img-src 'self'" } 

There is no background_page, but a background instead. I know that in β€œpermissions” there is a parameter that allows chrome to work in the background, even if it is not open, but it seems to be something else. It seems like you can use a background script instead of a background, but when I try to find information about it, I come up with nothing. Has anyone seen or used this before, and can you give a brief description of what would be the benefit of using a script background against the page background?

+4
source share
2 answers

This is a part of security that does not allow for embedded JavaScript. Instead of having a background_page that only loads background_script , use a separate file to allow JS loading directly. Since background code does not have a UI, HTML is really not needed. This looks like recent work and has not yet been documented.

+4
source

You can see this implementation in action in the Google code sample for the browserAction function:

http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/browserAction/

Check out make_bg_red in particular.

+2
source

Source: https://habr.com/ru/post/1391272/


All Articles