1

I am currently facing a weird issue with developing a simple chrome extension. Basically, I want to build an extension that saves the currently focused URL. However, seems like URL is not saved in variables that I defined.

I have tried to use an array to store it, and also a variable.

consoleClick() {
      var urlss = []
      var getTab = function(tab){
        urlss.push(tab)
        alert(tab)
      };
      chrome.tabs.query({'active':true}, function(tabs){
        getTab(tabs[0].url)
      });
      console.log(urlss)
      this.$store.commit('addUrl',urlss[0])
      console.log(this.$store.state.urls[0])
    }

Here, once button is clicked, with "chrome.tabs.query()" I was able to get currently focused url. and I saved it to "urlss", and checked it with console.log(urlss). I could see the url in console log, however, when I tried to access to url with urlss[0], it says urlss[0] is "undefined" even though I can see that

[]
0: "https://stackoverflow.com/posts/57089954/edit"
length: 1
__proto__: Array(0)

in log. This leads to push "undefined" to my vuex storage.

I also tried to push it to vuex storage right away in chrome.tabs.query(), however it gives me that $store is not defined.

I know that my explanation is pretty messy but I need help from the community! Thanks in advance

Crazy9
  • 9
  • 2
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – wOxxOm Jul 18 '19 at 08:18

0 Answers0