使用 chrome 存储 API 从本地存储存储和检索数据
Store and retrieve data from local storage using chrome storage API
我正在构建一个扩展程序,该扩展程序向用户显示一个以秒为单位的值,表示用户在特定网站上花费的时间。我一切正常,但每次 chrome 退出或计算机重新启动时,时间变量都会从 0 开始重新计数。我认为使用 chrome 存储 API 应该可以完成这项工作。在阅读 API 文档后,我设法从本地存储中存储和检索了一个数字。我不能做的是当用户退出时如何将数据保存到本地存储chrome。有没有办法检测到此类事件?
首先,您不需要使用 chrome.storage
API 来完成这项工作。顺便说一句,不幸的是,你要找的东西不存在。您正在寻找未在 Chrome API 中实现的事件(如 onBrowserClosed
)。已提交错误报告 HERE(虽然它实际上不是错误),如果您想保持更新,可以加注星标。
尽管如此,您仍然可以使用 setInterval()
来解决问题,这将执行您的功能以每隔特定时间间隔(以毫秒为单位)更新用户在网站上花费的时间,并在浏览器已关闭。像这样:
var currentActiveTab, chromeHasFocus = false;
localStorage.timeSpentOnSites = localStorage.timeSpentOnSites || "{}";
// get the first tab at startup
chrome.tabs.query({active: true, highlighted: true}, function(tabs) {
currentActiveTab = tabs[0];
console.log('New active tab:', tabs[0]);
});
// this will keep currentActiveTab updated to always be the active tab (the one that the user is watching)
chrome.tabs.onUpdated.addListener(function(tabID, info, tab) {
if (tab.active && tab.highlighted) currentActiveTab = tab;
console.log('New active tab:', tab);
});
// this also
chrome.tabs.onActivated.addListener(function(info) {
chrome.tabs.query({active: true, highlighted: true}, function(tabs) {
currentActiveTab = tabs[0];
console.log('New active tab:', tabs[0]);
});
});
// this will check if chrome is active or not
chrome.windows.onFocusChanged.addListener(function(windowID) {
if (windowID === chrome.windows.WINDOW_ID_NONE) {
chromeHasFocus = false;
console.log('Chrome lost focus.');
} else if (!chromeHasFocus) {
chromeHasFocus = true;
console.log('Chrome has focus.');
}
});
function addTimeSpentOnSite(site) {
var T = JSON.parse(localStorage.timeSpentOnSites);
// if site already exists increment the time spent on it
if (T[site]) T[site]++;
// otherwise set the time spent on it as 1 (second)
else T[site] = 1;
localStorage.timeSpentOnSites = JSON.stringify(T);
}
setInterval(function() {
if (!chromeHasFocus) return;
// if the chrome window isn't active the user is not watching the site
var site = currentActiveTab.url.split('/')[2];
// get the site name, something like www.site.com
addTimeSpentOnSite(site);
// increase the amount of time spent on the site
}, 1000);
我正在构建一个扩展程序,该扩展程序向用户显示一个以秒为单位的值,表示用户在特定网站上花费的时间。我一切正常,但每次 chrome 退出或计算机重新启动时,时间变量都会从 0 开始重新计数。我认为使用 chrome 存储 API 应该可以完成这项工作。在阅读 API 文档后,我设法从本地存储中存储和检索了一个数字。我不能做的是当用户退出时如何将数据保存到本地存储chrome。有没有办法检测到此类事件?
首先,您不需要使用 chrome.storage
API 来完成这项工作。顺便说一句,不幸的是,你要找的东西不存在。您正在寻找未在 Chrome API 中实现的事件(如 onBrowserClosed
)。已提交错误报告 HERE(虽然它实际上不是错误),如果您想保持更新,可以加注星标。
尽管如此,您仍然可以使用 setInterval()
来解决问题,这将执行您的功能以每隔特定时间间隔(以毫秒为单位)更新用户在网站上花费的时间,并在浏览器已关闭。像这样:
var currentActiveTab, chromeHasFocus = false;
localStorage.timeSpentOnSites = localStorage.timeSpentOnSites || "{}";
// get the first tab at startup
chrome.tabs.query({active: true, highlighted: true}, function(tabs) {
currentActiveTab = tabs[0];
console.log('New active tab:', tabs[0]);
});
// this will keep currentActiveTab updated to always be the active tab (the one that the user is watching)
chrome.tabs.onUpdated.addListener(function(tabID, info, tab) {
if (tab.active && tab.highlighted) currentActiveTab = tab;
console.log('New active tab:', tab);
});
// this also
chrome.tabs.onActivated.addListener(function(info) {
chrome.tabs.query({active: true, highlighted: true}, function(tabs) {
currentActiveTab = tabs[0];
console.log('New active tab:', tabs[0]);
});
});
// this will check if chrome is active or not
chrome.windows.onFocusChanged.addListener(function(windowID) {
if (windowID === chrome.windows.WINDOW_ID_NONE) {
chromeHasFocus = false;
console.log('Chrome lost focus.');
} else if (!chromeHasFocus) {
chromeHasFocus = true;
console.log('Chrome has focus.');
}
});
function addTimeSpentOnSite(site) {
var T = JSON.parse(localStorage.timeSpentOnSites);
// if site already exists increment the time spent on it
if (T[site]) T[site]++;
// otherwise set the time spent on it as 1 (second)
else T[site] = 1;
localStorage.timeSpentOnSites = JSON.stringify(T);
}
setInterval(function() {
if (!chromeHasFocus) return;
// if the chrome window isn't active the user is not watching the site
var site = currentActiveTab.url.split('/')[2];
// get the site name, something like www.site.com
addTimeSpentOnSite(site);
// increase the amount of time spent on the site
}, 1000);