最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open a URL at a specific time with a Chrome Extension - Stack Overflow

programmeradmin4浏览0评论

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 plete 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 = ".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.

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 plete 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./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.

Share Improve this question asked May 16, 2013 at 9:22 QFDevQFDev 9,00814 gold badges64 silver badges85 bronze badges 4
  • You should use setInterval instead of setTimeout here, it's much simplier. It wouldn't solve your problem, though. – Pavel Strakhov Commented May 16, 2013 at 9:34
  • Thanks @Riateche, I think it should make the code cleaner though. I'll take a look at this. – QFDev Commented May 16, 2013 at 9:36
  • Why are you using chrome instead of wget or curl for this? – rsanchez Commented May 16, 2013 at 14:51
  • Because the Chrome Extension needs to interact with a specific 3rd party website and the initial call triggers a cascade of events where the extension steps through the site and runs various checks. – QFDev Commented May 16, 2013 at 15:09
Add a ment  | 

1 Answer 1

Reset to default 12

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 });
    }
});
发布评论

评论列表(0)

  1. 暂无评论