-1

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?

jaggedsoft
  • 3,858
  • 2
  • 33
  • 41
  • have you read the documentation - https://developer.chrome.com/extensions/tabs#method-executeScript – Jaromanda X Nov 21 '15 at 04:25
  • Can you please elaborate on what you mean by "the value?" – jaggedsoft Nov 21 '15 at 04:48
  • I'm making an extension with get iframe cards from current tab and I create a variable called jcvideos to save them(with some function to execute it).After that,I want to get the value of jcvideos to use it in my other javascript file called background.js,elaborate in the code above(at function(){}).I've read the documentation of Chrome Developer to find help,but the information is equivocal,I can't understand that. – Huynh Thai Hoa Nov 21 '15 at 05:09
  • 1
    Possible duplicate of [about chrome.tabs.executeScript( id,details, callback)](http://stackoverflow.com/questions/13166293/about-chrome-tabs-executescript-id-details-callback) – rsanchez Nov 21 '15 at 19:44
  • I've seen the question above but it use {code:...},I don't know whether it's different from my case (I use {file:...}). – Huynh Thai Hoa Nov 22 '15 at 01:27
  • Possible duplicate of [How to pass a variable value between background scripts in chrome extensions](http://stackoverflow.com/questions/21012250/how-to-pass-a-variable-value-between-background-scripts-in-chrome-extensions) – jaggedsoft Nov 22 '15 at 17:53
  • @HuỳnhTháiHòa `{code:...}` and `{file:...}` are equivalent in this context. The callback function will get the last evaluated expression in each frame of the injected page. – Teepeemm Nov 22 '15 at 20:27

1 Answers1

-1

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

jaggedsoft
  • 3,858
  • 2
  • 33
  • 41