0

I am writing chrome extension and when I write to console "g_ActiveInventory" I get the result:

result

But when I am trying to get object using this code in my extension:

window.setTimeout(test2, 5000);
function test2()
{
    var size = Object.keys(g_ActiveInventory.rgInventory).size();    
    console.log(size);
}

I am receiving error:

Uncaught ReferenceError: g_ActiveInventory is not defined

How can I fix it?

manifest.json:

{
    "name": "How Many Items",
    "version": "1.0",
    "manifest_version": 2,
    "description": "",
    "icons":    {
        "16": "icons/love.png"
    },
             "background": {
        "scripts": [
            "node_modules/jquery/dist/jquery.min.js"           
        ],
        "persistent": true
    },
    "browser_action": {
        "default_icon": "icons/love.png",
        "default_title": "How Many Items"
    },
    "content_scripts":  [ {
    "js": [ "js/jquery/jquery-1.10.2.min.js", "js/code.js" ],
      "matches": [ "*://steamcommunity.com/tradeoffer/*" ],
      "run_at": "document_end"
      } ]
}
  • 1
    May it be that `test2` is called **before** `g_ActiveInventory` is defined? Please check the order of scripts where these symbols are defined. – Eduard Malakhov Feb 15 '17 at 20:37
  • I set timeout to be sure that all data is loaded. Firstly loading page, then loading this data with g_ActiveInventory. So I set timeout to wait when it will be loaded. – Good Person Feb 15 '17 at 20:41
  • 2
    If the variable `g_ActiveInventory` was created by the webpage, your content script cannot access it. See [documentation](https://developer.chrome.com/extensions/content_scripts): Content scripts **cannot** use variables or functions defined by web pages or by other content scripts. – Iván Nokonoko Feb 15 '17 at 20:42
  • Is there any way to get access in my chrome extension? – Good Person Feb 15 '17 at 20:46
  • See [Building a Chrome Extension - Inject code in a page using a Content script](//stackoverflow.com/a/9517879) – wOxxOm Feb 15 '17 at 22:51

3 Answers3

1

As the documentation states (see here and here), your content script cannot directly access the webpage's window object or variables. You can, however, try this little trick: creating a new script tag in the page's DOM that includes the code you want. For instance:

setTimeout(test2,5000);
function test2() {
  var myScript = document.createElement("script");
  myScript.innerHTML = `
    var size = Object.keys(g_ActiveInventory.rgInventory).size();
    console.log(size)`;
  document.body.appendChild(myScript);
}
Daniel Herr
  • 19,083
  • 6
  • 44
  • 61
Iván Nokonoko
  • 4,888
  • 2
  • 19
  • 27
-1

You should define the g_ActiveInventory before calling test2

Check this code :

function function_name() {
  console.log(size);
}


function_name();//it gives undefined because teh variable defined atfer calling

 var size="12";

and this is thecorrect way

 function function_name() {
  console.log(size);
}

var size="12";
function_name();//it gives 12 because the variable defined before calling

I hope this helps

Ali insan Soyaslan
  • 836
  • 5
  • 14
  • 33
  • I am trying to parse this data from web page. So my extension wait 5 seconds when page load and get g_ActiveInventory. If I will press refresh and will write to console "g_ActiveInventory" 1 sec later I will receive data, but I receive error in JS. – Good Person Feb 15 '17 at 20:44
  • As you can see, I get data in console, and then I receive error from extension: http://i.imgur.com/NSn4qC7.png – Good Person Feb 15 '17 at 20:45
  • okey i see, that'a diffrent problem then,, – Ali insan Soyaslan Feb 15 '17 at 20:48
-1

The object could be loaded past 5 seconds or is not availabe yet when you try to use it.

You can test the variable before using it, and wait and try again if it's not loaded yet, like this example:

setTimeout(test2,5000);
function test2()
{
    if (!g_ActiveInventory){
       console.log("Not loaded yet...");
       setTimeout(test2,1000);
       return;
    }
    var size = Object.keys(g_ActiveInventory.rgInventory).size();    
    console.log(size);
}
F.Igor
  • 4,119
  • 1
  • 18
  • 26