要实现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.aa();

刷新下 chrome://extensions/ 页面,点击插件图标即会弹出 test 浮层。

相关文章:

  • 2022-12-23
  • 2021-09-12
  • 2022-12-23
  • 2021-11-18
  • 2021-12-28
  • 2021-12-19
  • 2021-12-27
猜你喜欢
  • 2021-11-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-12-11
  • 2021-12-11
  • 2022-12-23
相关资源
相似解决方案