From d57fe2b04dfb33b9d18362da80cb90d40247732f Mon Sep 17 00:00:00 2001 From: lingling <1077478963@qq.com> Date: Mon, 30 Sep 2024 09:37:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9viewPermission=20=20all?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/background.js | 59 ++++++++++++++++++++++++++++-------------------- js/popup.js | 41 ++++++++++++--------------------- lib/content.js | 21 +++++++++++------ 3 files changed, 63 insertions(+), 58 deletions(-) diff --git a/js/background.js b/js/background.js index 9bdee18..ef7c839 100644 --- a/js/background.js +++ b/js/background.js @@ -2,7 +2,7 @@ * @Author: lingling 1077478963@qq.com * @Date: 2024-08-19 09:47:04 * @LastEditors: lingling 1077478963@qq.com - * @LastEditTime: 2024-09-27 15:57:31 + * @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 */ @@ -12,46 +12,55 @@ let Ggoogle_mail = ""; let executionAuthority = false; //是否查询过 let haveYouChecked = false; + /** * 查看是否有权限运行 * @param {*} google_mail 谷歌id */ -//TODO 授权没写完 async function viewPermission(google_mail) { - 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; + 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; // 保证状态更新,即使出现错误 } - haveYouChecked = true; - - return false; } + /** * 监听消息 * @param {*} req 发送过来的消息 * @param {*} sendResponse 返回消息 */ chrome.runtime.onMessage.addListener(async (req, sender, sendResponse) => { - if (haveYouChecked) { - sendResponse(executionAuthority); - return true; - } - if (req.ty == "viewPermission") { - const res = req.info; - if (res&&!haveYouChecked) { - let data = await viewPermission(res); - console.log(data); - sendResponse(data); + 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 true; + // 其他消息类型的处理 + return false; // 关闭通道,表示没有需要处理的异步操作 }); +// 启动扩展时自动创建一个新标签页 setTimeout(function () { - chrome.tabs.create({ url: "popup.html" }, function () {}); + chrome.tabs.create({ url: "popup.html" }); }, 3500); diff --git a/js/popup.js b/js/popup.js index 332f0d1..38748f4 100644 --- a/js/popup.js +++ b/js/popup.js @@ -2,11 +2,16 @@ * @Author: lingling 1077478963@qq.com * @Date: 2024-08-19 09:47:04 * @LastEditors: lingling 1077478963@qq.com - * @LastEditTime: 2024-09-27 16:04:40 + * @LastEditTime: 2024-09-29 14:07:35 * @FilePath: \谷歌自动搜索邮箱自动点击v3\js\popup.js * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ $(function () { + /** + * 数组打乱随机执行 + * @param {*} array + * @returns + */ function shuffleArray(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); @@ -51,16 +56,20 @@ $(function () { sender, sendResponse ) { - console.log(request) // 判断是产生一个新页面是 if (request.ty == "get") { sendResponse({ name, id }); } if (request.ty == "remove") { - chrome.tabs.remove(id); + chrome.tabs.remove(sender.tab.id); } - return true; + if(request.ty == "get"||request.ty == "remove"){ + return true; + }else{ + return false; + } + }); async function google() { @@ -112,11 +121,7 @@ $(function () { // queue.push(youtube); // queue.push(play_google); // queue.push(youtube_shorts); - queue.push(news_google); - - //乱序执行 此处有问题 - // queue=shuffleArray(queue) - //当前执行的队列索引 + // queue.push(news_google); let closed_index = 0; function sendMsg(obj) { @@ -141,25 +146,9 @@ $(function () { chrome.tabs.query({}, function (tabs) { // 遍历每个标签页并关闭它们 for (var i = 0; i < tabs.length; i++) { - chrome.tabs.remove(tabs[i].id); + // chrome.tabs.remove(tabs[i].id); } }); } }); - - function sed_cont(id) { - let message = { - info: "来自popup的情书💌", - }; - chrome.tabs.sendMessage(id, message, (res) => { - // console.log("popup=>content"); - console.log(res); - }); - } - - chrome.tabs.onCreated.addListener((e) => { - // sed_cont(id); - console.log(e); - console.log("创建了一个新的浏览器窗口"); - }); }); diff --git a/lib/content.js b/lib/content.js index f0e9523..97d580e 100644 --- a/lib/content.js +++ b/lib/content.js @@ -38,6 +38,16 @@ function is_mobile_web(){ return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? true : false } +//naver使用函数 +const all = async (r, id) => { + for (let index = 0; index < 10; index++) { + await delay(1) + console.log(`共等待10秒当前${index+1}秒`) + } + send_close(id); +}; + + //naver使用函数 const naver = async (r, id) => { let googelid_str = (elements = $(".gb_A.gb_Za.gb_0").attr("aria-label")); @@ -45,9 +55,9 @@ const naver = async (r, id) => { const match = googelid_str.match(emailRegex); const email = match ? match[0] : null; console.log(`email:${email} r:${r} id${id}`) - // let res = await to_background(email); - // console.log(`res:${res} emall:${email}`); - // await delay(10) + let res = await to_background(email); + console.log(`res:${res} emall:${email}`); + await delay(10) // send_close(id); }; /** @@ -443,12 +453,9 @@ chrome.runtime.sendMessage( youtube(r, id); break; default: + all() break; } } ); -// chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { -// console.log(request); -// sendResponse("我收到了你的情书,popup~"); -// });