Adam Meyer
Adam Meyer

Reputation: 1525

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.

Upvotes: 4

Views: 3976

Answers (1)

Tom Ashworth
Tom Ashworth

Reputation: 2665

  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.

Upvotes: 4

Related Questions