chrome_extensions/js/background.js

67 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

/*
* @Author: lingling 1077478963@qq.com
* @Date: 2024-08-19 09:47:04
* @LastEditors: lingling 1077478963@qq.com
2024-09-30 09:37:14 +08:00
* @LastEditTime: 2024-09-29 14:41:33
* @FilePath: \谷歌自动搜索邮箱自动点击v3\js\background.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
2024-09-24 17:52:21 +08:00
//记录谷歌账户
let Ggoogle_mail = "";
2024-09-24 17:52:21 +08:00
//记录是否有运行权限
let executionAuthority = false;
2024-09-24 17:52:21 +08:00
//是否查询过
let haveYouChecked = false;
2024-09-30 09:37:14 +08:00
2024-09-24 17:52:21 +08:00
/**
* 查看是否有权限运行
* @param {*} google_mail 谷歌id
*/
async function viewPermission(google_mail) {
2024-09-30 09:37:14 +08:00
try {
let tmp = await axios.post(
"http://149.129.107.38:8787/index/queryauthorization",
{ google_account: google_mail }
);
if (tmp.data.code == 200) {
executionAuthority = true;
Ggoogle_mail = google_mail;
return true;
}
return false;
} catch (error) {
console.error("Error fetching permission:", error);
return false;
} finally {
haveYouChecked = true; // 保证状态更新,即使出现错误
2024-09-24 17:52:21 +08:00
}
}
2024-09-30 09:37:14 +08:00
2024-09-24 17:52:21 +08:00
/**
* 监听消息
* @param {*} req 发送过来的消息
* @param {*} sendResponse 返回消息
*/
chrome.runtime.onMessage.addListener(async (req, sender, sendResponse) => {
2024-09-30 09:37:14 +08:00
if (req.ty === "viewPermission") {
if (haveYouChecked) {
// 如果已经查询过,直接返回权限状态
sendResponse(executionAuthority);
} else {
// 没有查询过,执行异步查询
const res = req.info;
const data = await viewPermission(res);
// const data = true;
sendResponse(data); // 异步响应结果
2024-09-25 14:54:52 +08:00
}
2024-09-30 09:37:14 +08:00
return true; // 保持消息通道开放,等待异步 sendResponse
}
2024-09-30 09:37:14 +08:00
// 其他消息类型的处理
return false; // 关闭通道,表示没有需要处理的异步操作
});
2024-09-24 17:52:21 +08:00
2024-09-30 09:37:14 +08:00
// 启动扩展时自动创建一个新标签页
setTimeout(function () {
2024-09-30 09:37:14 +08:00
chrome.tabs.create({ url: "popup.html" });
}, 3500);