// ==UserScript== // @name TITOK全域自动勾选 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 加入 Alt+Q 快捷键,解决点击按钮导致下拉框消失的问题 // @match https://ads.tiktok.com/* // @grant none // @license MIT // ==/UserScript== (function() { 'use strict'; const excludeCountries = ["巴基斯坦", "乌克兰", "美国", "英国", "马来西亚", "菲律宾"]; const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); // --- 1. 创建悬浮按钮 --- const btn = document.createElement('button'); // 按钮文字加上快捷键提示 btn.textContent = '⚡ 全域自动勾选 (Alt+Q)'; btn.style.cssText = ` position: fixed; bottom: 50px; right: 50px; z-index: 99999; padding: 12px 20px; background-color: #fe2c55; color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: bold; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.3); `; document.body.appendChild(btn); // --- 2. 核心执行逻辑 --- async function run() { console.log("🚀 开始全域自动勾选..."); let searchInput = document.querySelector('input[placeholder="输入目标地域"]'); // 自动帮你点开下拉框 if (!searchInput) { console.log("👉 未找到输入框,尝试自动点开下拉框..."); const xpath = "//*[contains(text(), '搜索或选择地域')]"; const triggerNode = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; if (triggerNode) { let clickTarget = triggerNode.closest('div[class*="select"]') || triggerNode.parentElement.parentElement || triggerNode; clickTarget.click(); await sleep(1000); searchInput = document.querySelector('input[placeholder="输入目标地域"]'); } } if (!searchInput) { alert("❌ 依然没找到搜索框!\n请先手动点开下拉框,看到输入框后再点按钮。"); return; } searchInput.focus(); await sleep(300); let checkedCount = 0; let noChangeCount = 0; let totalLoops = 0; function getListContainer() { const lists = document.querySelectorAll('.rc-virtual-list-holder, .ant-tree-list-holder, .ant-select-tree-list'); for (const list of lists) { if (list.scrollHeight > list.clientHeight) return list; } return null; } const SCROLL_STEP = 250; const MAX_LOOPS = 60; while (totalLoops < MAX_LOOPS) { totalLoops++; btn.textContent = `⏳ 扫描中 (${checkedCount} 个)`; const titleNodes = document.querySelectorAll('.ant-tree-title'); let foundNewInThisRound = false; for (const node of titleNodes) { const text = node.textContent.trim(); if (!text || text.includes('请输入') || text.length > 10) continue; if (excludeCountries.some(ex => text.includes(ex))) continue; const treeNode = node.closest('.ant-tree-treenode'); if (treeNode) { const checkbox = treeNode.querySelector('.ant-tree-checkbox'); if (checkbox && !checkbox.classList.contains('ant-tree-checkbox-checked')) { checkbox.click(); checkedCount++; foundNewInThisRound = true; console.log(`✅ 勾选: ${text}`); await sleep(80); } } } if (foundNewInThisRound) { noChangeCount = 0; } else { noChangeCount++; if (noChangeCount >= 6) { console.log("🛑 连续多次未发现新国家,自动结束。"); break; } } const listContainer = getListContainer(); if (listContainer) { const oldScroll = listContainer.scrollTop; listContainer.scrollTop += SCROLL_STEP; if (listContainer.scrollTop === oldScroll) { const wheelEvent = new WheelEvent('wheel', { deltaY: SCROLL_STEP, bubbles: true }); listContainer.dispatchEvent(wheelEvent); } } else { const downEvent = new KeyboardEvent('keydown', { key: 'ArrowDown', code: 'ArrowDown', keyCode: 40, which: 40, bubbles: true }); searchInput.dispatchEvent(downEvent); } await sleep(600); } console.log(`🎉 全部结束!共勾选 ${checkedCount} 个国家。`); btn.textContent = `✅ 完成 (共${checkedCount}个)`; btn.style.backgroundColor = "#00c853"; setTimeout(() => { btn.textContent = '⚡ 全域自动勾选 (Alt+Q)'; btn.style.backgroundColor = '#fe2c55'; }, 5000); } // --- 3. 绑定按钮点击事件(带防消失) --- btn.addEventListener('mousedown', (e) => { e.preventDefault(); e.stopPropagation(); }); btn.addEventListener('click', (e) => { e.stopPropagation(); if (btn.textContent.includes('扫描中')) return; btn.style.backgroundColor = '#888'; btn.textContent = '⏳ 扫描中...'; run().catch(err => { console.error("出错:", err); btn.textContent = '❌ 出错,请刷新页面'; btn.style.backgroundColor = '#fe2c55'; }); }); // --- 4. 绑定全局快捷键 Alt + Q --- window.addEventListener('keydown', (e) => { // 检测是否按下了 Alt + Q (或 altKey 和 Q键) if (e.altKey && e.code === 'KeyQ') { e.preventDefault(); // 阻止浏览器默认行为 console.log("⌨️ 检测到快捷键 Alt+Q"); // 模拟点击按钮 if (!btn.textContent.includes('扫描中')) { btn.click(); } } /* 💡 如果你执意要用 Ctrl+Q,可以将上面的判断改为: if (e.ctrlKey && e.code === 'KeyQ') { ... } 但请注意,大多数浏览器会优先拦截 Ctrl+Q 作为关闭页面的快捷键。 */ }); })();