// ==UserScript== // @name 快手页面自动刷新与商品自动讲解 // @namespace http://tampermonkey.net/ // @version 1.2 // @description 快手跟播助手页面检测到"刷新页面"或"我知道了"文字时自动刷新网页;同时自动讲解快手直播间的1号商品,并在掉讲解后重新开始。 // @author You // @match https://zs.kwaixiaodian.com/page/helper // @grant none // ==/UserScript== (function() { 'use strict'; // ==================== 自动刷新功能 ==================== // 定义要查找的文本数组 const targetTexts = ["刷新页面", "我知道了"]; // 是否已找到目标文本 let foundTarget = false; // 检查页面中是否包含目标文本 function checkForText() { // 如果已经找到目标文本,不再继续检查 if (foundTarget) return; // 检查整个页面的文本内容 if (document.body) { const pageText = document.body.textContent; // 检查是否包含任一目标文本 for (const text of targetTexts) { if (pageText.includes(text)) { console.log(`检测到"${text}"文字,即将刷新...`); foundTarget = true; window.location.reload(); return; } } } // 如果没找到,等待后再次检查 setTimeout(checkForText, 1000); } // ==================== 自动讲解功能 ==================== let checkInterval; let isMonitoring = false; function startMonitoring() { if (isMonitoring) return; console.log('开始监控1号商品讲解状态...'); isMonitoring = true; // 立即检查一次 checkAndExplainProductOne(); // 设置定时检查(每10秒检查一次) checkInterval = setInterval(checkAndExplainProductOne, 10000); } function stopMonitoring() { if (checkInterval) { clearInterval(checkInterval); checkInterval = null; } isMonitoring = false; console.log('停止监控1号商品讲解状态'); } function checkAndExplainProductOne() { console.log('检查1号商品讲解状态...'); // 尝试查找商品面板 const productPanel = findProductPanel(); if (!productPanel) { console.log('未找到商品面板'); return; } // 查找1号商品 const productOne = findProductOne(productPanel); if (!productOne) { console.log('未找到1号商品'); return; } // 检查讲解状态并触发讲解 checkExplanationStatus(productOne); } function findProductPanel() { // 尝试多种可能的选择器来找到商品面板 const possibleSelectors = [ '.goods-panel', // 常见商品面板类名 '[class*="goods"]', '[class*="product"]', '.product-list', '.goods-list', '.product-container' ]; for (const selector of possibleSelectors) { const element = document.querySelector(selector); if (element) { console.log('找到商品面板:', selector); return element; } } // 如果通过类名找不到,尝试通过标签特性查找 const allDivs = document.querySelectorAll('div'); for (const div of allDivs) { if (div.innerText && div.innerText.includes('商品') && div.children.length > 0) { console.log('通过文本内容找到商品面板'); return div; } } return null; } function findProductOne(panel) { // 在商品面板中查找商品项 const productItems = panel.querySelectorAll('[class*="item"], [class*="product"], [class*="goods"]'); console.log(`找到 ${productItems.length} 个可能的商品项`); if (productItems.length === 0) { return null; } // 假设第一个商品是1号商品 const productOne = productItems[0]; console.log('找到1号商品'); return productOne; } function checkExplanationStatus(productElement) { // 尝试查找讲解按钮 const explainButtons = productElement.querySelectorAll('button'); let explainButton = null; for (const button of explainButtons) { if (button.innerText && (button.innerText.includes('讲解') || button.innerText.includes('讲解中'))) { explainButton = button; break; } } // 如果找不到讲解按钮,尝试在父元素中查找 if (!explainButton) { const parentElement = productElement.parentElement; if (parentElement) { const parentButtons = parentElement.querySelectorAll('button'); for (const button of parentButtons) { if (button.innerText && (button.innerText.includes('讲解') || button.innerText.includes('讲解中'))) { explainButton = button; break; } } } } if (!explainButton) { console.log('未找到讲解按钮'); return; } // 检查按钮状态 if (explainButton.innerText.includes('讲解中')) { console.log('1号商品正在讲解中'); } else if (explainButton.innerText.includes('讲解') || explainButton.innerText.includes('开始讲解')) { console.log('1号商品未在讲解中,尝试开始讲解'); simulateClick(explainButton); } } function simulateClick(element) { // 创建鼠标事件 const mouseEvent = new MouseEvent('click', { view: window, bubbles: true, cancelable: true }); // 触发事件 element.dispatchEvent(mouseEvent); console.log('已模拟点击讲解按钮'); } // ==================== 初始化功能 ==================== // 页面加载完成后开始检查文本 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', checkForText); } else { checkForText(); } // 等待页面完全加载后开始监控商品讲解 window.addEventListener('load', function() { setTimeout(startMonitoring, 5000); // 5秒后开始监控 }); // 添加一个停止监控的函数,可以通过控制台调用 window.stopAutoExplain = stopMonitoring; console.log('自动刷新和自动讲解脚本已加载,如需停止监控讲解,请在控制台输入: stopAutoExplain()'); })();