0

I tried to follow a tutorial to create a chrome extension (https://github.com/apatrida/kotlin-example-chrome-ext/blob/master/src/main/kotlin/com/collokia/browserplugin/chrome/GoogleImageTest/Extension.kt)

But it seems to be outdated for the latest kotlinjs version.

I'm trying to get the active tab URL with this code

fun getActiveTabURL(): String {
var url = "NotInit"
chrome.tabs.query(chrome_tabs_CurrentTabQuery()) {
    tabs ->
        println("tabsssssssss")
        url = tabs[0].url ?: "NULL"
        println("print url $url")

}
println(url)
return url

}

But it doesn't seems to work the tutorial uses keywords like native but I can't use them on my IDE (IntelliJ)

external object chrome {
object tabs {
    object Tab {
        var status: String?
        var index: Number
        var openerTabId: Number?
        var title: String?
        var url: String?
        var pinned: Boolean
        var highlighted: Boolean
        var windowId: Number
        var active: Boolean
        var favIconUrl: String?
        var id: Number
        var incognito: Boolean
    }
    public fun query(queryInfo: Any, callback: (result: Array<Tab>) -> Unit): Unit = definedExternally
}
}

class chrome_tabs_CurrentTabQuery() {
val active = true;
val currentWindow = true
}

Do you know what's wrong or a more recent example? I obtain a

Uncaught TypeError: Cannot read property 'query' of undefined
at getActiveTabURL (kotlinextensiontest.js:61)
at main (kotlinextensiontest.js:20)
at kotlinextensiontest.js:122
at kotlinextensiontest.js:125
Romain Bitard
  • 128
  • 11
  • 2
    The error says chrome.tabs is undefined. Are you trying to use it inside a content script? You can't do that. – wOxxOm Apr 23 '18 at 20:40

1 Answers1

0

It works if I click on the extension button, I was testing in on a separate tab and it wasn't working. Thanks for the help

Romain Bitard
  • 128
  • 11