LaptopAdding websites

Adding websites

Put your own website in the Laptop's browser, at an address you pick. A page in your resource, a real web address, or your Tebex store.

On this page

A website is a page in the laptop's browser at an address you pick: a bank, a news site, a shop, a dark web forum. Players type its address or find it with the browser's search. It can be one of three kinds:

KindYou give itGood for
A pageui = 'web/index.html', a file in your resourceAnything of your own. It can ask your server for things, like an app
A web addressurl = 'https://...'A real website, if it allows being shown inside another page
A Tebex storetebex = '<public token>'Your server's store, as a shop window. See Your Tebex store

Your first website

A news site at paletonews.com.

Make a resource

A folder in your resources, with four files:

my_news/
├── fxmanifest.lua
├── server.lua
└── web/
    ├── index.html
    └── icon.svg

icon.svg is the icon in the browser's tab: any square SVG or PNG.

fxmanifest.lua

List the page and the icon in files, so the game can load them:

fxmanifest.lua
fx_version 'cerulean'
game 'gta5'
lua54 'yes'

server_script 'server.lua'

files {
    'web/index.html',
    'web/icon.svg',
}

server.lua

Register the site on the server, now and whenever the Laptop says it's ready (it may start after your resource, or restart):

server.lua
local function register()
    exports.tr_laptop:registerSite({
        domain = 'paletonews.com',
        title = 'Paleto News',
        ui = 'web/index.html',
        icon = 'web/icon.svg',
        search = {
            description = 'News from Paleto Bay.',
            keywords = { 'news', 'paleto' },
        },
    })
end

AddEventHandler('tr_laptop:ready', register)
if GetResourceState('tr_laptop') == 'started' then register() end

Leave out search and the search won't find it: players need to know the address.

web/index.html

A plain web page. Nothing else is needed for one that only shows something:

web/index.html
<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Paleto News</title>
</head>
<body>
  <h1>Paleto News</h1>
  <p>The bank on Paleto Boulevard was robbed again.</p>
</body>
</html>

The browser's tab shows the page's <title>.

Start it

Add it to server.cfg after tr_laptop, and start it (or restart the server):

server.cfg
ensure tr_laptop
ensure my_news

Open a laptop and type paletonews.com in its browser, or search for "news".

Asking your server

Give the site calls (in your register function from above), and load the Laptop's SDK on its page to use them. The page asks with laptop.call, and your function answers true and a result, or false and a reason:

server.lua
local HEADLINES = { 'Bank robbed again', 'Fishing boat comes home' }

exports.tr_laptop:registerSite({
    domain = 'paletonews.com',
    title = 'Paleto News',
    ui = 'web/index.html',
    calls = {
        headlines = function(src, laptop)
            return true, HEADLINES
        end,
    },
})
web/index.html
<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Paleto News</title>
  <script src="https://cfx-nui-tr_laptop/web/sdk/laptop.js"></script>
</head>
<body>
  <h1>Paleto News</h1>
  <ul id="headlines"></ul>

  <script>
    const headlines = document.getElementById('headlines');

    function show(list) {
      headlines.innerHTML = '';
      for (const headline of list) {
        const item = document.createElement('li');
        item.textContent = headline;
        headlines.append(item);
      }
    }

    laptop.ready(async () => show(await laptop.call('headlines')));
  </script>
</body>
</html>

src is the player, and laptop is { serial, coords }: the laptop they use. Calls are rate limited and only reach you while the player has that laptop open and logged in and stands next to it. Check everything else yourself: arguments, money, jobs. A reason you return is shown to the player (err.message on the page).

Sending to the page

exports.tr_laptop:sendSite(serial, domain, event, data) sends a message to your page on one laptop, if its player has the site up in the browser. The page gets it with laptop.on. The serial comes with every call, so keep the ones that asked:

server.lua
local readers = {}

-- in registerSite's calls:
headlines = function(src, laptop)
    readers[laptop.serial] = true
    return true, HEADLINES
end,

-- when there's news:
local function breaking(headline)
    for serial in pairs(readers) do
        exports.tr_laptop:sendSite(serial, 'paletonews.com', 'breaking', headline)
    end
end
web/index.html
laptop.on('breaking', (headline) => {
  document.title = `Breaking: ${headline}`;
  show([headline]);
});

A laptop that has closed or left the site doesn't get it, and nothing goes wrong.

More pages

Put more pages next to the first one (web/sports.html) and link to them as on any website: <a href="sports.html">. The browser shows their address (paletonews.com/sports.html), puts them in back and forward, and players can type it. Links with ? (?team=paleto) and # work too.

A link to another address (https://mazebank.com) opens it in the browser, with target="_blank" in a new tab. The tab's title follows your page's <title>, also when you change document.title; without one it shows your site's title.

Who can open it

canOpen(src, laptop) decides who gets in. Return false and a reason, and the browser shows the reason instead of the page. It's checked when the site opens and on every call. This one uses tr_core's TR.player, so the resource loads it with shared_script '@tr_core/init.lua' in its fxmanifest:

server.lua
exports.tr_laptop:registerSite({
    domain = 'pillboxstaff.net',
    title = 'Pillbox Staff',
    ui = 'web/index.html',
    canOpen = function(src, laptop)
        if not TR.player.hasJob(src, 'ambulance') then
            return false, 'Staff only. Sign in at the front desk.'
        end
        return true
    end,
})

Your app's own website

Every app gets a website made by the Laptop. To give an app a site of your own instead, register a site with the app's id as app:

server.lua
exports.tr_laptop:registerSite({
    domain = 'notesapp.com',
    title = 'Notes',
    ui = 'web/site.html',
    app = 'notes',
})

Your page draws its own download button. laptop.site.app says whether it's installed, and laptop.download() downloads it (asking first when it costs money), or opens it once it is:

web/site.html
<button id="get">Download</button>

<script src="https://cfx-nui-tr_laptop/web/sdk/laptop.js"></script>
<script>
  const button = document.getElementById('get');
  const words = { none: 'Download', installing: 'Downloading...', installed: 'Open' };

  function update() {
    const app = laptop.site.app;
    if (!app) return;
    button.textContent = words[app.status];
    button.disabled = app.status === 'installing';
  }

  laptop.ready(update);
  laptop.onState(update);
  button.onclick = () => laptop.download();
</script>

Your Tebex store

Give a site your Tebex webstore's public token, and the Laptop draws the store itself: its name (or the logo you uploaded to Tebex), a section per category with each package's picture, price and first line, and a page per package with its description.

server.lua
exports.tr_laptop:registerSite({
    domain = 'myserver.com',
    title = 'My Server Store',
    tebex = 'abcd-0123456789abcdef0123456789abcdef01234567',
    search = { description = 'Ranks, cars and more for My Server.' },
})

The public token is on Tebex's API Keys page. It's only ever read on your server: players never get it.

It's a shop window only. Nothing can be bought on the laptop, so players buy on your real store as before. The server reads the store from Tebex, keeps it for 5 minutes, and shows the last copy it has while Tebex doesn't answer.

Without Lua: config.lua

A site needs no resource of its own. Put its files in a folder in tr_laptop's sites/ and list it in tr_laptop's config.lua:

config.lua
sites = {
    {
        domain = 'paletonews.com',
        title = 'Paleto News',
        ui = 'sites/paletonews/index.html',
        search = { description = 'News from Paleto Bay.' },
    },
    {
        domain = 'mystore.com',
        title = 'My Store',
        url = 'https://example.com',
    },
    {
        domain = 'myserver.com',
        title = 'My Server Store',
        tebex = '<your public token>',
    },
},

Restart tr_laptop after adding one. For calls or canOpen, register the site from a resource of your own instead: they're code, and the config is for values.

Fields

Field
domainrequiredIts address, like mazebank.com (no path). horizon.com and compass.com are the search engines'
titlerequiredIts name in the browser's tab, up to 48 characters
ui, url or tebexone of themA page in your resource (listed in your files), an https:// address, or your Tebex public token
iconA file in your resource or an https:// address, for its tab
search{ summary?, description?, keywords? }: the search finds it by these and its title. Without it, only its address opens it
appAn app's id: this site is that app's website, instead of the one the Laptop makes
callsWhat its page can ask the server: name = function(src, laptop, ...) returning true, result or false, reason
canOpen(src, laptop)Return false, reason to keep a player out. Checked when it opens and on every call

registerSite returns true, or false and what's wrong (also printed in the server console). Registering the same domain again from your resource replaces it; another resource's is refused. unregisterSite(domain) takes one down, and when your resource stops its sites go.

The SDK on a website

A website's page uses the same SDK as an app's page: the player's light or dark theme, sounds, text size and the laptop's cursor all work the same. There's no state on a website (laptop.state is null). It also has:

laptop.call(name, ...args)Runs one of the site's calls. Resolves to its result, or rejects with an Error whose .message is the reason to show
laptop.on(event, fn)Messages from your server (sendSite)
laptop.site{ domain, title, app }. app is null, or the app this site is the page of: { id, label, price, status, remaining }, status being 'none', 'installing' or 'installed' and remaining the seconds its download has left. onState fires when it changes
laptop.download()Downloads that app (asking first when it costs money), or opens it once it's installed
laptop.open('mazebank.com/loans')Goes to an address in the browser's tab
laptop.notify(title, body?, error?)A notification under the browser's name

The browser's keys (Ctrl+L, Alt+Left, F5...) work on your page unless it uses them itself.

Was this page helpful?

Still stuck? Open a ticket on Discord.Privacy