0

I've tried delaying the code even if I execute window.functionName in the console before the code actually finishes it, I still get the same outcome (works when I directly enter window.functionName in the console but not when being executed from the javascript code).

Note this is all happening in the content-script of a chrome extension.

For full context, I have a Vue component which has a method that calls a function in another javascript file which eventually calls window.functionName -- so something like this:

SomeVueComponent.vue

<template>
  <button @click="buttonClicked">
</template>
<script>
  import { doSomething } from 'do-something.js';
   
  export default {
    ...
    methods: {
        buttonClicked() {
          doSomething();
        }
    }
  }
</script>

do-something.js

export function doSomething() {
  doFunctionName() {
     window.functionName(); // Returns "Uncaught ReferenceError: functionName is not defined"
  }
  ...
  whenElementIsShown() {
    // When the element was shown a script was injected to the page that sets window.functionName 
    // to something. I tried putting doFunctionName in a timeout and got the same results.
    doFunctionName();
  }
  
}

Whereas in the developer tools console...

window.functionName() // Returns "f (){...}"

I'm just not sure why window would be different in the two cases. Is it because of Vue? Is it some scoping issue? Is there a way to get the most recently updated window?

rrpa9
  • 1
  • 2
  • 1
    How does the call to `doSomething()` work? I can't see from your example how the button call ends up calling `whenElementIsShown` so that the function would be available in `window`. – leuquim Jul 13 '21 at 19:35
  • in the vue component when the button is clicked it calls the method `buttonClicked()`which calls `doSomething()` and then do something does stuff to the dom that ends up causing an element to show up. When that happens, a script is injected to the page that sets window.functionName – rrpa9 Jul 14 '21 at 00:00

1 Answers1

0

Turns out the window variable in the chrome extension is different than the page's window variable. More info here: Chrome extension - retrieving global variable from webpage

rrpa9
  • 1
  • 2