As the questions says, I just want to know the difference between background script and content script in chrome extension. When I logged the chrome object in both the scripts, I found the different objects.
I want to inject my javascript into the page when the icon is clicked So in manifest.json
I added a content script but I am unable to listen to icon click event inside content script.
chrome.browserAction is not defined in chrome object in content script.
How can I listen to click event in content script. Can we include both background and content script ?
This is my manifest.json
{ "name": "First Plugin Testing", "version": "1.0", "manifest_version": 2, "description": "Trying hands on first extension", "background": { "scripts": ["background.js"] }, "browser_action": { "default_icon": "icon.png" }, "permissions": [ "tabs", "http://*/*", "https://*/*" ], "content_scripts": [ { "matches": ["http://*/*"], "js": ["temp.js"] } ] }
The background script should be viewed as "running in the background of the Chrome browser". Your desired effect (running a script for every page) is actually a task for content scripts. To learn more, read https://developer.chrome.com/extensions/overview.html#arch. Follow this answer to receive notifications.
Background scripts are the place to put code that needs to maintain long-term state, or perform long-term operations, independently of the lifetime of any particular web pages or browser windows.
Communication between extensions and their content scripts works by using message passing. Either side can listen for messages sent from the other end, and respond on the same channel. A message can contain any valid JSON object (null, boolean, number, string, array, or object).
A background page runs at all times when the extension is enabled. You cannot see it, but it can modify other aspects of the extension, like setting the browser action badge. Then, you can make a request and schedule it so the data is retrieved and processed regularly, you can also stop the request at any time.
I have found the answer to the questions asked.
A. Can we include both content script and background script ?
Yes, we can include both the background scripts and content scripts in manifest. To do interaction between them you can use Chrome Message Passing API.
I was doing the same way but there was some error in background script which I could not see therefore I posted this question after some searching on google.
B. How can I listen to click event in content script ?
Solution: We can not have browser click event
in content script. It has only partial access to chrome object So you have to receive the click handler in background script and send message to content script and do whatever you want.
Use chrome.browserAction.onClicked
event in background script and then use message passing to send the information to content script that user clicked on icon.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With