Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a URL at a specific time with a Chrome Extension

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.

like image 525
QFDev Avatar asked May 16 '13 09:05

QFDev


People also ask

How do I open a link at a specific time?

All you have to do is click on the Chrome extension option that looks like a clock. At the bottom of the pop-up window is all the sites you have set up to open at a particular time. Place the mouse cursor over the site you want to make changes to. You'll automatically see the arrow keys appear.

What is a schedule URL?

Also called an automated scheduling link, a calendar URL is a unique URL that allows anyone with the link to schedule time on your calendar based on your availability. If you use a calendar URL tool like Calendly, for example, your link will typically look like this: calendly.com/yourname.

How do I add an extension to a URL?

Find and select the extension you want. Click Add to Chrome. Some extensions will let you know if they need certain permissions or data. To approve, click Add extension.


1 Answers

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 });
    }
});
like image 114
Rob W Avatar answered Oct 23 '22 08:10

Rob W