/** * 阻塞代码运行 * @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++) { for (let index = 0; index < getRandomInt(2, 5); index++) { RandomIntscrollBy(); await delay(getRandomInt(5, 10)); } //获取所有的商品链接 let sp = document.getElementsByClassName( "gkQHve RmEs5b zypKDd aKoISd RycMEf" ); //触发点击事件 sp[getRandomInt(0, sp.length - 1)].click(); await delay(getRandomInt(10, 20)); //获取关闭按钮 let close = document.getElementsByClassName( "ioQ39e wv9iH MjJqGe cd29Sd" )[0]; close.click(); await delay(getRandomInt(2, 5)); } send_close(id); } /** * 谷歌新闻 * @param {*} r * @param {*} id */ async function news_google(r, id) { //等待五秒怕加载速度过慢 await delay(3); //滑动次数 let sum = getRandomInt(5, 9); for (let index = 0; index < sum; index++) { RandomIntscrollBy(); await delay(getRandomInt(5, 10)); } let news_array = $(".cDgn2c"); //随机点击模块 news_array[getRandomInt(0, news_array.length - 1)].click(); for (let index = 0; index < getRandomInt(3, 8); index++) { RandomIntscrollBy(); await delay(getRandomInt(3, 8)); } let new_array = $(".VDXfz"); let new_array_href = []; for (let index = 0; index < new_array.length; index++) { const element = new_array[index]; console.log(element.getAttribute("href")); let url = element.getAttribute("href").replace(/\./g, ""); new_array_href.push(`https://news.google.com${url}`); } // for (let index = 0; index < new_array_href.length; index++) { //这里只打开4个链接太多了有毒 for (let index = 0; index < 4; index++) { const element = new_array_href[index]; let a = open(element); // console.log(JSON.stringify(a)) await delay(3); a.close(); } 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)); } //热门游戏随机列表 直接触发点击事件就行 let popular_games = document.querySelectorAll("a.Si6A0c.itIJzb"); for (let index = 0; index < getRandomInt(3, 5); index++) { try { popular_games[getRandomInt(0, popular_games.length - 1)].click(); } catch (error) { console.log(error); } let a = getRandomInt(3, 5); for (let index = 0; index < a; index++) { RandomIntscrollBy(); await delay(getRandomInt(0, 5)); } $(".f0UV3d").click(); await delay(getRandomInt(5, 10)); } 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) { async function whict() { await delay(5); for (let index = 0; index < getRandomInt(5, 10); index++) { RandomIntscrollBy(); } let all_video = $(".media-item-thumbnail-container"); all_video[getRandomInt(0, all_video.length - 1)].click(); // if(getRandomInt(0,1)==0){ // //点赞 // document.getElementsByClassName('yt-spec-touch-feedback-shape__fill')[3].click() // //订阅 // document.getElementsByClassName('yt-spec-touch-feedback-shape__fill')[2].click() // } await delay(getRandomInt(300, 1800)); } for (let index = 0; index < getRandomInt(5, 10); index++) { await whict(); } //看完关闭窗口 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)); 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 }, ]; let is_Match = false; 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) { is_Match = true; funct(r, id); } } } ); chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { console.log(request); sendResponse("我收到了你的情书,popup~"); });