Greasy Fork is available in English.

列举微博热搜有助于做搜索奖励任务

将微博热搜前五十个关键词显示在页面右侧

// ==UserScript==
// @name         列举微博热搜有助于做搜索奖励任务
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  将微博热搜前五十个关键词显示在页面右侧
// @author       You
// @match        *://*.weibo.com/top/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 等待页面加载完成
    window.addEventListener('load', function() {
        // 检查是否找到了微博热搜列表容器
        const hotSearchContainer = document.querySelector('#pl_top_realtimehot');
        if (!hotSearchContainer) {
            console.error('未找到微博热搜列表容器');
            return;
        }

        // 创建一个新的div作为热搜文本框的容器
        const container = document.createElement('div');
        container.id = 'weibo-hot-search-container';
        container.style.position = 'fixed';
        container.style.right = '20px';
        container.style.top = '100px';
        container.style.width = '300px';
        container.style.maxHeight = '80vh'; // 限制最大高度为视口高度的80%
        container.style.overflowY = 'auto'; // 当内容超出时显示滚动条
        container.style.backgroundColor = 'white';
        container.style.border = '1px solid #ccc';
        container.style.padding = '10px';
        container.style.zIndex = '9999';
        // 设定热搜关键词的字体大小
        container.style.fontSize = '18px'; // 例如,设置字体大小为18px

        // 将容器添加到body中
        document.body.appendChild(container);

        // 初始化序号
        let index = 1;

        // 获取从第二个tr开始的tr元素
        const hotSearchTrs = hotSearchContainer.querySelectorAll('table > tbody > tr:nth-child(n+2)');

        // 遍历tr元素并查找td.td-02下的a标签,但只获取前五十个
        hotSearchTrs.forEach((tr, i) => {
            if (i >= 50) return; // 只处理前50个热搜

            const keywordElement = tr.querySelector('td.td-02 > a');
            if (keywordElement) {
                const keyword = keywordElement.textContent.trim();

                // 创建一个新的p元素来包裹序号和关键词
                const p = document.createElement('p');

                // 添加序号
                p.textContent = `${index++}. ${keyword}`;

                // 将p元素添加到容器中
                container.appendChild(p);

                // 添加换行(如果需要的话)
                // 注意:由于p元素默认是块级元素,会自动换行,所以这里不需要br

                // 也可以在这里进一步设置p元素的样式,例如调整行高
                // p.style.lineHeight = '1.5';
            }
        });
    });
})();