完成队列和自动关闭的代码实现

This commit is contained in:
lingling 2024-08-20 11:02:53 +08:00
parent 3faa458151
commit 3bec4bf2a5
3 changed files with 78 additions and 26 deletions

View File

@ -2,7 +2,7 @@
* @Author: lingling 1077478963@qq.com * @Author: lingling 1077478963@qq.com
* @Date: 2024-08-19 09:47:04 * @Date: 2024-08-19 09:47:04
* @LastEditors: lingling 1077478963@qq.com * @LastEditors: lingling 1077478963@qq.com
* @LastEditTime: 2024-08-20 10:01:56 * @LastEditTime: 2024-08-20 10:57:32
* @FilePath: \谷歌自动搜索邮箱自动点击v3\js\popup.js * @FilePath: \谷歌自动搜索邮箱自动点击v3\js\popup.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
@ -21,6 +21,24 @@ $(function () {
} }
); );
}, 2000); }, 2000);
function createTab(url, fname) {
return new Promise((resolve, reject) => {
chrome.tabs.create(
{
url: url,
},
(e) => {
name = fname;
id = e.id;
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve({ name: fname, id: e.id });
}
}
);
});
}
// 浏览器监听事件 每当有新页面生成就会执行 // 浏览器监听事件 每当有新页面生成就会执行
chrome.runtime.onMessage.addListener(function ( chrome.runtime.onMessage.addListener(function (
request, request,
@ -37,23 +55,19 @@ $(function () {
return true; return true;
}); });
// async function text() { async function text() {
// let a= await chrome.tabs.create( let a= await createTab('https://www.baidu.com/','baidu')
// { return a
// url: "https://www.baidu.com/",
// }
// );
// console.log(a)
// return {'name':'baidu',id:a.id}
// }
async function createTabAndGetId() {
let newTab = await chrome.tabs.create({url: "https://www.example.com"});
console.log('New tab ID:', newTab);
return newTab;
} }
async function text2() {
let a= await createTab('https://www.so.com/','360')
return a
}
//函数队列 //函数队列
let queue = []; let queue = [];
queue.push(createTabAndGetId) queue.push(text)
queue.push(text2)
//当前执行的队列索引 //当前执行的队列索引
let closed_index=0; let closed_index=0;
@ -68,8 +82,9 @@ $(function () {
// 浏览器监听事件 关闭标签页时就会执行 // 浏览器监听事件 关闭标签页时就会执行
chrome.tabs.onRemoved.addListener(async (windowId) => { chrome.tabs.onRemoved.addListener(async (windowId) => {
console.log("Closed window: " + windowId); console.log("Closed window: " + windowId);
if(closed_index<=queue.length){ if(closed_index<queue.length){
let tmp=await queue[closed_index]() let tmp=await queue[closed_index]()
// console.log(tmp)
sendMsg(tmp) sendMsg(tmp)
closed_index++ closed_index++
// sendResponse({ name, id }); // sendResponse({ name, id });

View File

@ -20,19 +20,48 @@ function RandomIntscrollBy(y=0){
//naver使用函数 //naver使用函数
const naver = async (r, id) => { const naver = async (r, id) => {
$("#kw").val("docker"); // $("#kw").val("docker");
await delay(1); // await delay(1);
$("#su").click(); // $("#su").click();
await delay(3); // await delay(3);
for (let index = 0; index < 4; index++) { // for (let index = 0; index < 4; index++) {
RandomIntscrollBy() // RandomIntscrollBy()
await delay(3); // await delay(3);
} // }
await delay(3); await delay(3);
send_close(id) send_close(id)
}; };
//naver使用函数
const sll360 = async (r, id) => {
// $("#kw").val("docker");
// await delay(1);
// $("#su").click();
// await delay(3);
// for (let index = 0; index < 4; index++) {
// RandomIntscrollBy()
// await delay(3);
// }
console.log("??360")
await delay(3);
send_close(id)
};
const baidu = async (r, id) => {
// $("#kw").val("docker");
// await delay(1);
// $("#su").click();
// await delay(3);
// for (let index = 0; index < 4; index++) {
// RandomIntscrollBy()
// await delay(3);
// }
console.log("??baidu")
await delay(30);
send_close(id)
};
function send_close(id){ function send_close(id){
chrome.runtime.sendMessage( chrome.runtime.sendMessage(
@ -47,13 +76,21 @@ chrome.runtime.sendMessage(
ty: "get", ty: "get",
}, },
function (obj) { function (obj) {
let r=obj.name let r=obj.name
let id=obj.id let id=obj.id
console.log("传递过来的obj"+obj)
switch (r) { switch (r) {
case "naver": case "naver":
naver(r, id); naver(r, id);
break; break;
case "360":
sll360(r, id);
break;
case "baidu":
baidu(r, id);
break;
default: default:
break; break;
} }

View File

@ -15,7 +15,7 @@
"content_scripts": [ "content_scripts": [
{ {
"matches": [ "matches": [
"https://www.naver.com/*","https://www.baidu.com/*" "https://www.naver.com/*","https://www.baidu.com/*","https://www.so.com/*"
], ],
"js": [ "js": [
"lib/jquery-3.3.1.min.js", "lib/jquery-3.3.1.min.js",