修改百度背景颜色并显示微博热搜

添加一个按钮,点击按钮可以随机改变百度首页的背景颜色,并在下次进入页面时自动应用上次设置的颜色。同时,在页面的右上角显示微博的热搜内容。

目前为 2024-02-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         修改百度背景颜色并显示微博热搜
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  添加一个按钮,点击按钮可以随机改变百度首页的背景颜色,并在下次进入页面时自动应用上次设置的颜色。同时,在页面的右上角显示微博的热搜内容。
// @author       您的姓名
// @match        https://www.baidu.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
  'use strict';

  // 创建一个按钮元素
  var button = document.createElement('button');
  button.innerText = '变色';

  // 设置按钮样式
  button.style.position = 'fixed';
  button.style.top = '10px';
  button.style.right = '10px';
  button.style.zIndex = '9999';

  // 将按钮添加到页面中
  document.body.appendChild(button);

  // 创建一个容器元素用于显示微博热搜
  var container = document.createElement('div');
  container.style.position = 'fixed';
  container.style.top = '60px';
  container.style.right = '10px';
  container.style.width = '300px';
  container.style.height = '300px';
  container.style.padding = '10px';
  container.style.backgroundColor = '#fff';
  container.style.border = '1px solid #ccc';
  container.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.1)';
  container.style.zIndex = '9999';
  container.style.fontSize = '14px';
  container.style.overflow = 'auto';
  container.style.textAlign = 'left';

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

  // 定义随机生成颜色的函数
  function getRandomColor() {
    var color = '#' + Math.floor(Math.random() * 16777215).toString(16);
    color += '33';
    return color;
  }

  // 从本地存储中获取上次保存的颜色
  var lastColor = GM_getValue('baiduBackgroundColor');

  // 如果有上次保存的颜色,则应用到页面的背景颜色
  if (lastColor) {
    document.body.style.backgroundColor = lastColor;
    document.getElementById('s_top_wrap').style.backgroundColor = lastColor;
      document.getElementById('bottom_layer').style.backgroundColor = lastColor;
  }

  // 点击按钮时改变页面背景颜色,并保存到本地存储中
  button.addEventListener('click', function() {
    var color = getRandomColor();
    document.body.style.backgroundColor = color;
    document.getElementById('s_top_wrap').style.backgroundColor = color;
    GM_setValue('baiduBackgroundColor', color);
  });

  // 使用GM_xmlhttpRequest获取微博热搜数据
  GM_xmlhttpRequest({
    method: 'GET',
    url: 'https://api.wetab.link/api/hotsearch/list?type=weibo',
    headers: {
      'Accept': '*/*',
      'Accept-Encoding': 'gzip, deflate, br',
      'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
      'Connection': 'keep-alive',
      'Host': 'api.wetab.link',
      'I-App': 'hitab',
      'I-Branch': 'zh',
      'I-Lang': 'zh-CN',
      'I-Platform': 'chrome',
      'I-Version': '1.6.4',
      'Sec-Ch-Ua': '"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"',
      'Sec-Ch-Ua-Mobile': '?0',
      'Sec-Ch-Ua-Platform': '"Windows"',
      'Sec-Fetch-Dest': 'empty',
      'Sec-Fetch-Mode': 'cors',
      'Sec-Fetch-Site': 'none',
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'
    },
    onload: function(response) {
      if (response.status === 200) {
        var data = JSON.parse(response.responseText);
        // 解析热搜数据
        var hotTopics = data.data.list;

        // 将热搜内容插入到容器中
        hotTopics.forEach((topic, index) => {
          var topicElement = document.createElement('a');
          topicElement.href = topic.url;
          topicElement.target = '_blank';
          topicElement.innerText = (index + 1) + '. ' + topic.title;
          topicElement.style.display = 'block';
          container.appendChild(topicElement);
        });
      } else {
        console.error('Failed to fetch hot topics:', response);
      }
    }
  });
})();
document.addEventListener('mouseup', function(event) {
  var selectedText = window.getSelection().toString();
  if (selectedText) {
    copyToClipboard(selectedText);
    GM_notification({
      text: '已将选中文本复制到剪贴板',
      timeout: 2000,
      title: '复制成功'
    });
  }
});

function copyToClipboard(text) {
  var textarea = document.createElement('textarea');
  textarea.value = text;
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand('copy');
  document.body.removeChild(textarea);
}
document.addEventListener('contextmenu', function(event) {
  // 阻止默认右键菜单
  event.preventDefault();

  // 获取当前鼠标指向的元素
  const target = event.target;

  // 检查是否为图片元素
  if (target.tagName === 'IMG') {
    const imageUrl = target.src;

    // 创建下载链接
    const downloadLink = document.createElement('a');
    downloadLink.href = imageUrl;
    downloadLink.download = 'image.jpg'; // 可以根据需要更改文件名
    downloadLink.click();
  }
});

QingJ © 2025

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