简体   繁体   中英

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

I'm making a Google Chrome extension with using this code from Chrome API:

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

Now I want to get the value from file inject.js to use in my callback function but I don't know how to do that. Can anybody help me?

chrome.extension.getBackgroundPage() Returns the JavaScript 'window' object for the background page running inside the current extension.

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

A variable must be accessible at global scope for this to work. Example:

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.

Read more on developer.chrome.com:
About getBackgroundPage()
Background Page Examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM