我正在嘗試創建一個 Chrome 擴展程式來跟蹤用戶使用網站的時間(即活動跟蹤器)我的方法是獲取選項卡處于活動狀態的時間并獲取不活動的時間然后減去,但我可以' t 獲取非活動選項卡
//// background.js
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
這是我用來獲取當前標簽資訊的代碼
這是我的第一個擴展所以...
uj5u.com熱心網友回復:
使用api chrome.tabs.onActivated
const url = ''
const time = 0
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const { tabId } = activeInfo;
const newUrl = tabId
const currentTime = new Date().getTime()
if (url && url !== newUrl) {
const elapsedTime = currentTime - time;
time = new Date().getTime()
url = newUrl
// 1. if url is null, start timer
} else {
time = currentTime
url = newUrl
}
});
如果你想獲取url或href使用這個 apichrome.tabs.get(tabId, (res) => {} )
uj5u.com熱心網友回復:
我相信您可以使用地圖將所有訪問過的 Url 存盤為鍵,并將它們激活的時間存盤為值。如果這不是您要找的東西,那么很抱歉,但我確定您可以使用它?告訴我是否有問題:
let Opened = new Map();
let lastChanged, previousUrl;
chrome.tabs.onUpdated.addListener(function (tabid, changeInfo, tab) {
var currentUrl = changeInfo.url;
if (!currentUrl) return;
if (currentUrl !== previousUrl) {
if (lastChanged) {
const elapsedTime = (new Date().getTime()) - lastChanged;
if (!Opened.has(previousUrl)) {
Opened.set(previousUrl, elapsedTime); // Set time to the elapsed time if its a new URL
} else {
Opened.set(previousUrl, Opened.get(currentUrl) elapsedTime); // Increment the time for this URL
}
}
lastChanged = new Date().getTime();
previousUrl = currentUrl;
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394464.html
