期刊等级查询助手

在网页上添加一个浮动球,用于查询期刊等级信息

目前為 2025-01-02 提交的版本,檢視 最新版本

// ==UserScript==
// @name         期刊等级查询助手
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  在网页上添加一个浮动球,用于查询期刊等级信息
// @author       Your name
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // 添加样式
    GM_addStyle(`
        #journal-query-ball {
            position: fixed;
            right: 20px;
            bottom: 20px;
            width: 50px;
            height: 50px;
            background: #4CAF50;
            border-radius: 50%;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            z-index: 10000;
            transition: transform 0.3s;
        }
        #journal-query-ball:hover {
            transform: scale(1.1);
        }
        #journal-query-modal {
            display: none;
            position: fixed;
            right: 80px;
            bottom: 80px;
            width: 300px;
            background: white;
            border-radius: 8px;
            padding: 15px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            z-index: 10000;
        }
        #journal-query-modal input {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        #journal-query-modal button {
            background: #4CAF50;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
        }
        #journal-query-modal button:hover {
            background: #45a049;
        }
        #query-results {
            margin-top: 10px;
            padding: 10px;
            background: #f5f5f5;
            border-radius: 4px;
            display: none;
        }
        .loading {
            text-align: center;
            margin: 10px 0;
            display: none;
        }
        .error-message {
            color: red;
            margin-top: 10px;
            display: none;
        }
    `);

    // 创建浮动球和查询模态框
    const ball = document.createElement('div');
    ball.id = 'journal-query-ball';
    ball.innerHTML = '📚';
    document.body.appendChild(ball);

    const modal = document.createElement('div');
    modal.id = 'journal-query-modal';
    modal.innerHTML = `
        <input type="text" id="journal-name" placeholder="请输入期刊名称">
        <button id="query-button">查询</button>
        <div class="loading">查询中...</div>
        <div class="error-message"></div>
        <div id="query-results"></div>
    `;
    document.body.appendChild(modal);

    // 点击球显示/隐藏模态框
    let isModalVisible = false;
    ball.addEventListener('click', () => {
        isModalVisible = !isModalVisible;
        modal.style.display = isModalVisible ? 'block' : 'none';
    });

    // 查询函数
    async function queryJournalRank(journalName) {
        const apiKey = '69e5dfcf9f6b45b7947e6c8606ef4509';
        const baseUrl = 'https://www.easyscholar.cc/open/getPublicationRank';
        const encodedName = encodeURIComponent(journalName);
        const url = `${baseUrl}?secretKey=${apiKey}&publicationName=${encodedName}`;

        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: url,
                onload: function(response) {
                    try {
                        const data = JSON.parse(response.responseText);
                        if (data.code === 200) {
                            const officialRank = data.data.officialRank;
                            const rankData = officialRank.select || officialRank.all || {};
                            
                            const result = {};
                            const indicators = {
                                "CCF等级": "ccf",
                                "SCI五年影响因子": "sciif5",
                                "SCI分区": "sci",
                                "SCI影响因子": "sciif"
                            };

                            let foundAny = false;
                            for (const [displayName, key] of Object.entries(indicators)) {
                                if (rankData[key]) {
                                    result[displayName] = rankData[key];
                                    foundAny = true;
                                }
                            }

                            if (!foundAny) {
                                reject('未找到指定的期刊等级信息');
                            } else {
                                resolve(result);
                            }
                        } else {
                            reject(data.msg || '查询失败');
                        }
                    } catch (error) {
                        reject('数据解析错误');
                    }
                },
                onerror: function(error) {
                    reject('网络请求失败');
                }
            });
        });
    }

    // 查询按钮点击事件
    document.getElementById('query-button').addEventListener('click', async () => {
        const journalName = document.getElementById('journal-name').value.trim();
        if (!journalName) {
            showError('请输入期刊名称');
            return;
        }

        const loading = document.querySelector('.loading');
        const results = document.getElementById('query-results');
        const errorMessage = document.querySelector('.error-message');

        loading.style.display = 'block';
        results.style.display = 'none';
        errorMessage.style.display = 'none';

        try {
            const data = await queryJournalRank(journalName);
            let resultHtml = '<h4>查询结果:</h4>';
            for (const [indicator, value] of Object.entries(data)) {
                resultHtml += `<p><strong>${indicator}:</strong> ${value}</p>`;
            }
            results.innerHTML = resultHtml;
            results.style.display = 'block';
        } catch (error) {
            showError(error);
        } finally {
            loading.style.display = 'none';
        }
    });

    function showError(message) {
        const errorMessage = document.querySelector('.error-message');
        errorMessage.textContent = message;
        errorMessage.style.display = 'block';
    }

    // 点击其他地方关闭模态框
    document.addEventListener('click', (event) => {
        if (isModalVisible && 
            !modal.contains(event.target) && 
            !ball.contains(event.target)) {
            modal.style.display = 'none';
            isModalVisible = false;
        }
    });
})();