JetBrains / kotlin-playground

Self-contained component to embed in websites for running Kotlin code
https://jetbrains.github.io/kotlin-playground/examples/
Apache License 2.0
452 stars 80 forks source link

Playground Supports Run from File, Not from Web Server #58

Open commonsguy opened 5 years ago

commonsguy commented 5 years ago

Given this page:

<html>
  <head>
    <script src="https://unpkg.com/kotlin-playground@1" data-selector=".kotlin-code"></script>
  </head>
  <body>
    <div class="kotlin-code" data-target-platform="java">
      <pre>
        <code class="hljs language-kotlin">
fun main() {
  println("hello, world!")
}
        </code>
      </pre>
    </div>
  </body>
</html>

If you load that page directly in a Web browser (file: scheme), the Playground shows the green "Run" button and allows you to execute the Kotlin code, showing the result.

If you load that page from a Web server, it does not. See, for example, this hosted copy of that page. Or, use your favorite local Web server (tested using ruby -run -ehttpd .).

I assume that this is some sort of cross-origin limitation.

However, since I do not control unpkg.com, nor do I control the default server JetBrains is supplying to execute the Kotlin scripts, I do not know what to configure on my server to make this work. I'm no expert, but from what I can see, it is JetBrain's server that would need to have the Access-Control-Allow-Origin header to say that my host is allowed to make requests of JetBrain's server.

What do I need to do to make this work? And I apologize in advance if the answer is obvious...

Thanks!

commonsguy commented 5 years ago

More specifically, we get this:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
https://try.kotlinlang.org/kotlinServer?type=getKotlinVersions. (Reason: CORS request did not succeed)

which then results in Cann't get kotlin version from server from here.

The URL cited in the error works, and this repo's instructions indicate that we should be able to talk to that server from our pages that demo Kotlin code using the playground.