chrome_extensions/js/popup.js

119 lines
3.3 KiB
JavaScript

/*
* @Author: lingling 1077478963@qq.com
* @Date: 2024-08-19 09:47:04
* @LastEditors: lingling 1077478963@qq.com
* @LastEditTime: 2024-09-30 09:40:14
* @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));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
var id = 0;
var name = "";
setTimeout(function () {
chrome.tabs.create(
{
url: "https://www.google.com",
},
(e) => {
name = "naver";
id = e.id;
}
);
}, 1000);
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 (
request,
sender,
sendResponse
) {
// 判断是产生一个新页面是
if (request.ty == "get") {
sendResponse({ name, id });
}
if (request.ty == "remove") {
chrome.tabs.remove(sender.tab.id);
}
if (request.ty == "get" || request.ty == "remove") {
return true;
} else {
return false;
}
});
//乱序执行 此处有问题
// queue=shuffleArray(queue)
//当前执行的队列索引
//函数队列
let queue = [
{ url: "https://www.google.com", tip: "naver" },
// { url: "https://www.youtube.com/shorts/", tip: "youtube_shorts" },
// { url: "https://www.youtube.com/watch?v=LZ7DMB6h-hs", tip: "youtube" },
// { url: "https://play.google.com/", tip: "play_google" },
// { url: "https://shopping.google.com/", tip: "shopping" },
// { url: "https://calendar.google.com/calendar", tip: "calendar" },
// { url: "https://mail.google.com/mail/", tip: "mail" },
// { url: "https://www.google.com/finance/", tip: "play_finance" },
];
let closed_index = 0;
function sendMsg(obj) {
chrome.runtime.sendMessage(obj, (res) => {
// 答复
console.log("popup=>content");
console.log(res);
});
}
// 浏览器监听事件 关闭标签页时就会执行
chrome.tabs.onRemoved.addListener(async (windowId) => {
console.log("Closed window: " + windowId);
if (closed_index < queue.length) {
let url = queue[closed_index].url;
let tip = queue[closed_index].tip;
let tmp = await createTab(url, tip);
// console.log(tmp)
sendMsg(tmp);
closed_index++;
// sendResponse({ name, id });
} else {
//运行完成关闭所有窗口
chrome.tabs.query({}, function (tabs) {
// 遍历每个标签页并关闭它们
for (var i = 0; i < tabs.length; i++) {
// chrome.tabs.remove(tabs[i].id);
}
});
}
});
});