0

I'm trying to create an on/off toggle for my chrome extension. But, it keeps saying that it can't read my browser action on click? Here's what I have so far...

manifest

{
  "manifest_version": 2,

  "name": "Spatial Intelligence",
  "version": "0.1.0",
  "description": "Dope vibes for all",
  "chrome_url_overrides" : { 
      "newtab": "myNewTabPage.html"
  },


  "background": {
    "scripts": ["background.js"],
    "persistent": true
  },

  "browser_action": {
  },


  "content_scripts": [{
      "run_at" : "document_end",
    "css": ["styles.css"],
    "js": ["content.js"],
    "matches": ["<all_urls>"]
  }]      
}

background

var enable =false;
chrome.browserAction.onClicked.addListener(function (tab) {
 enable ?= enable false : true;
 if(enable){
  //turn on...
  chrome.browserAction.setIcon({ path: 'https://i.ibb.co/XCKg9qd/pizza.png' });
  chrome.browserAction.setBadgeText({ text: 'ON' });
  chrome.tabs.executeScript(null, { file: 'content.js' }); 
 }else{
  //turn off...
  chrome.browserAction.setIcon({ path: 'https://i.ibb.co/hFg555J/pizza-bw.png'});
  chrome.browserAction.setBadgeText({ text: 'OFF' });
 }
});

Does anyone have any insight as to how to fix this? Also, am I on the right track with a toggle on/off ?

Lewis
  • 3,479
  • 25
  • 40
  • Your problem seems similar to - https://stackoverflow.com/questions/11494232/why-is-chrome-browseraction-onclicked-undefined . – random Mar 21 '19 at 09:43
  • A common mistake is not reloading the extension on chrome://extensions page and looking at the [wrong console](https://stackoverflow.com/a/10258029). It's also not clear why you declare the content script as it means an automatically injected content script, but you probably don't want that. And `?=` is invalid code - hopefully it's just a borked copypaste here. – wOxxOm Mar 21 '19 at 14:40
  • @randomSoul the solution to that issue was to include the `"background"` section in the manifest. this problem is occurring for me as well, even though the code is in the `background.js` file. its very strange. – omikes Nov 09 '19 at 19:17

0 Answers0