0

I'm trying to write a bookmarklet that augments the functionality of a website I frequently use. All the code of said website is in a single script encompassed in a self invoking function like so:

(function() {

  var exampleVar=1;

  // code

}).call(this);

I have no control over that website. How do I get the value of exampleVar from the console or from a bookmarklet?

dj50
  • 51
  • 8
  • Either use Chrome Local Overrides to replace the script response, or a userscript to intercept the addition of the tag into the DOM and replace it with your own' – CertainPerformance May 01 '20 at 13:12

1 Answers1

0

You can't, that's one of the purposes of scoping, to protect the variable form being accessed.

t.niese
  • 39,256
  • 9
  • 74
  • 101
  • OP just wants to access the value, which is easy. It's also possible [to change it](https://stackoverflow.com/a/59518023), though more complicated. – CertainPerformance May 01 '20 at 12:42
  • 1
    @CertainPerformance the linked answer would not allow changing the variable. You can create a copy of the script and modify it and rerun it. But modifying a script before it is executed and running it, and/or accessing variable of a script are two different things. While I agree that it is not exactly clear if the OP talks about one or the other, that are still two different things. In the on case you extract the initial value of the variable by "parsing" the script, but accessing the variable itself is not possible. – t.niese May 01 '20 at 12:52
  • 2
    @CertainPerformance If `exampleVar` is never changed while the script is running, this can be indeed a way how to get that value, but not if the script changes the value over time. – t.niese May 01 '20 at 12:52
  • @t.niese Indeed, the script changes the value over time. – dj50 May 01 '20 at 13:01