chrome_extensions_phone/lib/content.js

254 lines
6.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 阻塞代码运行
* @param {*} n 秒数
* @returns
*/
function delay(n) {
return new Promise(function (resolve) {
setTimeout(resolve, n * 1000);
});
}
/**
* 随机数生成
* @param {*} min 最小值
* @param {*} max 最大值
* @returns
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* 随机滑动函数
* @param {*} y 滑动像素 不传递默认随机
*/
function RandomIntscrollBy(y = 0) {
let rand = getRandomInt(100, 600);
scrollBy(0, y == 0 ? rand : y);
console.log("随机滑动" + rand);
}
/**
*
* @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,
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,
});
const touchMove = new Touch({
identifier: Date.now() + 2,
target: element,
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,
});
const touchStartEvent = new TouchEvent('touchstart', {
touches: [touchStart],
targetTouches: [touchStart],
changedTouches: [touchStart],
cancelable: true,
bubbles: true,
});
const touchMoveEvent = new TouchEvent('touchmove', {
touches: [touchMove],
targetTouches: [touchMove],
changedTouches: [touchMove],
cancelable: true,
bubbles: true,
});
const touchEndEvent = new TouchEvent('touchend', {
touches: [],
targetTouches: [],
changedTouches: [touchEnd],
cancelable: true,
bubbles: true,
});
element.dispatchEvent(touchStartEvent);
element.dispatchEvent(touchMoveEvent);
element.dispatchEvent(touchEndEvent);
}
//naver使用函数
const naver = async (r, id) => {
send_close(id);
};
/**
*
* @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;
}
/**
* 谷歌商城
* @param {*} r
* @param {*} id
*/
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);
}
/**
* 谷歌新闻
* @param {*} r
* @param {*} id
*/
async function news_google(r, id) {
//等待五秒怕加载速度过慢
await delay(3);
//点击完整报道
let sum=getRandomInt(10,50)
for (let index = 0; index < sum; index++) {
RandomIntscrollBy()
await delay(getRandomInt(5,10));
}
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);
}
async function youtube_shorts2(r, id) {
//等待五秒怕加载速度过慢
await delay(5);
//观看次数
for (let index = 0; index < 5; index++) {
// 使用示例:
// let element = $('.YtShortsCarouselCarouselItem.carousel-item[aria-hidden$="false"]')
let element = document.querySelector('.YtShortsCarouselCarouselItem.carousel-item')
triggerSwipeEvent(element, 'up'); // 触发向左滑动事件
await delay(getRandomInt(10, 30))
}
//看完关闭窗口
send_close(id);
}
async function youtube(r, id) {
// 点击长视频观看
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()
await delay(getRandomInt(2000, 3600));
//看完关闭窗口
send_close(id);
}
function send_close(id) {
chrome.runtime.sendMessage({
ty: "remove",
id: id,
});
}
chrome.runtime.sendMessage(
{
ty: "get",
},
function (obj) {
let r = obj.name;
let id = obj.id;
console.log("传递过来的obj" + JSON.stringify(obj));
switch (r) {
case "naver":
naver(r, id);
break;
case "youtube":
youtube(r, id);
break;
case "youtube_shorts":
youtube_shorts2(r, id);
break;
case "news_google":
news_google(r, id);
break;
case "play_google":
play_google(r, id);
break;
case "shopping":
shopping(r, id);
break;
case "youtube":
youtube(r, id);
break;
default:
naver(r, id);
break;
}
}
);
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log(request);
sendResponse("我收到了你的情书popup~");
});