Greasy Fork

Greasy Fork is available in English.

世图增加已下载的红色链接

为世图科研助手增加已下载的红色链接,10天内点击记录生效,点击立即变色;动态元素持续监听;

当前为 2025-05-24 提交的版本,查看 最新版本

// ==UserScript==
// @name         世图增加已下载的红色链接
// @namespace    http://tampermonkey.net/
// @version      2025-05-017-001
// @description  为世图科研助手增加已下载的红色链接,10天内点击记录生效,点击立即变色;动态元素持续监听;
// @author       You
// @match        https://research.worldlib.site/result_query.aspx
// @icon         https://www.google.com/s2/favicons?sz=64&domain=worldlib.site
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // 添加样式
    function addVisitedStyle() {
        if (!document.querySelector('style[data-visited-style]')) {
            const style = document.createElement('style');
            style.setAttribute('data-visited-style', '');
            style.textContent = `
                .custom-visited {
                    color: #e233eb !important;
                    text-decoration: underline !important;
                }
              .cTable  a:visited {
                    color: #e233eb !important;
                }
            `;
            document.head.appendChild(style);
        }
    }

    // 当前时间戳
    function now() {
        return Date.now();
    }

    const EXPIRY_MS = 10 * 24 * 60 * 60 * 1000; // 10 天
    let visited = {}; // 全局有效链接记录

    // 只负责从 localStorage 加载全部记录,不清理
    function loadVisitedLinks() {
        visited = JSON.parse(localStorage.getItem('visitedLinks') || '{}');
    }

    // 保存点击链接并清理过期项
    function saveVisitedLink(href) {
        const current = now();
        visited[href] = current;

        const valid = {};
        for (const h in visited) {
            if (current - visited[h] < EXPIRY_MS) {
                valid[h] = visited[h];
            }
        }

        visited = valid;
        localStorage.setItem('visitedLinks', JSON.stringify(valid));
    }

    // 添加下载按钮和访问标记
    function addDownloadLinks() {
        document.querySelectorAll('.cTable  a[data-url]').forEach(link => {
            if (!link.nextElementSibling || !link.nextElementSibling.classList.contains('new-download-link')) {
                const href = link.getAttribute('data-url');
                const newA = document.createElement('a');
                newA.textContent = "下载";
                newA.href = href;
                newA.target = '_blank';
                newA.classList.add('new-download-link');

                if (visited[href] && now() - visited[href] < EXPIRY_MS) {
                    newA.classList.add('custom-visited');
                    newA.textContent = "已下载"
                }

                newA.addEventListener('click', () => {
                    saveVisitedLink(href);
                    newA.classList.add('custom-visited');
                    newA.textContent = "已下载"
                });

                link.parentNode.appendChild(newA);
            }
        });
    }

    // 初始化
    addVisitedStyle();
    loadVisitedLinks();
    setInterval(addDownloadLinks, 1000);
})();