chrome_extensions/js/popup.js

166 lines
4.3 KiB
JavaScript
Raw Normal View History

2024-08-19 17:05:34 +08:00
/*
* @Author: lingling 1077478963@qq.com
* @Date: 2024-08-19 09:47:04
* @LastEditors: lingling 1077478963@qq.com
2024-09-25 14:54:52 +08:00
* @LastEditTime: 2024-09-25 10:55:38
2024-08-19 17:05:34 +08:00
* @FilePath: \谷歌自动搜索邮箱自动点击v3\js\popup.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
2024-08-19 09:59:57 +08:00
$(function () {
2024-09-24 17:52:21 +08:00
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;
}
2024-08-20 10:05:28 +08:00
var id = 0;
var name = "";
setTimeout(function () {
chrome.tabs.create(
{
2024-08-21 13:56:31 +08:00
url: "https://www.google.com",
2024-08-20 10:05:28 +08:00
},
(e) => {
name = "naver";
id = e.id;
}
);
2024-08-21 11:30:52 +08:00
}, 1000);
function createTab(url, fname) {
2024-08-21 11:30:52 +08:00
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 });
}
}
);
});
}
2024-08-20 10:05:28 +08:00
// 浏览器监听事件 每当有新页面生成就会执行
chrome.runtime.onMessage.addListener(function (
request,
sender,
sendResponse
) {
2024-09-25 14:54:52 +08:00
console.log(request)
2024-08-20 10:05:28 +08:00
// 判断是产生一个新页面是
if (request.ty == "get") {
sendResponse({ name, id });
}
if (request.ty == "remove") {
2024-08-20 10:05:28 +08:00
chrome.tabs.remove(request.id);
}
return true;
});
2024-08-19 09:59:57 +08:00
2024-09-25 14:54:52 +08:00
async function google() {
let a = await createTab("https://www.google.com", "naver");
return a;
}
async function youtube_shorts() {
2024-08-21 11:30:52 +08:00
let a = await createTab(
"https://www.youtube.com/shorts/",
"youtube_shorts"
);
return a;
}
async function news_google() {
let a = await createTab("https://news.google.com/", "news_google");
return a;
2024-08-20 10:05:28 +08:00
}
2024-08-21 17:35:28 +08:00
async function play_google() {
let a = await createTab("https://play.google.com/", "play_google");
return a;
}
async function play_finance() {
let a = await createTab("https://www.google.com/finance/", "play_finance");
return a;
}
async function mail() {
let a = await createTab("https://mail.google.com/mail/", "mail");
return a;
}
2024-08-22 14:06:42 +08:00
async function calendar() {
let a = await createTab("https://calendar.google.com/calendar", "calendar");
return a;
}
2024-08-22 16:25:33 +08:00
async function shopping() {
let a = await createTab("https://shopping.google.com/", "shopping");
return a;
}
2024-08-26 14:34:55 +08:00
async function youtube() {
let a = await createTab("https://www.youtube.com/", "youtube");
return a;
}
2024-08-20 10:05:28 +08:00
//函数队列
let queue = [];
2024-09-25 14:54:52 +08:00
queue.push(google);
2024-08-24 11:17:58 +08:00
// queue.push(play_finance);//好像js注入不进去
2024-08-26 14:34:55 +08:00
queue.push(shopping);
queue.push(calendar);
queue.push(mail);
2024-08-26 14:34:55 +08:00
// queue.push(youtube);
queue.push(play_google);
queue.push(youtube_shorts);
2024-08-26 14:50:32 +08:00
queue.push(news_google);
2024-09-24 17:52:21 +08:00
//乱序执行 此处有问题
// queue=shuffleArray(queue)
2024-08-20 10:05:28 +08:00
//当前执行的队列索引
2024-08-21 11:30:52 +08:00
let closed_index = 0;
2024-08-20 10:05:28 +08:00
2024-08-21 11:30:52 +08:00
function sendMsg(obj) {
chrome.runtime.sendMessage(obj, (res) => {
// 答复
console.log("popup=>content");
console.log(res);
});
2024-08-20 10:05:28 +08:00
}
// 浏览器监听事件 关闭标签页时就会执行
chrome.tabs.onRemoved.addListener(async (windowId) => {
2024-08-21 11:30:52 +08:00
console.log("Closed window: " + windowId);
if (closed_index < queue.length) {
let tmp = await queue[closed_index]();
// console.log(tmp)
sendMsg(tmp);
closed_index++;
// sendResponse({ name, id });
2024-08-21 17:35:28 +08:00
} else {
//运行完成关闭所有窗口
chrome.tabs.query({}, function (tabs) {
// 遍历每个标签页并关闭它们
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.remove(tabs[i].id);
}
});
2024-08-21 11:30:52 +08:00
}
2024-08-20 10:05:28 +08:00
});
2024-08-21 17:35:28 +08:00
function sed_cont(id) {
let message = {
info: "来自popup的情书💌",
};
chrome.tabs.sendMessage(id, message, (res) => {
// console.log("popup=>content");
console.log(res);
});
}
2024-08-21 11:30:52 +08:00
chrome.tabs.onCreated.addListener((e) => {
2024-09-25 14:54:52 +08:00
// sed_cont(id);
2024-08-21 11:30:52 +08:00
console.log(e);
2024-08-21 17:35:28 +08:00
console.log("创建了一个新的浏览器窗口");
2024-08-21 11:30:52 +08:00
});
2024-08-19 17:05:34 +08:00
});