admin管理员组文章数量:1026195
I am trying to add some code to a chrome extension, that will refresh a specific page at a certain time of the day. I've found coding that involves adding meta tags to the header, or tags to the body but I cant edit the page's html so this is not possible. This is the code I have of now but it doesnt seem to be working --
//60000 milliseconds is 1 minute
window.setInterval("checkForRefresh()", 60000);
function checkForRefresh() {
var now = new Date();
if (now.getHours() == 14 && now.getMinutes() == 24) {
window.location.reload();
}
}
It seems to have worked a couple of times, but now it just stopped working. Not sure what went wrong.
Any ideas on how to fix this?
I am trying to add some code to a chrome extension, that will refresh a specific page at a certain time of the day. I've found coding that involves adding meta tags to the header, or tags to the body but I cant edit the page's html so this is not possible. This is the code I have of now but it doesnt seem to be working --
//60000 milliseconds is 1 minute
window.setInterval("checkForRefresh()", 60000);
function checkForRefresh() {
var now = new Date();
if (now.getHours() == 14 && now.getMinutes() == 24) {
window.location.reload();
}
}
It seems to have worked a couple of times, but now it just stopped working. Not sure what went wrong.
Any ideas on how to fix this?
Share Improve this question edited Dec 10, 2013 at 19:57 730wavy asked Dec 10, 2013 at 18:45 730wavy730wavy 7041 gold badge20 silver badges65 bronze badges 6- Are you using a content-script for that ? Also, keep in mind that the user might be in the middle of doing something on the page when the refresh happens and it can be veeery annoying ! – gkalpak Commented Dec 10, 2013 at 19:19
- Yeah Im using the content script. The user wont be doing nothing at the time because they will be awaiting a timer countdown to finish and for it to be a certain time of day where they will need to refresh the page exactly at that time of day. – 730wavy Commented Dec 10, 2013 at 19:27
- OK, then :) The solutions below should do the trick. BTW, if you want to develop an extension, you'll save a great deal of time by debugging easily resolved issues by yourself (instead of asking here). And when it es to debugging an extension the Developer Console is your best friend ! – gkalpak Commented Dec 10, 2013 at 19:30
- Ok thanks, Im familiar with the developer console so I think now that I've injected the jquery it should show up. – 730wavy Commented Dec 10, 2013 at 19:57
- You don't really need jQuery for this, but it won't hurt much either, I guess... – gkalpak Commented Dec 10, 2013 at 20:04
3 Answers
Reset to default 3Use location.reload(true)
That'll reload the page and bypass the cache, however, if you don't want to bypass the cache, set then use location.reload(false)
Take a look at this.
window.refresh()
is not a function. Try window.location.reload()
instead.
For a more reliable and resource friendly solution, you could use the chrome.alarms
API in a non-persistent background page (a.k.a. event page). E.g.:
In manifest.json:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"offline_enabled": false,
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"permissions": [
"alarms",
"tabs"
]
}
In background.js:
var refreshAlarmName = 'refreshAlarm';
/* Refresh at: 10 Dec 2013, 22:11:00 UTC */
var utcYear = 2013;
var utcMonth = 11; // <-- watch out: this is zero-based
var utcDate = 10;
var utcHours = 22;
var utcMinutes = 11;
/* Convert a date (year, month, dayOfMonth, hours, mins)
* to milliseconds past the epoch */
function at(year, month, dayOfMonth, hours, mins) {
var date = new Date();
date.setUTCFullYear(year);
date.setUTCMonth(month);
date.setUTCDate(dayOfMonth);
date.setUTCHours(hours);
date.setUTCMinutes(mins);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return date.getTime();
}
chrome.alarms.create(refreshAlarmName, {
when: at(utcYear, utcMonth, utcDate, utcHours, utcMinutes)
});
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name !== refreshAlarmName) {
return;
}
chrome.tabs.query({ url: "*://www.eastbay./*" }, function(tabs) {
tabs.forEach(function(tab) {
chrome.tabs.reload(tab.id, { bypassCache: true });
});
});
});
Important note:
According to the docs:
For performance reasons, the alarm may have been delayed an arbitrary amount beyond this.
According to my experience, that arbitrary amount is hardly ever more than negligible, but if the exact timing is of the essence, you might want to consider a more sophisticated mechanism, where the background page fire's an alarm a couple of minutes before the target time, which injects a content script that sets a, say, 30secs interval to check for the target time and refresh.
I am trying to add some code to a chrome extension, that will refresh a specific page at a certain time of the day. I've found coding that involves adding meta tags to the header, or tags to the body but I cant edit the page's html so this is not possible. This is the code I have of now but it doesnt seem to be working --
//60000 milliseconds is 1 minute
window.setInterval("checkForRefresh()", 60000);
function checkForRefresh() {
var now = new Date();
if (now.getHours() == 14 && now.getMinutes() == 24) {
window.location.reload();
}
}
It seems to have worked a couple of times, but now it just stopped working. Not sure what went wrong.
Any ideas on how to fix this?
I am trying to add some code to a chrome extension, that will refresh a specific page at a certain time of the day. I've found coding that involves adding meta tags to the header, or tags to the body but I cant edit the page's html so this is not possible. This is the code I have of now but it doesnt seem to be working --
//60000 milliseconds is 1 minute
window.setInterval("checkForRefresh()", 60000);
function checkForRefresh() {
var now = new Date();
if (now.getHours() == 14 && now.getMinutes() == 24) {
window.location.reload();
}
}
It seems to have worked a couple of times, but now it just stopped working. Not sure what went wrong.
Any ideas on how to fix this?
Share Improve this question edited Dec 10, 2013 at 19:57 730wavy asked Dec 10, 2013 at 18:45 730wavy730wavy 7041 gold badge20 silver badges65 bronze badges 6- Are you using a content-script for that ? Also, keep in mind that the user might be in the middle of doing something on the page when the refresh happens and it can be veeery annoying ! – gkalpak Commented Dec 10, 2013 at 19:19
- Yeah Im using the content script. The user wont be doing nothing at the time because they will be awaiting a timer countdown to finish and for it to be a certain time of day where they will need to refresh the page exactly at that time of day. – 730wavy Commented Dec 10, 2013 at 19:27
- OK, then :) The solutions below should do the trick. BTW, if you want to develop an extension, you'll save a great deal of time by debugging easily resolved issues by yourself (instead of asking here). And when it es to debugging an extension the Developer Console is your best friend ! – gkalpak Commented Dec 10, 2013 at 19:30
- Ok thanks, Im familiar with the developer console so I think now that I've injected the jquery it should show up. – 730wavy Commented Dec 10, 2013 at 19:57
- You don't really need jQuery for this, but it won't hurt much either, I guess... – gkalpak Commented Dec 10, 2013 at 20:04
3 Answers
Reset to default 3Use location.reload(true)
That'll reload the page and bypass the cache, however, if you don't want to bypass the cache, set then use location.reload(false)
Take a look at this.
window.refresh()
is not a function. Try window.location.reload()
instead.
For a more reliable and resource friendly solution, you could use the chrome.alarms
API in a non-persistent background page (a.k.a. event page). E.g.:
In manifest.json:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"offline_enabled": false,
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"permissions": [
"alarms",
"tabs"
]
}
In background.js:
var refreshAlarmName = 'refreshAlarm';
/* Refresh at: 10 Dec 2013, 22:11:00 UTC */
var utcYear = 2013;
var utcMonth = 11; // <-- watch out: this is zero-based
var utcDate = 10;
var utcHours = 22;
var utcMinutes = 11;
/* Convert a date (year, month, dayOfMonth, hours, mins)
* to milliseconds past the epoch */
function at(year, month, dayOfMonth, hours, mins) {
var date = new Date();
date.setUTCFullYear(year);
date.setUTCMonth(month);
date.setUTCDate(dayOfMonth);
date.setUTCHours(hours);
date.setUTCMinutes(mins);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return date.getTime();
}
chrome.alarms.create(refreshAlarmName, {
when: at(utcYear, utcMonth, utcDate, utcHours, utcMinutes)
});
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name !== refreshAlarmName) {
return;
}
chrome.tabs.query({ url: "*://www.eastbay./*" }, function(tabs) {
tabs.forEach(function(tab) {
chrome.tabs.reload(tab.id, { bypassCache: true });
});
});
});
Important note:
According to the docs:
For performance reasons, the alarm may have been delayed an arbitrary amount beyond this.
According to my experience, that arbitrary amount is hardly ever more than negligible, but if the exact timing is of the essence, you might want to consider a more sophisticated mechanism, where the background page fire's an alarm a couple of minutes before the target time, which injects a content script that sets a, say, 30secs interval to check for the target time and refresh.
本文标签: javascriptAuto Refresh a Page at Specific TimeStack Overflow
版权声明:本文标题:javascript - Auto Refresh a Page at Specific Time - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629704a2160085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论