You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tampermonkey脚本优化:自动触发日历并检测预约可用日期

解决预约日期检测脚本的问题

方案一:自动触发日历显示后检测

你的脚本当前仅在手动点开日历后生效,是因为day activeClass元素只有点击输入框后才会被渲染到DOM中。可以添加自动点击输入框的逻辑,确保日历DOM加载完成后再检测:

// ==UserScript==
// @name         预约日期监控脚本
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动检测预约可用日期并触发警报
// @author       You
// @match        替换为你的预约网站完整URL
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 触发日历显示的函数
    function showCalendar() {
        const dateInput = document.getElementById('app_date');
        // 仅在未找到可用日期时重复触发,避免无效操作
        if (dateInput && !document.querySelector('.day.activeClass')) {
            dateInput.click();
        }
    }

    // 检测可用日期的函数
    function checkAvailableDates() {
        const availableDays = document.getElementsByClassName('day activeClass');
        if (availableDays.length !== 0) {
            new Audio('https://www.soundjay.com/mechanical/sounds/smoke-detector-1.mp3').play();
            clearInterval(monitorTimer);
            alert('发现可用预约日期!');
        }
    }

    // 每隔1秒执行一次:先触发日历,再检测日期
    const monitorTimer = setInterval(() => {
        showCalendar();
        checkAvailableDates();
    }, 1000);
})();

方案二:直接抓取接口数据(更高效稳定)

多数预约网站的日历数据是通过AJAX接口获取的,无需依赖页面DOM渲染,这种方式更可靠,还能避免频繁操作页面元素被网站拦截:

  1. 打开浏览器开发者工具(F12),切换到「网络」标签
  2. 手动点击日历输入框,找到返回日期数据的XHR/Fetch请求,记录接口URL和请求方式
  3. 替换到以下脚本中:
// ==UserScript==
// @name         预约日期监控脚本(接口版)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  通过接口直接检测预约可用日期
// @author       You
// @match        替换为你的预约网站完整URL
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    function checkDatesFromAPI() {
        // 替换为你找到的日期接口URL
        const apiUrl = 'https://example.com/api/available-dates';
        GM_xmlhttpRequest({
            method: 'GET', // 根据实际接口调整为POST或其他方式
            url: apiUrl,
            // 若接口需要请求头,可在此添加,比如Cookie、User-Agent
            headers: {
                'Cookie': document.cookie,
                'User-Agent': navigator.userAgent
            },
            onload: function(res) {
                const dateData = JSON.parse(res.responseText);
                // 根据接口返回的JSON结构修改判断逻辑
                // 示例:假设接口返回的availableDates数组不为空则触发警报
                if (dateData.availableDates && dateData.availableDates.length > 0) {
                    new Audio('https://www.soundjay.com/mechanical/sounds/smoke-detector-1.mp3').play();
                    clearInterval(apiTimer);
                    alert('发现可用预约日期!');
                }
            }
        });
    }

    // 每隔5秒请求一次接口(可根据网站反爬规则调整间隔)
    const apiTimer = setInterval(checkDatesFromAPI, 5000);
})();

注意事项

  • 不要把检测间隔设得过短,避免触发网站的反爬机制导致IP被封禁
  • 若使用接口方案,需根据实际返回的数据结构调整判断逻辑,比如部分接口会用isAvailable字段标记日期是否可用

内容的提问来源于stack exchange,提问作者mahal jirari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:05:27