chrome_extensions/js/popup.js

155 lines
4.1 KiB
JavaScript

/*
* @Author: lingling 1077478963@qq.com
* @Date: 2024-08-19 09:47:04
* @LastEditors: lingling 1077478963@qq.com
* @LastEditTime: 2024-08-24 09:25:13
* @FilePath: \谷歌自动搜索邮箱自动点击v3\js\popup.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
$(function () {
// var id = 0, due = ['Dell', ' Lenovo ', "ASUS"], kme = 0 , name="";
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.query({}, function(tabs) {
// // 遍历每个标签页并关闭它们
// for (var i = 0; i < tabs.length; i++) {
// chrome.tabs.remove(tabs[i].id);
// }
// });
chrome.tabs.remove(request.id);
}
return true;
});
// async function text() {
// let a = await createTab("https://www.youtube.com/", "youtube");
// return a;
// }
async function youtube_shorts() {
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;
}
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;
}
async function calendar() {
let a = await createTab("https://calendar.google.com/calendar", "calendar");
return a;
}
async function shopping() {
let a = await createTab("https://shopping.google.com/", "shopping");
return a;
}
//函数队列
let queue = [];
// queue.push(play_finance);//好像js注入不进去
queue.push(shopping);
queue.push(calendar);
queue.push(mail);
queue.push(play_google);
queue.push(news_google);
queue.push(youtube_shorts);
//当前执行的队列索引
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 tmp = await queue[closed_index]();
// 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);
}
});
}
});
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("创建了一个新的浏览器窗口");
});
});