Google chrome extension 如何从background.js访问变量以在chrome扩展中弹出
在chrome extension中,我通过background.js创建了一个新的html页面作为弹出窗口。检查下面的background.js代码Google chrome extension 如何从background.js访问变量以在chrome扩展中弹出,google-chrome-extension,extension-methods,browser-extension,Google Chrome Extension,Extension Methods,Browser Extension,在chrome extension中,我通过background.js创建了一个新的html页面作为弹出窗口。检查下面的background.js代码 chrome.contextMenus.create({ title: "share this", contexts: ["selection"], onclick: myFunction })) 我已经通过了给定的getbackgroundpage函数。我可以使用它访问变量test0,但不能访问sourceURL(因为它不在窗口范围内) 通
chrome.contextMenus.create({
title: "share this",
contexts: ["selection"],
onclick: myFunction
}))
我已经通过了给定的getbackgroundpage函数。我可以使用它访问变量test0,但不能访问sourceURL(因为它不在窗口范围内)
通过重定向弹出窗口代码如下:
var bg = chrome.extension.getBackgroundPage();
var sourceURL = bg.sourceURL;
var testvariable = bg.test0;
sourceURL未定义
不知道如何将变量定义为全局变量,以便在弹出窗口中访问
有没有更好的办法,我们可以做吗?伙计,试着用
以前链接的一个小例子
背景剧本
内容脚本
使用消息或chrome.storage.local或localStorage或其他全局变量。如何使用全局变量选项。如果给定syntax.test0是一个全局变量,我们将不胜感激。
var bg = chrome.extension.getBackgroundPage();
var sourceURL = bg.sourceURL;
var testvariable = bg.test0;
var myFunc = function(){
//some code
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.messageType == "callSomeFunc") {
myFunc();
sendResponse({
status: "Function myFunc called."
});
}
//use return true for async response
return true;
});
chrome.runtime.sendMessage({
messageType: "callSomeFunc"
},
function(response) {
// response contain {status: "Function myFunc called."}
console.log(response);
});