Greasy Fork

Greasy Fork is available in English.

今日头条舆情

今日头条舆情抓取

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         今日头条舆情
// @namespace    http://tampermonkey.net/
// @version      0.0.2
// @description  今日头条舆情抓取
// @author       daben
// @match        https://www.toutiao.com/w/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=toutiao.com
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  function getArticleNum(domSelector, defaultText) {
    var el = document.querySelector(domSelector);
    var num = el ? el.textContent.trim() : 0;
    return /^\d+$/.test(num) ? num : defaultText;
  }
  function getCopyNum(numText) {
    return /^\d+$/.test(numText) ? numText : (parseInt(numText) || 0)
  }

  window.addEventListener('load', function() {
    setTimeout(() => {
      // 获取标题,这里假设标题元素是h1标签,可根据实际调整选择器
      var titleElement = document.querySelector('h1');
      var title = titleElement? titleElement.textContent.trim() : '未获取到标题';
      // 获取作者,假设作者信息在class为"author"的元素内,需按实际修改选择器
      var authorElement = document.querySelector('.desc .name');
      var author = authorElement? authorElement.textContent.trim() : '未获取到作者';
      // 获取发布时间,假设在class为"publish-time"的元素内,按需修改选择器
      var timeElement = document.querySelector('.abstract .time');
      var time = timeElement? timeElement.textContent.trim() : '未获取到发布时间';
      // 获取文章内容所在元素,假设文章内容在id为"article-content"的元素内,按实际调整
      var contentElement = document.querySelector('article');
      var wordCount = contentElement? contentElement.textContent.trim().length : 0;
      // // 阅读数
      // var readnum = getArticleNum('.read-num em', '未获取到阅读数')
      // 点赞数
      var likeNum = getArticleNum('.detail-like span', '未获取到点赞数')
      // 评论数
      var commentNum = getArticleNum('.detail-interaction-comment span', '未获取到评论数')
      // 收藏数
      var collectNum = getArticleNum('.detail-interaction-collect span', '未获取到收藏数')
      // // 分享数
      // var shareNum = getArticleNum('.share-c .count', '未获取到分享数')

      // 创建一个用于显示信息的div元素作为浮窗
      var infoDiv = document.createElement('div');
      infoDiv.style.position = 'fixed';
      infoDiv.style.top = '0';
      infoDiv.style.right = '0';
      infoDiv.style.backgroundColor = 'rgba(255, 255, 255, 0.8)';
      infoDiv.style.padding = '10px';
      infoDiv.style.border = '1px solid gray';
      infoDiv.style.zIndex = '9999';

      // 创建关闭按钮元素
      var closeButton = document.createElement('span');
      closeButton.id = '__close_btn__';
      closeButton.textContent = '×';
      closeButton.style.cursor = 'pointer';
      closeButton.style.float = 'right';
      closeButton.style.fontSize = '18px';
      closeButton.style.color = 'red';

      // 创建复制按钮元素
      var copyButton = document.createElement('span');
      copyButton.id = '__copy__';
      copyButton.textContent = '复制';
      copyButton.style.cursor = 'pointer';
      copyButton.style.float = 'right';
      copyButton.style.marginRight = '5px';
      copyButton.style.fontSize = '18px';
      copyButton.style.color = 'blue';

      // 先将关闭按钮添加到浮窗中
      infoDiv.appendChild(closeButton);
      // 再将复制按钮添加到浮窗中
      infoDiv.appendChild(copyButton);

      // 将信息拼接成字符串并设置到浮窗的innerHTML中
      var infoText = `标题:${title}<br>作者:${author}<br>发布时间:${time}<br>字数:${wordCount}<br>点赞数:${likeNum}<br>评论数:${commentNum}<br>收藏数:${collectNum}`;
      infoDiv.innerHTML += infoText;

      // 将浮窗添加到页面的body元素中
      document.body.appendChild(infoDiv);

      document.querySelector('#__close_btn__').addEventListener('click', function() {
        infoDiv.parentNode.removeChild(infoDiv);
      });

      document.querySelector('#__copy__').onclick = function() {
        var copyText = `${time.replace(/[年月]/g, '/').replace(/日/, '')}\t今日头条\t${author}\t${title}\t${window.location.href}\t点赞${getCopyNum(likeNum)} 评论${getCopyNum(commentNum)} 收藏${getCopyNum(collectNum)}\t${wordCount}`;
        navigator.clipboard.writeText(copyText).then(function() {
          console.log('已复制到剪贴板');
        }).catch(function(err) {
          console.error('复制失败:', err);
        });
      };
    }, 3 * 1000)
  });
})();