浏览器插件background.js导入js文件

1,429 阅读1分钟

浏览器插件

基于V3版本书写

在编写浏览器插件的时候,不可避免的需要在写background模块,随着项目的升级、迭代,background越来越大,这个时候,我们可能需要对background文件进行代码拆分,这个时候就需要在background文件中,引入模块、或者 js文件,下面说俩种引入方式

  • 基于 importScript api

当我们的manifest.json注册后台服务的时候

"background": {

    "service_worker": "background.js"

},

background.js

importScripts('./test.js')

say(3)

test.js

function say(id){

    console.log(id)

}
  • 基于ES模块

我们需要在manifest.json注册后台服务的时候

"background": {

"service_worker": "background.js",

"type": "module"

}

background.js


import say from './test.js';

say(3)

test.js

function say(id){

console.log(id)

}

export default say;