要实现background.js 和 popup.js 之间的交互,首先需要先配置好 manifest.json文件,如:

"background":{
    //"page":"background.html",
    "scripts":["js/jquery-1.10.2.min.js","js/background.js"]
}

必须要配置好 background 项,background.html 可以不需要,scripts 需要配置好js文件。这里我用的 background.js 文件。

 

popup.js调用 background.js 文件里面的函数。

background.js

function test(){
    alert("test");
}

popup.js

// 先获取background页面
var bg = chrome.extension.getBackgroundPage();
//再在返回的对象上调用background.js 里面的函数
bg.test();

更新插件即可。

原文:https://www.cnblogs.com/ayseeing/p/4010830.html

相关文章:

  • 2021-11-21
  • 2022-12-23
  • 2021-11-12
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2021-03-30
  • 2021-04-25
猜你喜欢
  • 2021-11-26
  • 2022-12-23
  • 2021-10-31
  • 2022-01-17
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
相关资源
相似解决方案