gpt4 book ai didi

javascript - Chrome 扩展脚本仅在显示弹出窗口时运行

转载 作者:行者123 更新时间:2023-11-30 12:40:16 25 4
gpt4 key购买 nike

我正在尝试为 Google Chrome 开发类似于约会提醒扩展程序的东西。该扩展包含一个浏览器操作,单击该操作会显示一个 HTML 页面,用户可以在其中输入约会的详细信息。在约会开始时,警报应显示约会的名称。

此扩展确实有效,但前提是浏览器操作处于事件状态。如果关闭浏览器操作,则不会在指定时间弹出警报。我不明白是什么导致了这个问题。

list .json

{
"manifest_version": 2,
"name": "Appointment",
"description": "",
"version": "1.0",
"background": {
"scripts": [
"js/jquery.js",
"js/Utils.js",
"js/Controller.js"
]
},
"permissions": [
"storage",
"alarms",
],
"browser_action": {
"default_icon": "icon-appointment.png",
"default_popup": "popup.html"
}
}

Controller.js

function loadAppointments(allAppointmentsLoadedCallback){


chrome.storage.sync.get("test_appointments",function(items){
allAppointments = [];
if("test_appointments" in items){
sAllAppointments = items["test_appointments"];
allAppointments = JSON.parse(sAllAppointments);
}

allAppointmentsLoadedCallback(allAppointments);
});
}



function getAppointmentScheduledAt(aUnixTime,appointmentFoundCallback){
console.log('Finding Appointment for ',new Date(aUnixTime));
loadAppointments(function(allAppointments){
for(var i=0;i<allAppointments.length;i++){
var anAppointment = allAppointments[i];
var startTime = new Date(anAppointment.startTime).getTime();
console.log('Start time is ',startTime,' and alarm time is',aUnixTime);
if(startTime == aUnixTime){
appointmentFoundCallback(anAppointment);
return;
}
}
appointmentFoundCallback(null);
});
}

chrome.alarms.onAlarm.addListener(function(anAlarm){

getAppointmentScheduledAt(anAlarm.scheduledTime,function(appointment){
if(appointment){
console.log('Found one');
alert(appointment.title);
}else{
console.log('No appointment found.');
}
});
});

最佳答案

我假设您在弹出页面中调用 chrome.alarms.create。这会向弹出窗口的 defaultView (window) 注册一个警报。它在弹出窗口关闭时被销毁。

需要将闹钟注册到后台页面的 View :

chrome.runtime.getBackgroundPage(function(bg) {
bg.chrome.alarms.create( ... );
});

关于javascript - Chrome 扩展脚本仅在显示弹出窗口时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24713144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com