Cver Paper Downloader

实现知识星球内的论文分享一键跳转,仅适配Cver,需要科学上网

目前為 2024-12-19 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Cver Paper Downloader
// @version      0.2
// @description  实现知识星球内的论文分享一键跳转,仅适配Cver,需要科学上网
// @author       Curtains
// @match        *://*.zsxq.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zsxq.com
// @grant        none
// @license MIT
// @namespace https://greasyfork.org/users/1224766
// ==/UserScript==

(function() {
    'use strict';

    // 创建按钮元素
    var button = document.createElement('button');
    button.id = 'transparentButton';
    button.innerText = '跳转paper';

    // 添加样式
    button.style.position = 'fixed';
    button.style.left = '50px'; // 调整按钮左边距,使其位于最左侧
    button.style.top = '50%'; // 使按钮垂直居中
    button.style.transform = 'translateY(-50%)';
    button.style.backgroundColor = 'rgba(0, 0, 0, 0.5)'; // 半透明背景颜色
    button.style.color = 'white'; // 文本颜色
    button.style.padding = '10px 20px'; // 调整按钮内边距
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';

    var currentUrl = window.location.href; // 检查当前页面的URL

    // 判断是否未登录
    function isNotLoggedIn() {
        let allSpans = document.querySelectorAll('span'); // 获取所有 span 元素
        for (let span of allSpans) {
            if (span.textContent.includes('星球禁止搜索和分享')) {
                return true; // 找到目标文本
            }
        }
        return false; // 未找到目标文本
    }

    // 使用 MutationObserver 监听 DOM 的变化
    function waitForElement(selector, callback) {
        const observer = new MutationObserver(() => {
            const element = document.querySelector(selector);
            if (element) {
                observer.disconnect(); // 停止观察
                callback(element);
            }
        });

        observer.observe(document.body, { childList: true, subtree: true });
    }

    // 检测登录状态
    waitForElement('span', () => {
        if (isNotLoggedIn()) {
            button.innerText = '需登陆后查看,点击跳转';
            button.addEventListener('click', function() {
                window.open('https://wx.zsxq.com/login', '_blank', 'noopener,noreferrer');
                setTimeout(function() {
                    location.reload(); // 20s后自动刷新当前页面
                }, 20000); // 60000ms = 1分钟
            });

        } else if (currentUrl.startsWith('https://wx.zsxq.com/topic/')) {
            // 在特定页面启用按钮的点击功能
            button.addEventListener('click', function() {

                // 找到类名为file-name的div元素
                var fileNameDiv = document.querySelector('.file-name');

                // 检查是否找到元素
                if (fileNameDiv) {
                    var fileNameContent = fileNameDiv.textContent || fileNameDiv.innerText;
                    var index = fileNameContent.indexOf('】');
                    if (index !== -1) {
                        fileNameContent = fileNameContent.substring(index + 1).trim();
                    }
                    fileNameContent = fileNameContent.replace(/\.pdf/i, '');

                    var dummyElement = document.createElement('textarea');

                    document.body.appendChild(dummyElement);
                    dummyElement.value = fileNameContent;
                    dummyElement.select();
                    document.execCommand('copy');
                    document.body.removeChild(dummyElement);

                    // 更新按钮文本并进行跳转尝试
                    button.innerText = '文件名已复制,正在尝试跳转';

                    setTimeout(function() {
                        var newWindow = window.open('https://duckduckgo.com/?q=!ducky+' +encodeURIComponent(fileNameContent), '_blank', 'noopener,noreferrer');
                    }, 1000); // 1秒后进行跳转尝试
                } else {
                    button.innerText = '未找到元素,可能是不支持的网页';
                }
            });
        } else {
            button.style.display = 'none';
        }

        // 将按钮添加到页面
        document.body.appendChild(button);
    });
})();