2

For chrome extension developed using typescript, we need a definition file called chrome.d.ts in typings folder for accessing web extension APIs like chrome.runtime.* or chrome.tabs.*.

For edge extension, developing in typescript, we need a definition file called browser.d.ts in typings folder for accessing web extension APIs like browser.runtime.*

How can we download this dependency using node package manager(npm). I added a reference to the file in my background.ts file of extension like:

/// reference path="typings/browser/browser.d.ts"

Shivanshu Goyal
  • 500
  • 9
  • 25

2 Answers2

5

Update:

Typings are available for Firefox's browser API and its browser polyfill library.

Unfortunately, Edge's browser API is not the same as Firefox's. Edge uses callbacks while Firefox uses Promises. This may or may not change.

See:

Eejdoowad
  • 1,297
  • 9
  • 10
  • No, it gives chrome is undefined error for edge extension. when I replace chrome by browser, the same script works for me. I tried to replace chrome by browser in typescript, but it gets failed at the build time saying browser namespace is not found. How can I add browser.d.ts typing dependency. – Shivanshu Goyal Aug 01 '17 at 07:26
  • Be sure to follow the instructions here to polyfill the chrome namespace in Edge. https://learn.microsoft.com/en-us/microsoft-edge/extensions/guides/porting-chrome-extensions – Eejdoowad Aug 15 '17 at 22:46
  • There are Firefox-specific typings available on https://www.npmjs.com/package/@types/firefox-webext-browser, and typings for the browser polyfill (which has partial support for Edge) on https://www.npmjs.com/package/webextension-polyfill-ts – Motin Sep 28 '18 at 15:53
2

Since you need browser.d.ts only for type conversion this solution will work

You can add:

declare var browser: any;

at the top of your file where you are using browser.runtime.

CoyBit
  • 1,592
  • 1
  • 17
  • 19
  • Could you elaborate a bit more? – sg7 Apr 01 '18 at 11:19
  • microsoft edge extensions allow the functions like 'browser.runtime.getUrl', when we want to convert from typescript to a javascript file typescript compiler throws an error like 'browser undefined'.. since we don't have proper types defined for 'browser.d.ts' as we have for chrome, to bypass the error stated above we can add this line declare var browser: any; then everything will work as expected – prathyusha bandela Apr 12 '18 at 08:03