0

I'm trying to get the currently displayed url from an Angular2 Typescript Chrome Extension. I've read this in order to get the url, and this regarding how to call Js functions in Ts. I also set allowJs to true in my tsconfig.json.

Here's my code :

  ngOnInit(){
    declare var tabs = chrome.tabs.query({params...},function(tabs) {
      return tabs[0];
    });
    console.log(tabs());
  }

This doesn't compile unless I remove the log.

I also tried not to use a function expression call :

declare chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
      function(tabs){}
    );

But it didn't work at all. I was wondering whether it could be async related, because the code is in the OnInit.

Any ideas ?

Thanks !

Community
  • 1
  • 1
  • Why do you use `declare`? I'm pretty sure that it's not what you want. Declaring a variable means "there's a variable with this name and type, but it's defined somewhere else, so no worries dear compiler". But in your example you are assigning the value right there. – Nitzan Tomer Jul 27 '16 at 10:05
  • Well, one of the links recommends to do so when calling Js functions in Ts. – Standaa - Remember Monica Jul 27 '16 at 10:11
  • 2
    There's not really such a thing as "calling js from ts" as typescript is javascript. The problem is that the compiler won't recognise `chrome.tabs.query` because it's not defined for it, so you can either declare that or use a definition file which define it for you. BUT when you assign the result of such a function to a variable, that variable shouldn't be `declared` but it should actually be there. – Nitzan Tomer Jul 27 '16 at 10:27
  • 1
    @Xan, question edited. Thanks for the notice. – Standaa - Remember Monica Jul 27 '16 at 13:39

1 Answers1

1

Solved it thanks to Nitzan's comment. Here is the code :

declare private getUrl(){
    chrome.tabs.query({}, function(tabs) {
       console.log(tabs[0]);
    });
  }

ngOnInit(){
  this.getUrl();
}