Greasy Fork

Greasy Fork is available in English.

掌阅小助手

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

当前为 2024-04-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

(function() {
    'use strict';

    // 获取所有的<p class="bodytext">标签
    var paragraphs = document.querySelectorAll('p.bodytext');
    // 遍历所有的<p>标签并设置透明度
    paragraphs.forEach(function(paragraph) {
        paragraph.style.opacity = 0.1;
        paragraph.addEventListener('mouseover', function() {
            this.style.opacity = 1;
        });
        paragraph.addEventListener('mouseout', function() {
            this.style.opacity = 0.1;
        });
    });

    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');
        const boxWidth = 600;
        const boxHeight = 600;
        const left = 220;
        const top = 320;

        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.height = `30px`; // Set the width to 100% of the viewport
        box.style.width = `${boxWidth}px`; // Set the width to 100% of the viewport
        box.style.wordWrap = 'break-word';
        box.style.overflow = 'hidden';

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


        const container = document.createElement('div');
        container.style.position = 'relative';
        container.style.wordWrap = 'break-word';
        container.style.overflow = 'hidden';
        container.style.height = `1px`; // Set the width to 100% of the viewport
        container.style.width = `${boxWidth}px`; // Set the width to 100% of the viewport

        // 创建iframe元素
        const iframe = document.createElement('iframe');
        iframe.src = `https://search.kongfz.com/product_result/?key=${encodeURIComponent(hoveredElement.textContent.replace(/[\s\t\n\r]+/g, ''))}`;

        iframe.style.visibility = 'hidden';
        iframe.style.border = 'none'; // Remove the iframe default border
        iframe.onload = function() {
            box.style.height = `${boxHeight+30}px`; // Set the width to 100% of the viewport
            container.style.height = `${boxHeight}px`; // Set the width to 100% of the viewport
            iframe.style.visibility = 'visible';
            iframe.style.width = `1200px`;
            iframe.style.height = `${top+boxHeight}px`;
            iframe.style.left = `-${left}px`;
            iframe.style.top = `-${top}px`;
            iframe.style.position = 'absolute';
            iframe.style.boxSizing = 'border-box'; // Ensure the iframe respects the border and padding
        };
        container.appendChild(iframe);

        // 将iframe添加到box中
        box.appendChild(header);
        box.appendChild(container);
        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';
        box.style.overflow = 'hidden';

        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 = '800px'; // Set the iframe height to 100%
        iframe.style.top = '-200px';
        iframe.style.boxSizing = 'border-box';
        iframe.style.position = 'relative';
        if (iframe.attachEvent) {
            iframe.attachEvent("onload", function() {
                handleKongfzSearch(iframe)
            });
        } else {
            iframe.onload = function() {
                handleKongfzSearch(iframe)
            };
        }
        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);
})();