如何从我的Chrome扩展中获取变量'的背景脚本

How to get a variable from my Chrome Extension's background script?

本文关键字:脚本 背景 变量 获取 我的 Chrome 扩展      更新时间:2023-09-26

我正在使用Chrome API的以下代码制作Google Chrome扩展:

chrome.tabs.executeScript(null,{file : 'inject.js'},function(){})

现在我想从文件inject.js中获取值,以便在回调函数中使用,但我不知道如何做到这一点。有人能帮我吗?

chrome.extension.getBackgroundPage()返回当前扩展中运行的后台页面的JavaScript"window"对象。

var background = chrome.extension.getBackgroundPage();
// Now you can get any of your variables from the background object:
var jcvideos = background.jcvideos;

变量必须在全局范围内可访问才能工作。示例:

var outer_result = 0;
function example() {
    var inner_result = 1;   // <-- local variable
    outer_result = 2;       // <-- global variable
}
// 'inner_result' is undefined outside the function.
// You can only use 'outer_result' from other scripts.

阅读developer.cochrome.com上的更多信息:
关于getBackgroundPage()
背景页示例