chrome_extensions_phone/lib/content.js

245 lines
6.3 KiB
JavaScript
Raw Normal View History

2024-08-24 11:17:58 +08:00
/**
* 阻塞代码运行
* @param {*} n 秒数
2024-08-26 14:34:55 +08:00
* @returns
2024-08-24 11:17:58 +08:00
*/
2024-08-19 17:05:34 +08:00
function delay(n) {
return new Promise(function (resolve) {
setTimeout(resolve, n * 1000);
});
}
2024-08-24 11:17:58 +08:00
/**
* 随机数生成
* @param {*} min 最小值
* @param {*} max 最大值
2024-08-26 14:34:55 +08:00
* @returns
2024-08-24 11:17:58 +08:00
*/
2024-08-19 18:02:42 +08:00
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
2024-08-19 18:02:42 +08:00
2024-08-24 11:17:58 +08:00
/**
* 随机滑动函数
* @param {*} y 滑动像素 不传递默认随机
*/
function RandomIntscrollBy(y = 0) {
let rand = getRandomInt(100, 600);
scrollBy(0, y == 0 ? rand : y);
console.log("随机滑动" + rand);
2024-08-19 18:02:42 +08:00
}
2024-08-19 17:05:34 +08:00
2024-09-21 21:58:25 +08:00
/**
2024-09-21 22:36:11 +08:00
*
2024-09-21 21:58:25 +08:00
* @param {*} element html对象
* @param {*} direction 操作 down|up|left|right
*/
function triggerSwipeEvent(element, direction) {
const touchStart = new Touch({
identifier: Date.now(),
target: element,
clientX: 0,
clientY: 0,
screenX: 0,
screenY: 0,
pageX: 0,
pageY: 0,
});
const touchEnd = new Touch({
identifier: Date.now() + 1,
target: element,
2024-09-21 22:36:11 +08:00
clientX: direction === "left" ? -100 : direction === "right" ? 100 : 0,
clientY: direction === "up" ? -500 : direction === "down" ? 100 : 0,
screenX: direction === "left" ? -100 : direction === "right" ? 100 : 0,
screenY: direction === "up" ? -500 : direction === "down" ? 100 : 0,
pageX: direction === "left" ? -100 : direction === "right" ? 100 : 0,
pageY: direction === "up" ? -500 : direction === "down" ? 100 : 0,
2024-09-21 21:58:25 +08:00
});
const touchMove = new Touch({
identifier: Date.now() + 2,
target: element,
2024-09-21 22:36:11 +08:00
clientX: direction === "left" ? -50 : direction === "right" ? 50 : 0,
clientY: direction === "up" ? -500 : direction === "down" ? 50 : 0,
screenX: direction === "left" ? -50 : direction === "right" ? 50 : 0,
screenY: direction === "up" ? -500 : direction === "down" ? 50 : 0,
pageX: direction === "left" ? -50 : direction === "right" ? 50 : 0,
pageY: direction === "up" ? -500 : direction === "down" ? 50 : 0,
2024-09-21 21:58:25 +08:00
});
2024-09-21 22:36:11 +08:00
const touchStartEvent = new TouchEvent("touchstart", {
2024-09-21 21:58:25 +08:00
touches: [touchStart],
targetTouches: [touchStart],
changedTouches: [touchStart],
cancelable: true,
bubbles: true,
});
2024-09-21 22:36:11 +08:00
const touchMoveEvent = new TouchEvent("touchmove", {
2024-09-21 21:58:25 +08:00
touches: [touchMove],
targetTouches: [touchMove],
changedTouches: [touchMove],
cancelable: true,
bubbles: true,
});
2024-09-21 22:36:11 +08:00
const touchEndEvent = new TouchEvent("touchend", {
2024-09-21 21:58:25 +08:00
touches: [],
targetTouches: [],
changedTouches: [touchEnd],
cancelable: true,
bubbles: true,
});
element.dispatchEvent(touchStartEvent);
element.dispatchEvent(touchMoveEvent);
element.dispatchEvent(touchEndEvent);
}
2024-08-19 17:05:34 +08:00
//naver使用函数
const naver = async (r, id) => {
send_close(id);
2024-08-19 17:05:34 +08:00
};
2024-08-24 11:17:58 +08:00
/**
2024-08-26 14:34:55 +08:00
*
2024-08-24 11:17:58 +08:00
* @param {*} str 字符串
* @param {*} arr 需要匹配的字符串数组
* @returns boole
*/
function searchStringContainsArrayKeywords(str, arr) {
for (let i = 0; i < arr.length; i++) {
if (str.includes(arr[i])) {
return true;
}
}
return false;
}
/**
* 谷歌商城
2024-08-26 14:34:55 +08:00
* @param {*} r
* @param {*} id
*/
2024-08-22 16:25:33 +08:00
async function shopping(r, id) {
let number = getRandomInt(2, 10);
for (let index = 0; index < number; index++) {
RandomIntscrollBy();
await delay(getRandomInt(3, 7));
}
send_close(id);
}
/**
* 谷歌新闻
2024-08-26 14:34:55 +08:00
* @param {*} r
* @param {*} id
*/
2024-08-21 11:30:52 +08:00
async function news_google(r, id) {
//等待五秒怕加载速度过慢
await delay(3);
//点击完整报道
2024-09-21 22:36:11 +08:00
let sum = getRandomInt(10, 50);
2024-09-21 21:58:25 +08:00
for (let index = 0; index < sum; index++) {
2024-09-21 22:36:11 +08:00
RandomIntscrollBy();
await delay(getRandomInt(5, 10));
}
2024-08-21 17:35:28 +08:00
send_close(id);
}
//谷歌商店浏览
async function play_google(r, id) {
//等待五秒怕加载速度过慢
await delay(5);
//滑动次数
let a = getRandomInt(3, 5);
for (let index = 0; index < a; index++) {
RandomIntscrollBy();
await delay(getRandomInt(0, 5));
}
//点击推荐
if (getRandomInt(0, 1) == 0) {
$(".Pdcv8e").click();
await delay(getRandomInt(5, 10));
//返回主页
$("#kO001e > header > nav > a").click();
}
//热门游戏随机列表 直接触发点击事件就行
let popular_games = $(
"#yDmH0d > c-wiz.SSPGKf.glB9Ve > div > div > div.N4FjMb.Z97G4e > c-wiz > div > c-wiz > c-wiz:nth-child(4) > c-wiz > section > div > div > div > div > div.aoJE7e.b0ZfVe"
).children;
await delay(getRandomInt(5, 10));
//
send_close(id);
}
2024-08-21 11:30:52 +08:00
async function youtube_shorts2(r, id) {
//等待五秒怕加载速度过慢
await delay(5);
//观看次数
2024-09-21 21:58:25 +08:00
for (let index = 0; index < 5; index++) {
2024-09-21 22:36:11 +08:00
// 使用示例:
2024-09-21 21:58:25 +08:00
// let element = $('.YtShortsCarouselCarouselItem.carousel-item[aria-hidden$="false"]')
2024-09-21 22:36:11 +08:00
let element = document.querySelector(
".YtShortsCarouselCarouselItem.carousel-item"
);
triggerSwipeEvent(element, "up"); // 触发向左滑动事件
await delay(getRandomInt(10, 30));
2024-08-21 11:30:52 +08:00
}
//看完关闭窗口
send_close(id);
}
async function youtube(r, id) {
// 点击长视频观看
2024-08-26 14:34:55 +08:00
await delay(5);
2024-09-21 22:36:11 +08:00
$(
"#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();
2024-09-21 21:58:25 +08:00
await delay(getRandomInt(2000, 3600));
//看完关闭窗口
send_close(id);
}
function send_close(id) {
chrome.runtime.sendMessage({
ty: "remove",
id: id,
});
}
2024-08-19 18:02:42 +08:00
2024-08-19 17:05:34 +08:00
chrome.runtime.sendMessage(
{
ty: "get",
},
2024-08-19 18:02:42 +08:00
function (obj) {
let r = obj.name;
let id = obj.id;
console.log("传递过来的obj" + JSON.stringify(obj));
2024-09-21 22:36:11 +08:00
let functoons = [
{ name: "naver", funct: naver },
{ name: "youtube", funct: youtube },
{ name: "youtube_shorts", funct: youtube_shorts2 },
{ name: "news_google", funct: news_google },
{ name: "play_google", funct: play_google },
{ name: "shopping", funct: shopping },
];
for (let index = 0; index < functoons.length; index++) {
// const element = functoons[index];
let name=functoons[index].name
let funct=functoons[index].funct
if(r==name){
funct(r,id)
}
2024-08-19 17:05:34 +08:00
}
}
);
2024-08-20 10:05:28 +08:00
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log(request);
sendResponse("我收到了你的情书popup~");
});