I'm new to chrome extensions and can't figure out how the background concept works. I create a counter that continues to count even when the user closes the extension (but not the browser) and wants to run a simple test to find out if I can understand how to use the background file. Below is my attempt to create a function that is activated every time a user clicks on a tab (outside my extension), and when they click on 5 tabs, a warning appears. I canβt understand why this is not working.
background.js:
var counter = 0; chrome.browserAction.onClicked.addListener(function(tab){ counter++; if (counter == 5) { alert("Hi"); } });
manifest.json:
{ "name": "Hello World!", "description": "My first packaged app.", "version": "0.1", "permissions": ["tabs", "http://*/*"], "manifest_version":2, "content_scripts": [ { "js": [ "jquery-1.9.1.js", "myscript.js" ], "matches": [ "http://*/*", "https://*/*"] }], "background": { "scripts": [ "background.js" ] }, "browser_action": { "default_title": "10,000 Hours", "default_icon": "icon16.png", "default_popup": "index.html" }, "icons": { "16": "icon16.png", "48": "icon48.png", "128": "icon128.png" } }
source share