展示豆瓣推荐书籍标签

提取网页内的推荐标签,放置到书籍详情中

// ==UserScript==
// @name         展示豆瓣推荐书籍标签
// @namespace    http://tampermonkey.net/
// @version      2025-03-19
// @description  提取网页内的推荐标签,放置到书籍详情中
// @author       blue-bird
// @match        https://book.douban.com/subject/*/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=douban.com
// @license MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Wait until the DOM is fully loaded
    window.addEventListener('load', function() {
        // Extract the criteria string from the DoubanAdRequest object
        const criteria = (window.DoubanAdRequest && window.DoubanAdRequest.crtr) || '';
        if (!criteria) {
            console.warn('No criteria found in DoubanAdRequest!');
            return;
        }

        // Split the criteria string into individual entries
        const entries = criteria.split('|');

        // Extract keywords (they are after the colon `:`)
        const keywords = entries.filter(entry => entry.startsWith('7:')).map(entry => {
            const parts = entry.split(':');
            return parts.length > 1 ? parts[1] : null;
        }).filter(Boolean); // Remove null/undefined entries

                // Create a toggle button
        const toggleButton = document.createElement('button');
        toggleButton.textContent = '显示/隐藏 标签';
        toggleButton.style.marginBottom = '10px';

                // Create a container for the keywords
        const keywordContainer = document.createElement('div');
        keywordContainer.style.display = 'none'; // Initially hidden

        // Create links for keywords
        keywords.forEach((keyword, index) => {
            const link = document.createElement('a');
            link.href = `https://book.douban.com/tag/${encodeURIComponent(keyword)}`; // Create the link
            link.textContent = keyword; // Set the link text
            link.target = '_blank'; // Open in a new tab
            link.style.marginRight = '10px'; // Add some space between links

            // Append the link to the container
            keywordContainer.appendChild(link);
        });

        // Add toggle functionality
        toggleButton.addEventListener('click', () => {
            if (keywordContainer.style.display === 'none') {
                keywordContainer.style.display = 'block';
                toggleButton.textContent = '隐藏 关键词';
            } else {
                keywordContainer.style.display = 'none';
                toggleButton.textContent = '显示 关键词';
            }
        });

        // Find the element with id="info"
        const infoElement = document.getElementById('info');
        if (infoElement) {
            // Append the toggle button and the keyword container to the info element
            infoElement.appendChild(toggleButton);
            infoElement.appendChild(keywordContainer);
        } else {
            console.warn('Element with id="info" not found!');
        }
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址