1

I am trying to access the window level variable in my script in the window.onLoad(), but it always returns undefined. But when I console log it or try to just type in the window in the debug console, it clearly shows me the variable exists on window object and has a value. I am unclear why this is happening and is there a way to access it? Please correct me if I am doing something wrong and point me in the right direction. Any help is greatly appreciated.

Update: The window._vp variable is declared in the script of some live website and I am trying to access that particular variable using my chrome extension.

My script:

window.onload = _ => {
    console.log("window._vp is:", window._vp);
}

Website script:

var _vp = {};
_vp['IsDashAvailable'] = false;
//...

You can see the behavior in the following screenshot of the dev tool console: enter image description here

Thank you.

Yankuan Zhang
  • 71
  • 1
  • 6
  • Some contextual code would be helpful. Is _vp something you attach to window? is it defined before you console.log it? – rjustin Sep 14 '17 at 03:28
  • *"But when I console log it...it clearly shows me the variable exists"* - Where are you calling `console.log()` from such that it works? – nnnnnn Sep 14 '17 at 03:36
  • @RyanAmundson Yes, by looking at the website scripts, the _vp is attached to the window. – Yankuan Zhang Sep 14 '17 at 03:38
  • can we see the code? – rjustin Sep 14 '17 at 03:40
  • @nnnnnn It's not working on `window.onLoad`, but does work when I do it in the dev tools console. And also, I tried to `console.log` inside the `setInterval` and also inside the `setTimeout`, but still, I was getting `undefined`. – Yankuan Zhang Sep 14 '17 at 03:42

1 Answers1

2

I believe this is an issue with your extensions isolated context, as detailed in another SO post about Context isolation who also faced an identical issue.

You should try the following Method quoted from the other answer:

Method 2b: Using a function

For a big chunk of code, quoting the string is not feasible. Instead of using an array, a function can be used, and stringified:

var actualCode = '(' + function() {
    // All code is executed in a local scope.
    // For example, the following does NOT overwrite the global `alert` method
    var alert = null;
    // To overwrite a global variable, prefix `window`:
    window.alert = null;
} + ')();';

var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.remove();

This method works, because the + operator on strings and a function converts all objects to a string. If you intend on using the code more than once, it's wise to create a function to avoid code repetition. An implementation might look like:

function injectScript(func) {
    var actualCode = '(' + func + ')();'
    ...
}
injectScript(function() {
   alert("Injected script");
});

Note: Since the function is serialized, the original scope, and all bound properties are lost!

var scriptToInject = function() {
    console.log(typeof scriptToInject);
};
injectScript(scriptToInject);
// Console output:  "undefined"
damanptyltd
  • 561
  • 4
  • 14