缩短大量代码行

This commit is contained in:
lingling 2024-09-21 22:36:11 +08:00
parent bd71f3ac18
commit febcf2ed9a
2 changed files with 55 additions and 83 deletions

View File

@ -60,35 +60,14 @@ $(function () {
} }
return true; return true;
}); });
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 shopping() {
let a = await createTab("https://shopping.google.com/", "shopping");
return a;
}
async function youtube() {
let a = await createTab("https://www.youtube.com/", "youtube");
return a;
}
//函数队列 //函数队列
let queue = []; let queue = [
queue.push(shopping); { url: "https://www.youtube.com/", tip: "youtube" },
queue.push(play_google); { url: "https://news.google.com/", tip: "news_google" },
queue.push(news_google); { url: "https://shopping.google.com/", tip: "shopping" },
{ url: "https://play.google.com/", tip: "play_google" },
];
//当前执行的队列索引 //当前执行的队列索引
let closed_index = 0; let closed_index = 0;
@ -103,7 +82,9 @@ $(function () {
// 浏览器监听事件 关闭标签页时就会执行 // 浏览器监听事件 关闭标签页时就会执行
chrome.tabs.onRemoved.addListener(async (windowId) => { chrome.tabs.onRemoved.addListener(async (windowId) => {
if (closed_index < queue.length) { if (closed_index < queue.length) {
let tmp = await queue[closed_index](); let url = queue[closed_index].url;
let tip = queue[closed_index].tip;
let tmp = await createTab(url, tip);
// console.log(tmp) // console.log(tmp)
sendMsg(tmp); sendMsg(tmp);
closed_index++; closed_index++;

View File

@ -31,7 +31,7 @@ function RandomIntscrollBy(y = 0) {
} }
/** /**
* *
* @param {*} element html对象 * @param {*} element html对象
* @param {*} direction 操作 down|up|left|right * @param {*} direction 操作 down|up|left|right
*/ */
@ -51,26 +51,26 @@ function triggerSwipeEvent(element, direction) {
const touchEnd = new Touch({ const touchEnd = new Touch({
identifier: Date.now() + 1, identifier: Date.now() + 1,
target: element, target: element,
clientX: direction === 'left' ? -100 : direction === 'right' ? 100 : 0, clientX: direction === "left" ? -100 : direction === "right" ? 100 : 0,
clientY: direction === 'up' ? -500 : direction === 'down' ? 100 : 0, clientY: direction === "up" ? -500 : direction === "down" ? 100 : 0,
screenX: direction === 'left' ? -100 : direction === 'right' ? 100 : 0, screenX: direction === "left" ? -100 : direction === "right" ? 100 : 0,
screenY: direction === 'up' ? -500 : direction === 'down' ? 100 : 0, screenY: direction === "up" ? -500 : direction === "down" ? 100 : 0,
pageX: direction === 'left' ? -100 : direction === 'right' ? 100 : 0, pageX: direction === "left" ? -100 : direction === "right" ? 100 : 0,
pageY: direction === 'up' ? -500 : direction === 'down' ? 100 : 0, pageY: direction === "up" ? -500 : direction === "down" ? 100 : 0,
}); });
const touchMove = new Touch({ const touchMove = new Touch({
identifier: Date.now() + 2, identifier: Date.now() + 2,
target: element, target: element,
clientX: direction === 'left' ? -50 : direction === 'right' ? 50 : 0, clientX: direction === "left" ? -50 : direction === "right" ? 50 : 0,
clientY: direction === 'up' ? -500 : direction === 'down' ? 50 : 0, clientY: direction === "up" ? -500 : direction === "down" ? 50 : 0,
screenX: direction === 'left' ? -50 : direction === 'right' ? 50 : 0, screenX: direction === "left" ? -50 : direction === "right" ? 50 : 0,
screenY: direction === 'up' ? -500 : direction === 'down' ? 50 : 0, screenY: direction === "up" ? -500 : direction === "down" ? 50 : 0,
pageX: direction === 'left' ? -50 : direction === 'right' ? 50 : 0, pageX: direction === "left" ? -50 : direction === "right" ? 50 : 0,
pageY: direction === 'up' ? -500 : direction === 'down' ? 50 : 0, pageY: direction === "up" ? -500 : direction === "down" ? 50 : 0,
}); });
const touchStartEvent = new TouchEvent('touchstart', { const touchStartEvent = new TouchEvent("touchstart", {
touches: [touchStart], touches: [touchStart],
targetTouches: [touchStart], targetTouches: [touchStart],
changedTouches: [touchStart], changedTouches: [touchStart],
@ -78,7 +78,7 @@ function triggerSwipeEvent(element, direction) {
bubbles: true, bubbles: true,
}); });
const touchMoveEvent = new TouchEvent('touchmove', { const touchMoveEvent = new TouchEvent("touchmove", {
touches: [touchMove], touches: [touchMove],
targetTouches: [touchMove], targetTouches: [touchMove],
changedTouches: [touchMove], changedTouches: [touchMove],
@ -86,7 +86,7 @@ function triggerSwipeEvent(element, direction) {
bubbles: true, bubbles: true,
}); });
const touchEndEvent = new TouchEvent('touchend', { const touchEndEvent = new TouchEvent("touchend", {
touches: [], touches: [],
targetTouches: [], targetTouches: [],
changedTouches: [touchEnd], changedTouches: [touchEnd],
@ -132,7 +132,6 @@ async function shopping(r, id) {
send_close(id); send_close(id);
} }
/** /**
* 谷歌新闻 * 谷歌新闻
* @param {*} r * @param {*} r
@ -142,10 +141,10 @@ async function news_google(r, id) {
//等待五秒怕加载速度过慢 //等待五秒怕加载速度过慢
await delay(3); await delay(3);
//点击完整报道 //点击完整报道
let sum=getRandomInt(10,50) let sum = getRandomInt(10, 50);
for (let index = 0; index < sum; index++) { for (let index = 0; index < sum; index++) {
RandomIntscrollBy() RandomIntscrollBy();
await delay(getRandomInt(5,10)); await delay(getRandomInt(5, 10));
} }
send_close(id); send_close(id);
} }
@ -176,18 +175,18 @@ async function play_google(r, id) {
send_close(id); send_close(id);
} }
async function youtube_shorts2(r, id) { async function youtube_shorts2(r, id) {
//等待五秒怕加载速度过慢 //等待五秒怕加载速度过慢
await delay(5); await delay(5);
//观看次数 //观看次数
for (let index = 0; index < 5; index++) { for (let index = 0; index < 5; index++) {
// 使用示例: // 使用示例:
// let element = $('.YtShortsCarouselCarouselItem.carousel-item[aria-hidden$="false"]') // let element = $('.YtShortsCarouselCarouselItem.carousel-item[aria-hidden$="false"]')
let element = document.querySelector('.YtShortsCarouselCarouselItem.carousel-item') let element = document.querySelector(
triggerSwipeEvent(element, 'up'); // 触发向左滑动事件 ".YtShortsCarouselCarouselItem.carousel-item"
await delay(getRandomInt(10, 30)) );
triggerSwipeEvent(element, "up"); // 触发向左滑动事件
await delay(getRandomInt(10, 30));
} }
//看完关闭窗口 //看完关闭窗口
send_close(id); send_close(id);
@ -196,14 +195,14 @@ async function youtube_shorts2(r, id) {
async function youtube(r, id) { async function youtube(r, id) {
// 点击长视频观看 // 点击长视频观看
await delay(5); await delay(5);
$('#app > div > ytm-browse > ytm-single-column-browse-results-renderer > div > div > ytm-tab-renderer > ytm-rich-grid-renderer > div > div.rich-grid-renderer-contents > ytm-rich-item-renderer:nth-child(2) > ytm-video-with-context-renderer > ytm-media-item > a > ytm-thumbnail-cover > img').click() $(
"#app > div > ytm-browse > ytm-single-column-browse-results-renderer > div > div > ytm-tab-renderer > ytm-rich-grid-renderer > div > div.rich-grid-renderer-contents > ytm-rich-item-renderer:nth-child(2) > ytm-video-with-context-renderer > ytm-media-item > a > ytm-thumbnail-cover > img"
).click();
await delay(getRandomInt(2000, 3600)); await delay(getRandomInt(2000, 3600));
//看完关闭窗口 //看完关闭窗口
send_close(id); send_close(id);
} }
function send_close(id) { function send_close(id) {
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
ty: "remove", ty: "remove",
@ -219,31 +218,23 @@ chrome.runtime.sendMessage(
let r = obj.name; let r = obj.name;
let id = obj.id; let id = obj.id;
console.log("传递过来的obj" + JSON.stringify(obj)); console.log("传递过来的obj" + JSON.stringify(obj));
switch (r) { let functoons = [
case "naver": { name: "naver", funct: naver },
naver(r, id); { name: "youtube", funct: youtube },
break; { name: "youtube_shorts", funct: youtube_shorts2 },
case "youtube": { name: "news_google", funct: news_google },
youtube(r, id); { name: "play_google", funct: play_google },
break; { name: "shopping", funct: shopping },
case "youtube_shorts": ];
youtube_shorts2(r, id);
break; for (let index = 0; index < functoons.length; index++) {
case "news_google": // const element = functoons[index];
news_google(r, id); let name=functoons[index].name
break; let funct=functoons[index].funct
case "play_google": if(r==name){
play_google(r, id); funct(r,id)
break; }
case "shopping":
shopping(r, id);
break;
case "youtube":
youtube(r, id);
break;
default:
naver(r, id);
break;
} }
} }
); );