• Posted on July 31, 2014

Chrome Extension - Check If Developer

Creating Chrome Extensions is very enjoyable, up until the point you realize that you kept all of the debugging code enabled for your users to see. Often times, you will write this data to the console, so having it always enabled isn’t such a big deal, but why fill up the console with a ton of gibberish to the average user?

The main solution out there is to have a logging function, and when you publish your extension you disable it by making it return false or breaking before any output is displayed. But as noted before, you will forget to do this here and there.

What’s the better solution? Well, using chrome.management API’s to detect the installType is one method, but you need to modify your permissions to an overkill API. You don’t need to manage all extensions, just your own. That’s why I created a little “hack” that will easily let you know if the extension is installed or if you (the developer) are working on it.

function is_installed() {
	// Make sure the chrome.runtime.getManifest() function exists
	if(typeof chrome == 'object' && typeof chrome.runtime == 'object' && typeof chrome.runtime.getManifest == 'function') {
		var manifest = chrome.runtime.getManifest();
		if(!manifest) {
			return true;
		}

		if(typeof manifest.key == 'undefined' && typeof manifest.update_url == 'undefined') {
			return false;
		}else{
			return true;
		}
	}

	// We are unsure if the extension is installed or in developer mode. Assume installed.
	return true;
}

Using the function above, you can check if the extension is installed or not. This solution doesn’t rely on you having to setup event listeners for the onInstall functions and store a value. This solely relies on how the Google Web Store modifies the manifest file.

How it Works

The reason this works relies on how the Google Web Store modifies the manifest.json file. When it’s uploaded, two new keys are added under the names of key and update_url. If you specified these in your manifest.json file, this function won’t work.

For the developer, these two value will not be set and won’t be returned with the getManifest() function, but normal users who download your extension from the Web Store will have these values set. The function checks to see if those values are set or not and returns a boolean.

I added a few extra cases to make the function more strict. All failed paths will return true as if the extension is installed. I wanted this function to be used so I could add extra development tools without forgetting to disable them when I publish the extension. When in doubt, assume the extension is installed and not manually / locally setup.

I also setup a smaller line of code if you want. It doesn’t contain as many checks, is difficult to read, and may have issues since I didn’t test is in all cases. You will then use the variable installed inside of various functions to see if the extension is installed (through the Web Store) or not.

var m = null;
var installed = (m = chrome.runtime.getManifest(),(m&&!m.key&&!m.update_url)?false:true);