Background.js not working Chrome extension

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" } } 
+6
source share
1 answer

It works for me with the following code.

manifest.json

 { "name": "Popping Alert", "description": "http://stackoverflow.com/questions/15194198/background-js-not-working-chrome-extension", "background": { "scripts": [ "background.js" ] }, "version": "1", "manifest_version": 2, "browser_action": { "default_title": "Click Me" } } 

background.js

 var counter = 0; chrome.browserAction.onClicked.addListener(function (tab) { counter++; if (counter == 5) { alert("Hey !!! You have clicked five times"); } }); 

Can you share your related code or articulate your problem if that doesn't work?

+9
source

All Articles