Satya
Satya

Reputation: 1037

chrome.management.onInstalled.addListener throwing Uncaught SyntaxError: Unexpected identifier

I am trying to write a chrome extension to listen to the install / uninstall / enabled / disabled events for other extensions either by using chrome web store or by other means.

I am planning to use the chrome.management module for that.

  1. I had declared the permissions in the manifest.json file.

    "permissions": [
        "management"
    ],
    
  2. I have a background.html page

    "background": {
        "page": "background.html"
    },
    
  3. I had included the background.js as a part of the background.html page

    <html>
    <script type="text/javascript" src="js/background.js"></script>
    <body>
    </body>
    </html>
    
  4. And here is my background.js

    chrome.management.onInstalled.addListener(function(ExtensionInfo info) {
      console.log('somethingz installed...');
      printInfo(info);
      });
    
    chrome.management.onEnabled.addListener(function(ExtensionInfo info) {
      console.log('somethingz enabled...');
      printInfo(info);
    });
    
    
    chrome.management.onDisabled.addListener(function(ExtensionInfo info) {
      console.log('somethingz disabled...');
      printInfo(info);
    });
    
    
    function printInfo(info)
    {
      console.log("id: " ++ info.id);
      console.log("name: " ++ info.name);
      console.log("description: " ++ info.description);
      console.log("version: " ++ info.version);
      console.log("mayDisable: " ++ info.mayDisable);
      console.log("enabled: " ++ info.enabled);
      console.log("disabledReason: " ++ info.disabledReason);
      console.log("type: " ++ info.type);
      console.log("appLaunchURL: " ++ info.appLaunchURL);
      console.log("homepageUrl: " ++ info.homepageUrl );
      console.log("updateUrl: " ++ info.updateUrl  );
      console.log("offlineEnabled: " ++ info.offlineEnabled);
      console.log("optionsUrl: " ++ info.optionsUrl);
      console.log("icons: " ++ info.icons);
      console.log("permissions: " ++ info.permissions);
      console.log("hostPermissions: " ++ info.hostPermissions);
      console.log("installType: " ++ info.installType);
    }
    

I am trying to debug the background.html page by looking at the chrome://extensions page and I am seeing -

    Uncaught SyntaxError: Unexpected identifier

and the events are not firing as expected because of this error.

Please help!! not sure what is causing the "Uncaught SyntaxError: Unexpected identifier" error... May be it is something too basic, however, I am not able to catch it by looking at it for quite some time!

Upvotes: 0

Views: 547

Answers (1)

BeardFist
BeardFist

Reputation: 8201

Your code has 2 major problems. First you include the type of the parameters in the listeners, in this case the ExtensionInfo, remove that. Second, you are using 2 + change it to 1. The resulting code looks like this:

chrome.management.onInstalled.addListener(function(info) {
  console.log('somethingz installed...');
  printInfo(info);
});

And this

function printInfo(info){
  console.log("id: " + info.id);

As a side note, instead of manually logging every part of info, you can just do something like this:

console.log(JSON.stringify(info).replace(',','\n'));

Upvotes: 3

Related Questions