使用如下方式,执行工作js,但是工作js,读不到chrome存储的数据。
let worker = new Worker(‘js/worker.js’, {type: ‘module’});
所以通过消息的形式,来传输数据。
worker发送消息
function callbackLog(matchLog, successArray, errorMsg) {
self.postMessage({
action: 'callbackLog',matchLog, successArray, errorMsg
})
}
background.js接收消息
worker.onmessage = function (event) {
console.log('来自 Worker 的消息:', event.data);
var data = event.data;
switch (data.action) {
case "callbackLog":
console.log("callbackLog", data);
matchLogManager.callbackMatchLog(data.matchLog, data.successArray)
// sendMsgLogManager.callbackSendMsgLog(data.sendMsgLogId, data.errorMsg)
break;
default:
break;
}
};
在background.js中,定义manager 来控制数据。
class MatchLogManager {
constructor() {
}
// log = {
// id: 1,
// my_user_id: 1,
// reply_user_id: 2,
// nickname: "123",
// first: false,
// second: false,
// lastTime: 1
// }
// 没有日志则新增一个日志并返回
createLogAndThenReturn(my_user_id, reply_user_id, nickname) {
var curMatchLog = {
id: Date.now(),
my_user_id: my_user_id,
reply_user_id: reply_user_id,
nickname: nickname,
first: false,
second: false,
lastTime: Date.now()
}
console.log("new curMatchLog", curMatchLog)
// 获取当前的日志数据
chrome.storage.sync.get({matchLogs: []}, (data) => {
const matchLogs = data.matchLogs;
// 添加新的日志记录
matchLogs.push(curMatchLog);
// 保存更新后的日志数据
chrome.storage.sync.set({matchLogs: matchLogs}, () => {
console.log('matchLogs 日志记录已保存。');
});
console.log("configData", configData)
// 判断数据量是否超过上限
if (matchLogs.length > configData.leftMatchLogNumber) {
// 根据id进行倒序排序
matchLogs.sort((a, b) => b.lastTime - a.lastTime);
// 保留前上限个元素,删除其他的
let newMatchLogs = matchLogs.slice(0, configData.leftMatchLogNumber);
// 将更新后的数据重新存储到chrome.storage.sync
chrome.storage.sync.set({matchLogs: newMatchLogs}, () => {
console.log('Data trimmed and updated successfully.');
});
}
});
return curMatchLog;
}
checkUserThenReturnMatchLog(my_user_id, reply_user_id, nickname) {
console.log("start getLogById");
chrome.storage.sync.get({matchLogs: []}, (data) => {
const matchLogs = data.matchLogs;
var curMatchLog = matchLogs.find(log => log.my_user_id=== my_user_id&& log.reply_user_id=== reply_user_id);
console.log("checkUserThenSendMsg", curMatchLog)
console.log("checkUserThenSendMsg", matchLogs)
// 如果log为空,则新建一个,返回
if (!curMatchLog) {
curMatchLog = this.createLogAndThenReturn(my_user_id, reply_user_id, nickname)
console.log("new log", curMatchLog)
}
worker.postMessage({
action: "sendMsg",
data: curMatchLog
});
})
}
callbackMatchLog(matchLog, successArray) {
console.log("callbackMatchLog", matchLog, successArray);
// 得到log,并设置值
// 去日志记录得到当前用户、当前接收人的消息坑位
// 发送消息到background进行处理
var first = false;
var second = false;
if (successArray) {
successArray.forEach(item => {
if (item.sendType === 1) {
first = true;
} else if (item.sendType === 2) {
second = true;
}
})
}
chrome.storage.sync.get({matchLogs: []}, (data) => {
const matchLogs = data.matchLogs;
const curMatchLog = matchLogs.find(log => log.id === matchLog.id);
if (curMatchLog) {
curMatchLog.first = (curMatchLog.first | first);
curMatchLog.second = (curMatchLog.second | second);
curMatchLog.lastTime = Date.now();
}
});
}
}