I'm trying to use the same Kotlin/JS code in two e...
# javascript
j
I'm trying to use the same Kotlin/JS code in two environments, one of which has access to the "chrome.scripting" namespace and one in which "chrome.scripting" is undefined. I made an external declaration in a file with
@file:JsQualifier("chrome.scripting")
and an
external fun executeScript(...)
method, and it works fine when the namespace exists. However, if it's undefined, the generated JS fails to load even if I never actually try calling the method, since the code tries to declare a variable equal to "chrome.scripting.executeScript", which breaks. Is there a way to make such an external declaration where it does not attempt to initialize this variable unless/until the method is actually called?
r
Not that I know of. A potential (rather convoluted) workaround would be to use the file-per-module JS generation, and only load the module with the external declaration if you have access to
chrome.scripting
. But that requires manual loading, and I'm not sure how messy it would get
j
shame, since I can just do
js("chrome.scripting.executeScript(...)")
and that works fine