Javascript – Open a URL at a specific time with a Chrome Extension

google-chrome-extensionjavascriptscheduled-tasks

I have a Chrome extension that I am using to run some batch jobs on a site in the early hours of the morning. I already have a content script in place to run when this URL is called in Chrome and complete all the necessary jobs. I'm having a problem now figuring out the best way to string this to a scheduler so this URL gets opened automatically in a Chrome tab at 3:00 am. I am running all this code in a dedicated Azure Virtual Machine so there won't be any user logged in when the script is set to run. When the new tab has finished it's work it will close automatically, this I have already handled.

So far I have experimented with using the Windows Task Scheduler to open Chrome with the URL passed in as an argument. This method however is proving to be somewhat unreliable!

If I leave Chrome open on the Virtual Machine, is there any native Chrome API I can use to open a tab at a specific time with a URL? I've also used the following javascript function in a separate page to trigger the URL to open, however I have no mechanism to test if it's already running in another tab so this code would result in endless new tabs being opened, unless it could be adapted to check if the URL is already open, and I think this will be outside the scope of Javascript on it's own.

var myUrlToCall = "http://www.myspecialurl.com/runme.html";

//IS 3 AM YET?
function runTimer() {
    setTimeout(function () {
        var dtNow = new Date();
        var hoursNow = dtNow.getHours() * 1;
        if (hoursNow >= 3) {
            //Open new window here (but how can I know if it's already open?)
            window.open(myUrlToCall);
        } else {
            runTimer();
        }
    }, 3000);
}

Any thoughts on this would be much appreciated.

Best Solution

The chrome.alarms API is a perfect fit for your use case, to be used at an event page.

function createAlarm() {
    var now = new Date();
    var day = now.getDate();
    if (now.getHours() >= 3) {
        // 3 AM already passed
        day += 1;
    }
    // '+' casts the date to a number, like [object Date].getTime();
    var timestamp = +new Date(now.getFullYear(), now.getMonth(), day, 3, 0, 0, 0);
    //                        YYYY               MM              DD  HH MM SS MS
    
    // Create
    chrome.alarms.create('3AMyet', {
        when: timestamp
    });
}

// Listen
chrome.alarms.onAlarm.addListener(function(alarm) {
    if (alarm.name === '3AMyet') {
        // Whatever you want
    }
});
createAlarm();

About creating the tab: The chrome.tabs.query method can be used to check for the existence of a tab, and open a new one if necessary. I assume that you want to focus an existing tab if needed:

var url = '...';
chrome.tabs.query({
    url: url
}, function(tabs) {
    if (tabs.length === 0) {
        chrome.tabs.create({ url:url, active: true });
    } else {
        // Focus first match
        chrome.tabs.update(tabs[0].id, { active: true });
    }
});