0

I am making an extension for a site called csgo500.com . In my script there is a function that starts if I win or lose. The function works when I put it directly into the console, BUT when I make it as a extension it only tells me:

Uncaught ReferenceError: winner is not defined

My code looks like this:

var winlose = function() {
  if (winner.choice === 0) {
    document.getElementById('bet-btn-min').click();
    document.getElementById('bet-btn-10').click();
}
else {
  document.getElementById('bet-btn-double').click();
}
}

I have checked the other explanations for "is not defined" on this site, but none has the same type of problem as I have. Any suggestions or soultions?

Edit: The winner is being declared in the sites js as: '

function generateRound(data) {
    lastRound = round;
    round = data.round;
    winner = data.winner;
    numberBets = 0;
    $wheelTimer.show();
Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
  • 1
    Where's your winner being declared? – shriek May 29 '16 at 15:40
  • Still don't see it being declared. It should be something like `var winner;` – shriek May 29 '16 at 16:00
  • new update @shriek – Andreas Moldskred May 29 '16 at 16:21
  • I recommend you go through [these](https://github.com/getify/You-Dont-Know-JS) books. Not trying to talk down on you but when you're assigning `winner` without declaring it on non strict mode, the js compiler will automatically declare this on global scope which is `window` in case of browsers. More of that on [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) too. Pretty much all your variables assigned in `generateRound` are hanging in global scope even though you've assigned them in function scope. – shriek May 29 '16 at 16:50
  • @shriek I am not the one assigning the winner state. Winner.choice is a part of the website and I want by extension to react to it when it's state is 0. All my content is loaded on the site but when it checks for winner.choice I get the 'Uncaught reference'. I thought by assigning my js file into contentscripts it would work as if I would put it into console, what is different? – Andreas Moldskred May 29 '16 at 19:29
  • @shriek FYI, the issue at hand is [context isolation](https://developer.chrome.com/extensions/content_scripts#execution-environment) of Chrome extensions. The answer is correct and implements [this technique](http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script). – Xan May 31 '16 at 13:48

1 Answers1

1

I found a solution to my problem after a while:

What I ended up doing was changing the way I put my script into the site. I change my manifest:

   "js": ["contentscript.js"]
    }
  ], 
   "web_accessible_resources": ["inject.js","index.html"],

And I made the file contentscript.js:

var s = document.createElement('script');
s.src = chrome.extension.getURL('inject.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
    s.parentNode.removeChild(s);
};

SO now instead of just adding the inject file I insert it like if it was a document from the site. It can now access the sites files properly.

I hope this helps other people=)