Using chrome extension apis in typescript

前端 未结 5 1553
广开言路
广开言路 2021-01-30 09:17

I\'m building a chrome extension written in TypeScript. I\'m using WebStorm and I added the chrome-DefiniteltyTyped library in my project.

However, when I w

5条回答
  •  既然无缘
    2021-01-30 09:58

    Types Across Chrome and Firefox

    Since the extension API is basically the same across Chrome and Firefox now, you can use @types/chrome for both situations.

    1. install

    yarn add @types/chrome --dev
    

    2. update tsconfig

    {
      "compilerOptions": {
        ....
        "types": ["chrome"]
      }
    }
    

    3. Get browser api function

    export function getBrowserInstance(): typeof chrome {
      // Get extension api Chrome or Firefox
      const browserInstance = window.chrome || (window as any)['browser'];
      return browserInstance;
    }
    

提交回复
热议问题