File size: 5,288 Bytes
61d39e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
> **NOTICE:** This documentation is new and might contain errors.
> Feel free to open a Github issue if you run into any problems.

# Service Scripts

## What is a Service Script?

Service scripts allow backend services to provide client-side code that
runs in Puter's GUI. This is useful if you want to make a mod or plugin
for Puter that has backend functionality. For example, you might want
to add a tab to the settings panel to make use of or configure the service.

Service scripts are made possible by the `puter-homepage` service, which
allows you to register URLs for additional javascript files Puter's
GUI should load.

## ES Modules - A Problem of Ordering

In browsers, script tags with `type=module` implicitly behave according
to those with the `defer` attribute. This means after the DOM is loaded
the scripts will run in the order in which they appear in the document.

Relying on this execution order however does not work. This is because
`import` is implicitly asynchronous. Effectively, this means these
scripts will execute in arbitrary order if they all have imports.

In a situation where all the client-side code is bundled with rollup
or webpack this is not an issue as you typically only have one
entry script. To facilitate loading service scripts, which are not
bundled with the GUI, we require that service scripts call the global
`service_script` function to access the API for service scripts.

## Providing a Service Script

For a service to provide a service script, it simply needs to serve
static files (the "service script") on some URL, and register that
URL with the `puter-homepage` service.

In this example below we use builtin functionality of express to serve
static files.

```javascript

class MyService extends BaseService {

    async _init () {

        // First we tell `puter-homepage` that we're going to be serving

        // a javascript file which we want to be included when the GUI

        // loads.

        const svc_puterHomepage = this.services.get('puter-homepage');

        svc_puterHomepage.register_script('/my-service-script/main.js');

    }



    async ['__on_install.routes'] (_, { app }) {

        // Here we ask express to serve our script. This is made possible

        // by WebServerService which provides the `app` object when it

        // emits the 'install.routes` event.

        app.use('/my-service-script',

            express.static(

                PathBuilder.add(__dirname).add('gui').build()

            )

        );

    }

}

```

## A Simple Service Script



```javascript

import SomeModule from "./SomeModule.js";



service_script(api => {

    api.on_ready(() => {

        // This callback is invoked when the GUI is ready



        // We can use api.get() to import anything exposed to

        // service scripts by Puter's GUI; for example:

        const Button = api.use('ui.components.Button');

        // ^ Here we get Puter's Button component, which is made

        // available to service scripts.

    });

});

```

## Adding a Settings Tab

Starting with the following example: 

```javascript

import MySettingsTab from "./MySettingsTab.js";



globalThis.service_script(api => {

    api.on_ready(() => {

        const svc_settings = globalThis.services.get('settings');

        svc_settings.register_tab(MySettingsTab(api));

    });

});

```

The module **MySettingsTab** exports a function for scoping the `api`
object, and that function returns a settings tab. The settings tab is
an object with a specific format that Puter's settings window understands.

Here are the contents of `MySettingsTab.js`:

```javascript

import MyWindow from "./MyWindow.js";



export default api => ({

    id: 'my-settings-tab',

    title_i18n_key: 'My Settings Tab',

    icon: 'shield.svg',

    factory: () => {

        const NotifCard = api.use('ui.component.NotifCard');

        const ActionCard = api.use('ui.component.ActionCard');

        const JustHTML = api.use('ui.component.JustHTML');

        const Flexer = api.use('ui.component.Flexer');

        const UIAlert = api.use('ui.window.UIAlert');



        // The root component for our settings tab will be a "flexer",

        // which by default displays its child components in a vertical

        // layout.

        const component = new Flexer({

            children: [

                // We can insert raw HTML as a component

                new JustHTML({

                    no_shadow: true, // use CSS for settings window

                    html: '<h1>Some Heading</h1>',

                }),

                new NotifCard({

                    text: 'I am a card with some text',

                    style: 'settings-card-success',

                }),

                new ActionCard({

                    title: 'Open an Alert',

                    button_text: 'Click Me',

                    on_click: async () => {

                        // Here we open an example window

                        await UIAlert({

                            message: 'Hello, Puter!',

                        });

                    }

                })

            ]

        });



        return component;

    }

});

```