Chrome扩展-使用javascript定期运行,并永久记录数据
目前,我有一个脚本,当点击右上角托盘中的图像时(仅针对一个特定的允许网站),它会扫描HTML页面,然后输出一些值。这种扫描和输出是单个JS文件中的一个函数,称为say checkData.JSChrome扩展-使用javascript定期运行,并永久记录数据,javascript,database,google-chrome,google-chrome-extension,Javascript,Database,Google Chrome,Google Chrome Extension,目前,我有一个脚本,当点击右上角托盘中的图像时(仅针对一个特定的允许网站),它会扫描HTML页面,然后输出一些值。这种扫描和输出是单个JS文件中的一个函数,称为say checkData.JS 即使用户没有主动使用选项卡,但它是打开的,是否有可能每10秒自动运行一次脚本,并将数据记录到扩展中稍后可以访问的某个位置?这是因为HTML页面在不断变化。我想我会使用报警或事件页面,但我不确定如何集成它们。Chrome将重复报警的频率限制为每分钟最多一次。如果可以的话,下面是操作方法: 请参见关于如何设置
即使用户没有主动使用选项卡,但它是打开的,是否有可能每10秒自动运行一次脚本,并将数据记录到扩展中稍后可以访问的某个位置?这是因为HTML页面在不断变化。我想我会使用报警或事件页面,但我不确定如何集成它们。Chrome将重复报警的频率限制为每分钟最多一次。如果可以的话,下面是操作方法: 请参见关于如何设置事件页面的 在background.js中,您可以执行以下操作:
// event: called when extension is installed or updated or Chrome is updated
function onInstalled() {
// CREATE ALARMS HERE
...
}
// event: called when Chrome first starts
function onStartup() {
// CREATE ALARMS HERE
...
}
// event: alarm raised
function onAlarm(alarm) {
switch (alarm.name) {
case 'updatePhotos':
// get the latest for the live photo streams
photoSources.processDaily();
break;
...
default:
break;
}
}
// listen for extension install or update
chrome.runtime.onInstalled.addListener(onInstalled);
// listen for Chrome starting
chrome.runtime.onStartup.addListener(onStartup);
// listen for alarms
chrome.alarms.onAlarm.addListener(onAlarm);
// create a daily alarm to update live photostreams
function _updateRepeatingAlarms() {
// Add daily alarm to update 500px and flickr photos
chrome.alarms.get('updatePhotos', function(alarm) {
if (!alarm) {
chrome.alarms.create('updatePhotos', {
when: Date.now() + MSEC_IN_DAY,
periodInMinutes: MIN_IN_DAY
});
}
});
}
创建重复报警的操作如下所示:
// event: called when extension is installed or updated or Chrome is updated
function onInstalled() {
// CREATE ALARMS HERE
...
}
// event: called when Chrome first starts
function onStartup() {
// CREATE ALARMS HERE
...
}
// event: alarm raised
function onAlarm(alarm) {
switch (alarm.name) {
case 'updatePhotos':
// get the latest for the live photo streams
photoSources.processDaily();
break;
...
default:
break;
}
}
// listen for extension install or update
chrome.runtime.onInstalled.addListener(onInstalled);
// listen for Chrome starting
chrome.runtime.onStartup.addListener(onStartup);
// listen for alarms
chrome.alarms.onAlarm.addListener(onAlarm);
// create a daily alarm to update live photostreams
function _updateRepeatingAlarms() {
// Add daily alarm to update 500px and flickr photos
chrome.alarms.get('updatePhotos', function(alarm) {
if (!alarm) {
chrome.alarms.create('updatePhotos', {
when: Date.now() + MSEC_IN_DAY,
periodInMinutes: MIN_IN_DAY
});
}
});
}
Chrome将重复报警的频率限制为每分钟最多一次。如果可以的话,下面是操作方法: 请参见关于如何设置事件页面的 在background.js中,您可以执行以下操作:
// event: called when extension is installed or updated or Chrome is updated
function onInstalled() {
// CREATE ALARMS HERE
...
}
// event: called when Chrome first starts
function onStartup() {
// CREATE ALARMS HERE
...
}
// event: alarm raised
function onAlarm(alarm) {
switch (alarm.name) {
case 'updatePhotos':
// get the latest for the live photo streams
photoSources.processDaily();
break;
...
default:
break;
}
}
// listen for extension install or update
chrome.runtime.onInstalled.addListener(onInstalled);
// listen for Chrome starting
chrome.runtime.onStartup.addListener(onStartup);
// listen for alarms
chrome.alarms.onAlarm.addListener(onAlarm);
// create a daily alarm to update live photostreams
function _updateRepeatingAlarms() {
// Add daily alarm to update 500px and flickr photos
chrome.alarms.get('updatePhotos', function(alarm) {
if (!alarm) {
chrome.alarms.create('updatePhotos', {
when: Date.now() + MSEC_IN_DAY,
periodInMinutes: MIN_IN_DAY
});
}
});
}
创建重复报警的操作如下所示:
// event: called when extension is installed or updated or Chrome is updated
function onInstalled() {
// CREATE ALARMS HERE
...
}
// event: called when Chrome first starts
function onStartup() {
// CREATE ALARMS HERE
...
}
// event: alarm raised
function onAlarm(alarm) {
switch (alarm.name) {
case 'updatePhotos':
// get the latest for the live photo streams
photoSources.processDaily();
break;
...
default:
break;
}
}
// listen for extension install or update
chrome.runtime.onInstalled.addListener(onInstalled);
// listen for Chrome starting
chrome.runtime.onStartup.addListener(onStartup);
// listen for alarms
chrome.alarms.onAlarm.addListener(onAlarm);
// create a daily alarm to update live photostreams
function _updateRepeatingAlarms() {
// Add daily alarm to update 500px and flickr photos
chrome.alarms.get('updatePhotos', function(alarm) {
if (!alarm) {
chrome.alarms.create('updatePhotos', {
when: Date.now() + MSEC_IN_DAY,
periodInMinutes: MIN_IN_DAY
});
}
});
}
要求有点高。。不清楚的。“扫描”有什么作用?写作有多重要?是否担心篡改(或更改/清除浏览器数据)?等。没有篡改,HTML没有改变。扫描只是执行document.firstChild并将一些数据转换为字符串格式。定期检查的要点是,随着HTML的更改,定期将该字符串写入文件(每10秒一次就足够了)。。不清楚的。“扫描”有什么作用?写作有多重要?是否担心篡改(或更改/清除浏览器数据)?等。没有篡改,HTML没有改变。扫描只是执行document.firstChild并将一些数据转换为字符串格式。定期检查的全部要点是,随着HTML的更改,定期将该字符串写入文件(每10秒一次就足够了)。这太完美了!我的情况下,MIN_IN_DAY会是1吗?onInstalled和onStartup会启动警报吗?你是怎么做到的?@Joebb在1号上回答是的
onInstalled
和启动时的onStartup
将调用类似\u updateRepeatingAlarms
的函数。调用chrome.alarms.create
会自动启动报警。太棒了!一天中的毫秒是1000?photoSources.processDaily()如何将字符串发布到背景页面,以及如何检索背景页面信息(例如,当用户单击托盘中的图标时?)为了防止有人发现这一点,上述操作对我来说很好,但我必须在清单文件中的权限中添加“警报”。这太完美了!我的情况下,MIN_IN_DAY会是1吗?onInstalled和onStartup会启动警报吗?你是怎么做到的?@Joebb在1号上回答是的onInstalled
和启动时的onStartup
将调用类似\u updateRepeatingAlarms
的函数。调用chrome.alarms.create
会自动启动报警。太棒了!一天中的毫秒是1000?photoSources.processDaily()如何将字符串发布到背景页面,以及如何检索背景页面信息(例如,当用户单击托盘中的图标时?)为了防止有人发现这一点,上述操作对我来说很好,但我必须在清单文件中的权限中添加“警报”。