Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension to monitor a kiosk and reload if needed

I have a kiosk that processes portraits of people. It is running in chrome.

One out of 200 people who use the kiosk end up with it freezing during the process. When it freezes, I never get the "ah snap" and the page just kinda hangs.

I was wondering if it is possible to make an extension to monitor the page and check for a heartbeat - If it is not seen after 30 seconds reload the window.

Any help would be great.

like image 976
Adam Meyer Avatar asked Jul 22 '12 09:07

Adam Meyer


1 Answers

  1. Have a content script in the kiosk page send a message every X seconds back to the background page

    In the content script:

    var port = chrome.extension.connect({ name: "keep-alive" });
    
    port.postMessage('is-alive', { alive: true });
    
    setInterval(function () {
        port.postMessage('is-alive', { alive: true });
    }, 1000 * 15);
    
  2. If the background page detects the message hasn't come back after a certain time then reload the tab

    In the background page:

    var last = Date.now();
    var interval = 1000 * 15;
    
    chrome.extension.onConnect.addListener(function (port) {
        if (port.name === 'keep-alive') {
            port.onMessage.addListener(function (data) {
                if (data.type === 'is-alive' && data.payload.alive === true) {
                    last = Date.now();
                }
            });
        }
    });
    
    setInterval(function () {
        if (Date.now() - last > interval) {
            // Reload the tab...
        }
    }, interval);
    

For information about reloading the tab, see the chrome.tabs documentation. You will need to add tabs to your permission list in the manifest.

If you'd like to know more about message passing, see the Messaging docs.

like image 104
Tom Ashworth Avatar answered Sep 30 '22 06:09

Tom Ashworth