hilbert / hilbert-docker-images

Application with a dynamic choice of docker containers to run
Apache License 2.0
22 stars 8 forks source link

Preloading the heartbeat script #55

Closed porst17 closed 6 years ago

porst17 commented 6 years ago

This is a copy of https://github.com/IMAGINARY/kiosk-browser/issues/3:

The kiosk browser now has a --preload option that allows to load (almost) arbitrary nodejs-enabled JavaScript code before a page is loaded. The idea is to use this to inject a script for sending heartbeats into arbitrary web pages.

npm run start -- -v -v -d --preload preload.js -l https://www.heise.de?HB_APP_ID=bla

Using this as preload.js

{
    // include modules
    const fs = require('fs');
    const path = require('path');
    const vm = require('vm');

    // include the heartbeat JS library
    var content = fs.readFileSync(path.join(__dirname, 'hilbert-heartbeat.js'),'utf8');
    vm.runInThisContext(content,{filename:'hilbert-heartbeat.js'});

    // wait for the document finish loading and ...
    var webContents = require('electron').remote.getCurrentWindow().webContents;
    webContents.on('did-finish-load',() => {
        // ... define the heartbeat configuration
        const heartbeatCfg = {
            url: Heartbeat.getPassedUrl('http://localhost:8881'),
            appId: Heartbeat.getPassedAppId('test_app'),
            interval: 1000,
            sendInitCommand: true,
            sendDoneCommand: true,
            debugLog: console.log
        };

        // ... init the heartbeat
        window.heartbeat = new Heartbeat(heartbeatCfg);
    });
}

works quite well. You can see the heartbeat script in action in the console.

My question is where to put the preload.js. It is separate from the kiosk browser and currently only makes sense in the context of hilbert.

Should we put it into this repository (since the Dockerfile the there as well) and separate the kiosk browser and hilbert related parts later?

Note: In order to make the above preload.js work, you need at least this version of heartbeat.js (there was a bug with delayed loading of the library). So if we put the preload.js here, we also need to update hilbert-heartbeat.js.

The repository subfolder and the Dockerfile of the kiosk browser need to be updated.

malex984 commented 6 years ago

TODO: finishhb_kiosk.sh

malex984 commented 6 years ago

the general pre-loading works fine in all my tests! @porst17 i will add a special script hb_kiosk.sh into hilbert/kiosk image in order to enable the special heartbeat-library pre-loading due to Hilbert's needs. The current script is available at https://gist.github.com/malex984/f82078da605c1249b8e7a43895db1d3e for checking