天巡历史价格记录

比较现在和上次记录的价格

当前为 2025-06-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         天巡历史价格记录
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  比较现在和上次记录的价格
// @author       你的名字
// @match        https://www.tianxun.com/transport/flights/*
// @grant        GM_setValue
// @grant        GM_getValue
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 等待页面加载完成
    function waitForElement(selector, callback) {
        const el = document.querySelector(selector);
        if (el) {
            callback(el);
        } else {
            setTimeout(() => waitForElement(selector, callback), 500);
        }
    }

    // 等待页面中出现"条结果"文本
    function waitForText(text, callback) {
        console.log('等待页面中出现"条结果"文本');
        const el = Array.from(document.querySelectorAll('body *')).find(e => e.textContent.includes(text));
        if (el) {
            callback(el);
        } else {
            setTimeout(() => waitForText(text, callback), 1000);
        }
    }

    waitForText('条结果', () => {
        waitForElement('.FlightsResults_dayViewItems__NzJiY', (container) => {

            // 获取当前搜索结果的日期
            const dateInput = document.getElementById('outbound_date');
            const searchDate = dateInput ? dateInput.value : '';

            // 添加一个key,从<div class="SearchDetails_location__NDZmN">下的文本获取
            const searchLocation = document.querySelector('.SearchDetails_location__NDZmN')?.textContent.trim();

            // 获取所有子div
            const divs = Array.from(container.children);
            if (divs.length <= 1) return;

            // 排除第一个div
            divs.slice(1).forEach(div => {
                // 获取航司名
                const airlineSpan = div.querySelector('.LogoImage_label__ZjIxY');
                const airline = airlineSpan ? airlineSpan.textContent.trim() : '';

                // 获取出发和到达时间
                const departDiv = div.querySelector('.LegInfo_routePartialDepart__YTMzN');
                const departTime = departDiv ? departDiv.textContent.trim() : '';

                // 获取到达时间
                const arriveDiv = div.querySelector('.LegInfo_routePartialArrive__ZjZlZ');
                const arriveTime = arriveDiv ? arriveDiv.textContent.trim() : '';

                // 获取价格
                const priceDiv = div.querySelector('.Price_mainPriceContainer__NTJmZ');
                const price = priceDiv ? priceDiv.textContent.trim() : '';

                if (airline && departTime && price && searchDate) {
                    const key = `${airline}_${departTime}__${arriveTime}_${searchLocation}_${searchDate}`;

                    // 这里添加代码
                    let record = GM_getValue(key, null);
                    let lastPrice = null;
                    let minPrice = null;
                    if (record) {
                        try {
                            record = JSON.parse(record);
                            lastPrice = record.lastPrice;
                            minPrice = record.minPrice;
                        } catch (e) {
                            lastPrice = record;
                            minPrice = record;
                        }
                    }
                    const infoSpan = document.createElement('span');
                    infoSpan.style.marginLeft = '10px';
                    infoSpan.style.color = '#f60';
                    if (lastPrice !== null) {
                        if (lastPrice === price) {
                            infoSpan.textContent = '没有变化';
                        } else {
                            infoSpan.textContent = `上次价格:${lastPrice}`;
                        }
                        if (minPrice !== null) {
                            infoSpan.textContent += ` / 历史最低:${minPrice}`;
                        }
                        div.appendChild(infoSpan);
                        // 更新lastPrice和minPrice
                        const newMin = minPrice === null ? price : (parseFloat(price.replace(/[^\d.]/g, '')) < parseFloat(minPrice.replace(/[^\d.]/g, '')) ? price : minPrice);
                        GM_setValue(key, JSON.stringify({ lastPrice: price, minPrice: newMin }));
                    } else {
                        // 首次保存
                        GM_setValue(key, JSON.stringify({ lastPrice: price, minPrice: price }));
                        console.log(`已保存: ${key} => ${price}`);
                    }
                }
            });
        });
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址