我为计时器和提醒设置了谷歌浏览器的扩展程序。如果用户设置时间(提醒时间)等于当前时间,则会发出警报。这工作正常,但只有扩展是开放的。我想做那个警报火,甚至扩展没有打开。
这是一个开放的扩展程序。仅在扩展程序打开时发出警报。
这是我的代码jquery代码,用于检查时间并根据条件触发警报。
window.setInterval(function(){
// Get/Display CurrentTime
$(document).ready(function(){
// Check If Time set
$("#RemindTime").text(localStorage.getItem("newtime"));
var RemindTime = localStorage.getItem("newtime");
RemindTime = RemindTime+":00";
var today = new Date();
var cHour = (today.getHours()<10?'0':'') + today.getHours();
var cMin = (today.getMinutes()<10?'0':'') + today.getMinutes();
var cSec = (today.getSeconds()<10?'0':'') + today.getSeconds();
var currentTime = cHour+ ":" + cMin + ":" +cSec;
$("#CurrentTime").text(currentTime);
// Tell If Time Passed Or Not
if(RemindTime == currentTime)
{
// Alert if set time is equal to current time
alert("Your reminder is in progress");
$("#status").text("Your reminder is in progress");
}
if(localStorage.getItem("newtime") < currentTime)
{
$("#status").text("Your reminder is over");
}
if(localStorage.getItem("newtime") > currentTime)
{
// Calculate Difference to be pass
var start = currentTime;
var end = localStorage.getItem("newtime");
s = start.split(':');
e = end.split(':');
min = e[1]-s[1];
hour_carry = 0;
if(min < 0){
min += 60;
hour_carry += 1;
}
hour = e[0]-s[0]-hour_carry;
diff = hour + ":" + min;
$("#status").text("Your reminder will execute after "+hour+" Hour and "+min+" Min.");
}
// Set New Reminder time for new reminder
$("#submit").click(function(){
if($("#newtime").val())
{
var newtime = $("#newtime").val();
localStorage.setItem("newtime", newtime+":00");
}
else
{
var newtime = currentTime;
localStorage.setItem("newtime", newtime);
}
$("#RemindTime").text(newtime);
});
});
}, 100);
HTML
<body>
<div class="container">
Current Time : <span id="CurrentTime"></span>
</div>
<div class="container">
Remind Time : <span id="RemindTime"></span>
</div>
<br><hr>
<div class="container">
<span id="status"></span>
</div>
<br><hr>
<div class="container">
Change Time : <input type="time" name="newtime" id = "newtime" />
<br>
<input type = "button" id="submit" value="Set Time"/>
</div>
</body>
的manifest.json
{
"manifest_version": 2,
"name": "Remind Me :) ",
"description": "This extension shows a Simple Time Counter for slected time",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["popup.html"],
"persistent": false
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/"
]
}
答案 0 :(得分:1)
使用Event page(您已经拥有一个,但请参见下面的第一点)和alarms API:
为了清晰起见,我建议使用单独的背景页面文件
的manifest.json
"background": {
"scripts": ["eventPage.js"],
"persistent": false
},
"permissions": [
"alarms"
],
popup.js向活动页面发送消息:
// in 60 minutes
chrome.runtime.sendMessage({name: "alarm 1", time: {delayInMinutes: 60}});
// in 5 seconds
chrome.runtime.sendMessage({name: "alarm 2", time: {when: Date.now() + 5000}});
eventPage.js:
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.time) {
chrome.alarms.create(msg.name, msg.time);
}
});
chrome.alarms.onAlarm.addListener(function(alarm) {
alert(alarm.name + " fired, wake up!");
});
alert()
而不是 $s .= ('<td class="select0" id="start_'.$currentTasken.'" nowrap="nowrap" align="center" style="' . $style . '; border: none;" title="'.$AppUI->_('Double click to edit date').'">'
. $NewStartData . '</td>
检查the samples,您还可以在其中查看如何组织后台(事件)页面与扩展程序其他部分之间的通信。