Greasy Fork

Greasy Fork is available in English.

书籍快捷信息查询

书籍快捷信息查询(基于孔夫子旧书网快速显示掌阅书籍信息)

目前为 2024-03-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         书籍快捷信息查询
// @namespace    http://tampermonkey.net/
// @version      v2024.03.28.1411
// @description  书籍快捷信息查询(基于孔夫子旧书网快速显示掌阅书籍信息)
// @author       tangyujun
// @match        https://www.ireader.com.cn/index.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=ireader.com.cn
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function isAllWhitespace(str) {
        return /^\s*$/.test(str);
    }

    // Function to create the styled green box element with the hovered element's text
    function createGreenBox(hoveredElement) {
        const box = document.createElement('div');
        box.classList.add('styled-green-box');
        box.style.backgroundColor = 'rgba(0, 128, 0, 0.7)';
        box.style.position = 'fixed';
        box.style.pointerEvents = 'none'; // Set to none to allow mouse events to pass through
        box.style.border = '1px solid #8FBC8F';
        box.style.borderRadius = '5px';
        box.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.1)';
        box.style.padding = '8px 15px';
        box.style.color = 'white';
        box.style.fontSize = '14px';
        box.style.zIndex = '9999';
        box.style.width = '800px'; // Set the width to 100% of the viewport
        box.style.wordWrap = 'break-word';

        const header = document.createElement('h3');
        header.textContent = hoveredElement.textContent.replace(/[\s\t\n\r]+/g, '');

        // 创建iframe元素
        const iframe = document.createElement('iframe');
        iframe.src = `https://search.kongfz.com/item_result/?status=1&key=${encodeURIComponent(hoveredElement.textContent.replace(/[\s\t\n\r]+/g, '').replace("(", " ").replace(")", " ").replace("(", " ").replace(")", " ").replace(":", " ").replace(":", " "))}`;
        iframe.style.width = '800px'; // Set the iframe width to 100%
        iframe.style.border = 'none'; // Remove the iframe default border
        iframe.style.height = '600px'; // Set the iframe height to 100%
        iframe.style.boxSizing = 'border-box'; // Ensure the iframe respects the border and padding

        // 将iframe添加到box中
        box.appendChild(header);
        box.appendChild(iframe);
        document.body.appendChild(box);
        return box;
    }
    // Function to remove the green box if it exists
    function removeGreenBox(greenBox) {
        if (greenBox && document.body.contains(greenBox)) {
            document.body.removeChild(greenBox);
        }
    }

    // Event listener for mouseover on anchor tags
    document.addEventListener('mouseover', function(e) {
        let hoveredElement;
        if (e.target.tagName === 'A') {
            hoveredElement = e.target;
        }else if(e.target.parentNode.tagName === 'A'){
            hoveredElement = e.target.parentNode;
        }
        if(hoveredElement && !isAllWhitespace(hoveredElement.textContent) && hoveredElement.parentNode && ((hoveredElement.parentNode.classList.contains("bookMess") || hoveredElement.parentNode.classList.contains("bookName"))||(hoveredElement.parentNode.parentNode && hoveredElement.parentNode.parentNode.classList.contains("bookMation")))){
            console.log(hoveredElement.textContent.replace(/[\s\t\n\r]+/g, '').replace("(", " ").replace(")", " ").replace("(", " ").replace(")", " ").replace(":", " ").replace(":", " "));
            let greenBox = document.querySelector('.styled-green-box');
            let mouseMoveHandler = function(mouseEvent) {
                updateGreenBoxPosition(greenBox, mouseEvent.clientX, mouseEvent.clientY);
            };
            let mouseOutHandler = function() {
                removeGreenBox(greenBox);
                document.removeEventListener('mousemove', mouseMoveHandler);
                document.removeEventListener('mouseout', mouseOutHandler);
            };

            // Create the box if it doesn't exist
            if (!greenBox) {
                greenBox = createGreenBox(hoveredElement);
            }

            document.addEventListener('mousemove', mouseMoveHandler);
            document.addEventListener('mouseout', mouseOutHandler);
        }
    }, { passive: true });

    // Function to update the position of the styled green box
    function updateGreenBoxPosition(box, x, y) {
        const boxWidth = box.offsetWidth;
        const boxHeight = box.offsetHeight;
        box.style.left = `${x + 5}px`; // Add a small offset to the right
        box.style.top = `${y + 5}px`; // Add a small offset upwards and adjust for the box height
    }

    let searchForm = document.querySelector('#search_form > input[type=text]');
    if(searchForm && !isAllWhitespace(searchForm.value)){
        const box = document.createElement('div');
        box.style.backgroundColor = 'rgba(250, 250, 250, 0.7)';
        box.style.border = '1px solid #8FBC8F';
        box.style.borderRadius = '5px';
        box.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.1)';
        box.style.padding = '8px 15px';
        box.style.color = 'white';
        box.style.width = '100%'; // Set the width to 100% of the viewport
        box.style.height = '600px'; // Set the width to 100% of the viewport
        box.style.display = 'flex';
        box.style.justifyContent = 'center';

        const iframe = document.createElement('iframe');
        iframe.src = `https://search.kongfz.com/item_result/?status=1&key=${encodeURIComponent(searchForm.value)}`;
        iframe.style.width = '934px'; // Set the iframe width to 100%
        iframe.style.border = 'none'; // Remove the iframe default border
        iframe.style.height = '600px'; // Set the iframe height to 100%
        iframe.style.boxSizing = 'border-box';
        let searchResultBox = document.querySelector('body > div.conLayout.cf.search.rConlNav');
        box.appendChild(iframe);
        if(searchResultBox){
            searchResultBox.insertAdjacentElement('afterend', box);
        }
    }

    // Style for the styled green box
    const style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = `
        .styled-green-box {
            border-radius: 5px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            background-color: rgba(0, 128, 0, 0.7);
            color: white;
            font-size: 14px;
            padding: 8px 15px;
            position: fixed;
            pointer-events: auto;
            max-width: 100%;
            word-wrap: break-word;
            z-index: 9999;
        }
    `;
    document.head.appendChild(style);
})();