154

I just bought WebStorm 5 and so far have been really enjoying its Inspection features. One hitch I've run in to when developing my Chrome extension is that it doesn't recognize the chrome variable:

Unresolved variable or type chrome

Is there a way I can add the chrome variable to the Inspector so that it can autocomplete as I type? I'm guessing I would need to add Chromium as an External Library but I'm unsure where to start.

matpie
  • 17,033
  • 9
  • 61
  • 82

4 Answers4

321

First Time Setup

  1. Open the Settings dialog (File > Settings)

  2. Click Languages & Frameworks > Javascript > Libraries

  3. Click Download

  4. Make sure TypeScript community stubs is selected

  5. Select chrome from the list (you can find it quickly by just typing chrome)

  6. Click Download and Install

  7. Click OK to close the Settings dialog.


Steps 2-6 illustrated below:

Webstorm Screenshot


In Subsequent Projects

In any subsequent project, you just:

  1. Open the Settings dialog again (File > Settings)

  2. Click Languages & Frameworks > Javascript > Libraries again

  3. Check chrome-DefinitelyTyped

  4. Click OK to close the dialog.


Steps 2-4 shown below:

Webstorm screenshot

Miscreant
  • 6,626
  • 3
  • 22
  • 21
50

UPDATE 2:

It's now supported out of the box, see the complete answer below.

Download library

UPDATE:

There is a more complete stub file that can be added as a library to get code completion. It's a part of the Closure Compiler project. Download chrome_extensions.js.

See also the feature request for WebStorm to add this library automatically from the IDE.


You need to get the JavaScript library for the Chrome API somewhere, or use a stub to get basic completion.

Library or a stub can be configured in WebStorm.


I found the JSON files with the Extension API. One can write a script that will build JS stubs from these JSON files, the stubs can look like the basic version linked on GitHub above, but with the automatic generation they will contain almost complete API and JSDoc comments so that documentation like here can be viewed directly in the IDE.

JSON => JavaScript object stubs mapping is pretty straightforward in this case and writing this kind of converter should not take more than a day (or several hours for the skilled coder).

If someone goes ahead and implements it, please post the link to the results here.

Tyler Crompton
  • 12,284
  • 14
  • 65
  • 94
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Thanks, I like this idea the best. I actually ended up cloning the Chromium Git Repo and added the folder `/chrome/renderer/resources/extensions` which gave me most of what I wanted for Inspection. – matpie Dec 24 '12 at 17:16
  • sirlancelot - doesn't work for me ... can you post here some snipper which has auto completition after adding this dir as a library to WebStorm (or IntelliJ) ? – Michal Bernhard Mar 19 '13 at 13:37
  • 2
    I downloaded [Chrome Extension](https://code.google.com/p/closure-compiler/source/browse/contrib/externs/chrome_extensions.js) but to get rid of the `Unresolved variable` warning in WebStorm, I had to add the following line to `chrome_extension.js`: `var chrome = {}` and include it as a library in my project. – M.K. Safi Jan 19 '14 at 19:38
  • There is also a fairly recent (Dec. 2013) vsdoc file at https://code.google.com/p/chrome-api-vsdoc. It has object, method and parameter descriptions (which this TypeScript version lacks). Descriptions are a bit mangled when viewed with Ctrl-Q due to vsdoc using XML for doc annotations, but still readable and usable. Just add the downloaded vsdoc as a custom JS library in Webstorm. – Boris B. Oct 23 '14 at 17:25
  • New URL to chrome_extension.js: https://raw.githubusercontent.com/google/closure-compiler/master/contrib/externs/chrome_extensions.js – Tuan Ha Apr 07 '22 at 15:56
2

WebStorm should one day accept json definitions directly to enable autocomplete for the functions defined. Meanwhile, you can use the program at https://github.com/QuickrWorld/jsgen to convert the json files to js to enable auto-complete for the chrome extension APIs.

Amitabh
  • 162
  • 9
-2

For writing AppScript, functions and classes such as DriveApp, SpreadsheetApp, there is a plugin in WebStorm or Intellij called google-app-script.
The installation method is the same as above. On the other hand, you should mark or open the .gs file as JavaScript. (July 2017)

Mingzhong
  • 223
  • 4
  • 6