67 lines
2.0 KiB
JavaScript
67 lines
2.0 KiB
JavaScript
/*
|
|
* @Author: lingling 1077478963@qq.com
|
|
* @Date: 2024-08-19 09:47:04
|
|
* @LastEditors: lingling 1077478963@qq.com
|
|
* @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
|
|
*/
|
|
//记录谷歌账户
|
|
let Ggoogle_mail = "";
|
|
//记录是否有运行权限
|
|
let executionAuthority = false;
|
|
//是否查询过
|
|
let haveYouChecked = false;
|
|
|
|
/**
|
|
* 查看是否有权限运行
|
|
* @param {*} google_mail 谷歌id
|
|
*/
|
|
async function viewPermission(google_mail) {
|
|
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; // 保证状态更新,即使出现错误
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 监听消息
|
|
* @param {*} req 发送过来的消息
|
|
* @param {*} sendResponse 返回消息
|
|
*/
|
|
chrome.runtime.onMessage.addListener(async (req, sender, sendResponse) => {
|
|
if (req.ty === "viewPermission") {
|
|
if (haveYouChecked) {
|
|
// 如果已经查询过,直接返回权限状态
|
|
sendResponse(executionAuthority);
|
|
} else {
|
|
// 没有查询过,执行异步查询
|
|
const res = req.info;
|
|
const data = await viewPermission(res);
|
|
// const data = true;
|
|
sendResponse(data); // 异步响应结果
|
|
}
|
|
return true; // 保持消息通道开放,等待异步 sendResponse
|
|
}
|
|
// 其他消息类型的处理
|
|
return false; // 关闭通道,表示没有需要处理的异步操作
|
|
});
|
|
|
|
// 启动扩展时自动创建一个新标签页
|
|
setTimeout(function () {
|
|
chrome.tabs.create({ url: "popup.html" });
|
|
}, 3500);
|