WebSK docs (English version)

So you’re ready to create a website from a Skript application? WebSK is a simple Skript addon that allows you to do just that! It allows for sessions, query parameters and much more.

Please tell me at https://github.com/jensjeflensje/websk/issues if you have any suggestions.

Initial setup

Since WebSK 1.1.2, the web server creation is now through a single custom event (called scope), which contains a require port entry and a require on request section:

define webserver:
   port:
8000
   
on request:
       
# </>

For now, this code will only throw an error while reloading, since we don’t return any value to show to the user.

You just have to add a return statement and the content will be shown, for example:

define webserver:
   port:
8000
   
on request:
        return "My homepage!"

If you start up your script now and go to the IP of the server followed by the port in the browser (HTTP://(IP):(port)) you should see the output, being “My homepage!”.

Manage Path

Let’s imagine that you want to show different text according to the used path:

It’s very easy to do that, since we can get the request URL through the [the] [event( |-)](ur(l|i)|path) expression:

define webserver:
   port:
8000
   
on request:
       
if the path is "/":
            return "Welcome to the main page"
       
else if the path is "/panel/":
            return "Panel is under maintenance!"

And tada 🎉 ! It works and we have two different paths leading to two different pages!

However, if you tried another path, it would only show an empty and blank page, so let’s add a real 404 page:

define webserver:
        port:
8000
        
on request:
                
if the path is "/":
                        return "Welcome to the main page"
                
else if the path is "/panel/":
                        return "Panel is under maintenance!"
                
else: # It's not either "/" or "/panel/", so we cannot handle it ourselves.
                        return "Error 404: Unkown page"

Request methods

You can get the request method (GET, POST, etc) through the property expression method.

The event request can access through the [the] [event( |-)]request expression!

 Your code would look something like this:

on request:
        
if method of request = "POST":
                return "You sent a POST request!"
        return "You didn't send a post request!"

Request parameters

Most HTTP requests have query parameters. They’re the part behind the ? in your URL. A parameter is also the urlencoded body of a POST request. A sample url could be http://localhost:8000/checkonline?player=jensjeflensje. This is exactly what this tutorial will explain to you!

First, we have to add a new path to point to a new page itself, and by taking the previous code, we now have:

on request:
        
if the path is "/":
                return "Welcome to the main page"
        
else if the path is "/panel/":
                return "Panel is under maintenance!"
        
        
else if the path is "/checkonline/"
                
# </> Our code will come here
        
else: # It's not either "/" or "/panel/", so we cannot handle it ourselves.
                return "Error 404: Unkown page"

The expression to get a query parameter is parameter <parameter> of <request>. So the code should look something like this:

on request:
        
if the path is "/":
                return "Welcome to the main page"
        
else if the path is "/panel/":
                return "Panel is under maintenance!"
        
        
else if the path is "/checkonline/":
                loop all players:
                        
if name of loop-player is parameter "player" of the request:
                                return "Online"
                return "Offline"
        
else: # It's not either "/" or "/panel/", so we cannot handle it ourselves.
                return "Error 404: Unkown page"

Now this will return “Online” if the player is currently online, and “Offline” if the player is not currently online.


Templating

Of course, you don’t want to return a string of text as a response on a website. That doesn’t look cool. That’s why, with WebSK, you can use HTML (and CSS, JavaScript) to spice up your website. Templates should be located inside the Skript plugin folder, in a folder called templates. WebSK will not automatically create this folder; you have to create it yourself. The path would be plugins/Skript/templates/. A simple example of returning an HTML template would be:

on request:
 
return the file "index.html"

This will point to the file plugins/Skript/templates/index.html, keep that in mind!

The cool thing about templates is that you can put variables from Skript into the template, and even do for loops! The variable you create to use in a template has to be a local variable!!!! This means it should have an underscore as a prefix!!! This is a simple example of a function that will display the Minecraft server version on a webpage:

on request:
 
set {_version} to minecraft version
 
return the file "version.html"

We made our own parser to manage strings inside code marks. They are mustaches, so the code will always be between {{ and }}:

<h1>Version: {{version}}</h1>

As you can see, you have to put the variable name between {{ and }}  to use it inside an HTML template. You can put anything inside these HTML templates.

Show custom Expression

Since using variable only in the skript file itself can be hard sometimes, you can directly use custom Skript expression within the template file, just by preceding your code by show:

<h1>Version: {{show skript version}}</h1>

This will show the skript version exactly as the previous part, but without using any variable.

Don’t forget it’s always better to use variables for showing basic information such as this, since using expressions will make the loading time of the page longer (WebSK will need to parse in addition to get the expression, or the variable will just be got).

Loop in Templates

You can now make your own loops inside the template, and therefore the code inside both marks will be repeated every time (could be zero if the loop is empty).

As for the previous part, you can directly use the variable name (always under local) or use a custom expression.

Every loop has a tag that will be used to close the loop. You must close the loop when you start one!

To specify you are looping something, the key-word for code is loop or for:

{{loop all players -> players}}
        
<!-- The HTML code -->
{{/players}}

As you can see here, the expression we are looping is all players, and our loop has players as tags. The end of the loop is marked by the closing using the loop’s tag, here {{/players}}.

WebSK provides a custom expression only usable in loop to get the current loop value, and it’s loop entity (copy it exactly, it’s to avoid Skript’s conflict with the original loop value). This is an expression, and will be parsed as it, so we can simply show something according to it:

<ul>
{{loop all players -> players}}
        
<li>{{show name of loop entity}}</li>
{{/players}}
</ul>

And here you go, the page will now show the list of current online players of your server!

Conditions

You can also use Condition directly inside the template file, and if the specified condition is checked, then the code between will be shown.

It works like loop, mean each condition has its own tag to specify the closing moment:

{{if script "test.sk" is loaded -> scriptLoaded}}
        
<p>The script has been loaded!</p>
{{//scriptLoaded}}

This code will show the text only if the condition is passed, so here if the script test.sk is loaded.

As you can see, the closing mark of the condition has a double slash // to avoid confusion with the loop's closing mark. However, the tag definition is still the same.

Session data

Session data can be handled very easily in Skript. It is not persistent though! If the server restarts, the session data is gone. Only use this for actual session data, such as if a user is logged in. Do keep in mind that, currently, a session property can only be set to a string of text!

Change a property using Skript’s changer system:

set session property "logged_in" to "true"

Get a session property by using the expression:

session property "logged_in" of request

Or even clear the property:

clear session property "logged_in" of request