// ==UserScript== // @name weiyong2222 // @namespace http://tampermonkey.net/ // @version 2025-11-21-6 // @description 自动跟进系统 - 简化版 // @author You // @match http://call.changmujiayu.cn/web/2024/* // @match https://call.changmujiayu.cn/web/2024/* // @grant none // ==/UserScript== (function() { 'use strict'; console.log('脚本开始执行...'); // 立即拦截所有网页弹窗 (function() { console.log('设置弹窗拦截...'); // 保存原始函数 window._originalAlert = window.alert; window._originalConfirm = window.confirm; window._originalPrompt = window.prompt; // 重写弹窗函数 window.alert = function(msg) { console.log('拦截alert:', msg); return; }; window.confirm = function(msg) { console.log('拦截confirm:', msg); return true; }; window.prompt = function(msg, def) { console.log('拦截prompt:', msg); return def || ''; }; console.log('弹窗拦截已设置'); })(); // 简单提示 function showToast(msg, type = 'info') { const toast = document.createElement('div'); toast.textContent = msg; toast.style.cssText = ` position: fixed; top: 20px; right: 20px; padding: 12px 24px; background: ${type === 'error' ? '#ff4444' : '#4CAF50'}; color: white; border-radius: 4px; box-shadow: 0 2px 4px rgba(0,0,0,0.2); z-index: 99999; `; document.body.appendChild(toast); setTimeout(() => toast.remove(), 3000); } // 等待元素出现 function waitForElement(selector, timeout = 2000) { return new Promise((resolve) => { const start = Date.now(); function check() { const el = document.querySelector(selector); if (el) { resolve(el); return; } if (Date.now() - start < timeout) { setTimeout(check, 100); } else { resolve(null); } } check(); }); } // 主函数 async function main() { console.log('开始主流程...'); try { // 1. 等待关键元素 const date1 = await waitForElement('#date1'); const date2 = await waitForElement('#date2'); const pkey = await waitForElement('#pkey_id'); const hasName = await waitForElement('#hasName'); if (!date1 || !date2 || !pkey || !hasName) { console.log('关键元素未找到'); return; } console.log('关键元素已找到'); // 2. 设置基础筛选条件 const today = new Date(); const dateStr = today.toISOString().split('T')[0]; date1.value = dateStr; date2.value = dateStr; hasName.value = ''; // 取消姓名筛选 pkey.value = '4'; // YXH-D平台 // 触发事件 [date1, date2, hasName, pkey].forEach(el => { el.dispatchEvent(new Event('change', { bubbles: true })); el.dispatchEvent(new Event('input', { bubbles: true })); }); console.log('基础筛选条件已设置'); // 3. 等待渠道下拉框更新(平台变更后) await new Promise(resolve => setTimeout(resolve, 500)); // 4. 设置渠道 const ckey = document.getElementById('ckey_id'); if (ckey) { const savedChannel = localStorage.getItem('weiyong222_selected_channel'); if (savedChannel !== null) { // 检查渠道是否可用 let available = false; for (let i = 0; i < ckey.options.length; i++) { if (ckey.options[i].value === savedChannel) { available = true; break; } } if (available || savedChannel === '') { ckey.value = savedChannel; ckey.dispatchEvent(new Event('change', { bubbles: true })); console.log('渠道已设置:', savedChannel); } } // 添加渠道选择按钮 addChannelButton(); } // 5. 执行筛选 await new Promise(resolve => setTimeout(resolve, 500)); const filterBtn = document.querySelector('.btn-primary.btn-block'); if (filterBtn && filterBtn.textContent.includes('筛选')) { filterBtn.click(); showToast('自动筛选完成'); console.log('筛选按钮已点击'); // 开始自动跟进 setTimeout(startAutoFollow, 300); } console.log('主流程完成'); } catch (error) { console.error('执行出错:', error); showToast('脚本执行出错', 'error'); } } // 自动跟进功能 function startAutoFollow() { console.log('开始自动跟进...'); // 检查客户列表 checkCustomers(); // 每5秒检查一次 setInterval(() => { checkCustomers(); }, 300); } function checkCustomers() { const rows = document.querySelectorAll('#load tr'); let followed = 0; rows.forEach(row => { const timeCell = row.querySelector('td:nth-child(5)'); if (timeCell) { const timeText = timeCell.textContent.trim(); const customerTime = parseTime(timeText); const now = new Date(); const threeMinsAgo = new Date(now.getTime() - 3 * 60 * 1000); if (customerTime && customerTime > threeMinsAgo) { const followBtn = row.querySelector('a[onclick*="follow"]'); if (followBtn && followBtn.textContent.includes('开始跟进')) { followBtn.click(); followed++; console.log('跟进客户:', timeText); } } } }); if (followed > 0) { // 跟进成功,不显示弹窗 console.log('跟进 ' + followed + ' 个客户'); } } function parseTime(timeStr) { if (!timeStr) return null; try { // 尝试解析时间格式 const match = timeStr.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/); if (match) { return new Date(match[1], match[2]-1, match[3], match[4], match[5], match[6]); } const match2 = timeStr.match(/(\d{4})-(\d{2})-(\d{2})/); if (match2) { return new Date(match2[1], match2[2]-1, match2[3]); } } catch(e) { console.log('时间解析错误:', e); } return null; } // 添加固定渠道选择按钮 function addChannelButton() { if (document.querySelector('#channel-buttons')) return; const container = document.querySelector('.row .col-xl-1.col-md-6'); if (!container) return; // 创建按钮容器 const buttonContainer = document.createElement('div'); buttonContainer.id = 'channel-buttons'; buttonContainer.style.cssText = 'margin-top: 10px; display: flex; gap: 5px; flex-direction: column;'; // 创建趣记花按钮 const qujihuaBtn = document.createElement('button'); qujihuaBtn.type = 'button'; qujihuaBtn.className = 'btn btn-success btn-block'; qujihuaBtn.innerHTML = '趣记花'; qujihuaBtn.style.padding = '8px 12px'; qujihuaBtn.onclick = function() { selectChannel('趣记花'); }; // 创建微融钱包按钮 const weirongBtn = document.createElement('button'); weirongBtn.type = 'button'; weirongBtn.className = 'btn btn-warning btn-block'; weirongBtn.innerHTML = '微融钱包'; weirongBtn.style.padding = '8px 12px'; weirongBtn.onclick = function() { selectChannel('微融钱包'); }; // 添加到容器 buttonContainer.appendChild(qujihuaBtn); buttonContainer.appendChild(weirongBtn); // 添加到筛选按钮旁边 container.parentNode.insertBefore(buttonContainer, container.nextSibling); console.log('固定渠道按钮已添加'); } // 选择渠道函数 function selectChannel(channelName) { const ckey = document.getElementById('ckey_id'); if (!ckey) { showToast('渠道选择框未找到', 'error'); return; } // 查找对应的渠道值 let channelValue = ''; for (let i = 0; i < ckey.options.length; i++) { const option = ckey.options[i]; if (option.text.includes(channelName)) { channelValue = option.value; break; } } if (channelValue) { ckey.value = channelValue; ckey.dispatchEvent(new Event('change', { bubbles: true })); // 保存渠道选择到本地存储 localStorage.setItem('weiyong222_selected_channel', channelValue); // 重新执行筛选 setTimeout(() => { const filterBtn = document.querySelector('.btn-primary.btn-block'); if (filterBtn && filterBtn.textContent.includes('筛选')) { filterBtn.click(); showToast(`已选择渠道: ${channelName}`, 'info'); } }, 500); } else { showToast(`未找到渠道: ${channelName}`, 'error'); } } // 启动 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', main); } else { setTimeout(main, 100); } // 自动跟进功能 let autoFollowEnabled = true; let autoRefreshEnabled = true; let refreshTimer = null; let lastFollowTime = Date.now(); // 开始自动跟进 function startAutoFollow() { console.log('开始自动跟进...'); // 检查客户列表 checkCustomers(); // 每5秒检查一次 setInterval(() => { if (autoFollowEnabled) { checkCustomers(); } }, 300); } function checkCustomers() { if (!autoFollowEnabled) return; const rows = document.querySelectorAll('#load tr'); let followed = 0; rows.forEach(row => { const timeCell = row.querySelector('td:nth-child(5)'); if (timeCell) { const timeText = timeCell.textContent.trim(); const customerTime = parseTime(timeText); const now = new Date(); const threeMinsAgo = new Date(now.getTime() - 3 * 60 * 1000); if (customerTime && customerTime > threeMinsAgo) { const followBtn = row.querySelector('a[onclick*="follow"]'); if (followBtn && followBtn.textContent.includes('开始跟进')) { followBtn.click(); followed++; lastFollowTime = Date.now(); if (refreshTimer) { clearInterval(refreshTimer); refreshTimer = null; } } } } }); if (followed > 0) { // 跟进成功,不显示弹窗 console.log('跟进 ' + followed + ' 个客户'); } else if (autoRefreshEnabled) { // 10秒内没跟进,开始自动刷新 const timeSinceLast = Date.now() - lastFollowTime; if (timeSinceLast > 2000 && !refreshTimer) { startAutoRefresh(); } } } // 移除弹窗拦截功能,让网页弹窗正常工作 function parseTime(timeStr) { if (!timeStr) return null; try { // 尝试解析时间格式 const match = timeStr.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/); if (match) { return new Date(match[1], match[2]-1, match[3], match[4], match[5], match[6]); } const match2 = timeStr.match(/(\d{4})-(\d{2})-(\d{2})/); if (match2) { return new Date(match2[1], match2[2]-1, match2[3]); } } catch(e) { console.log('时间解析错误:', e); } return null; } function startAutoRefresh() { console.log('开始自动刷新...'); let count = 0; refreshTimer = setInterval(() => { if (!autoRefreshEnabled || !autoFollowEnabled) { clearInterval(refreshTimer); refreshTimer = null; return; } count++; // 在刷新前设置选定的渠道 const channelId = localStorage.getItem('weiyong222_selected_channel'); if (channelId) { const ckey = document.getElementById('ckey_id'); if (ckey && ckey.value !== channelId) { ckey.value = channelId; ckey.dispatchEvent(new Event('change', { bubbles: true })); } } // 等待渠道设置生效 setTimeout(() => { const btn = document.querySelector('.btn-primary.btn-block'); if (btn && btn.textContent.includes('筛选')) { btn.click(); showToast('自动刷新 ' + count + ' 次'); // 刷新后检查客户 setTimeout(checkCustomers, 300); } }, 500); }, 3000); } // 添加控制面板 function addControlPanel() { const panel = document.createElement('div'); panel.innerHTML = `
自动跟进设置
状态: 运行中
`; document.body.appendChild(panel); // 事件监听 document.getElementById('af-toggle').addEventListener('change', function(e) { autoFollowEnabled = e.target.checked; document.getElementById('status').textContent = autoFollowEnabled ? '运行中' : '已暂停'; showToast('自动跟进 ' + (autoFollowEnabled ? '启用' : '禁用')); }); document.getElementById('ar-toggle').addEventListener('change', function(e) { autoRefreshEnabled = e.target.checked; if (!autoRefreshEnabled && refreshTimer) { clearInterval(refreshTimer); refreshTimer = null; } showToast('自动刷新 ' + (autoRefreshEnabled ? '启用' : '禁用')); }); document.getElementById('check-btn').addEventListener('click', function() { checkCustomers(); }); document.getElementById('stop-btn').addEventListener('click', function() { if (refreshTimer) { clearInterval(refreshTimer); refreshTimer = null; showToast('已停止刷新'); } }); document.getElementById('refresh-btn').addEventListener('click', function() { const filterBtn = document.querySelector('.btn-primary.btn-block'); if (filterBtn) { filterBtn.click(); showToast('重新筛选完成'); setTimeout(checkCustomers, 2000); } }); } // 修改主函数,在筛选完成后开始自动跟进 async function main() { console.log('开始主流程...'); try { // 1. 等待关键元素 const date1 = await waitForElement('#date1'); const date2 = await waitForElement('#date2'); const pkey = await waitForElement('#pkey_id'); const hasName = await waitForElement('#hasName'); if (!date1 || !date2 || !pkey || !hasName) { console.log('关键元素未找到'); return; } console.log('关键元素已找到'); // 2. 设置基础筛选条件 const today = new Date(); const dateStr = today.toISOString().split('T')[0]; date1.value = dateStr; date2.value = dateStr; hasName.value = ''; // 取消姓名筛选 pkey.value = '4'; // YXH-D平台 // 触发事件 [date1, date2, hasName, pkey].forEach(el => { el.dispatchEvent(new Event('change', { bubbles: true })); el.dispatchEvent(new Event('input', { bubbles: true })); }); console.log('基础筛选条件已设置'); // 3. 等待渠道下拉框更新(平台变更后) await new Promise(resolve => setTimeout(resolve, 500)); // 4. 设置渠道 const ckey = document.getElementById('ckey_id'); if (ckey) { const savedChannel = localStorage.getItem('weiyong222_selected_channel'); if (savedChannel !== null) { // 检查渠道是否可用 let available = false; for (let i = 0; i < ckey.options.length; i++) { if (ckey.options[i].value === savedChannel) { available = true; break; } } if (available || savedChannel === '') { ckey.value = savedChannel; ckey.dispatchEvent(new Event('change', { bubbles: true })); console.log('渠道已设置:', savedChannel); } } // 添加渠道选择按钮 addChannelButton(); } // 5. 执行筛选 await new Promise(resolve => setTimeout(resolve, 1000)); const filterBtn = document.querySelector('.btn-primary.btn-block'); if (filterBtn && filterBtn.textContent.includes('筛选')) { filterBtn.click(); showToast('自动筛选完成'); console.log('筛选按钮已点击'); // 添加控制面板 setTimeout(addControlPanel, 300); // 开始自动跟进 setTimeout(startAutoFollow, 500); } console.log('主流程完成'); } catch (error) { console.error('执行出错:', error); showToast('脚本执行出错', 'error'); } } })();