没忍住给论坛写了首页展示帖子摘要的脚本

V hahahaha (UID: 2041) 9月前

4832 22

看见有大佬写了屏蔽脚本,我也没忍住,写了个访问大佬论坛首页时,直接显示帖子详情内容的脚本,这样可有效防止标题党,不感兴趣的内容可以不必点击进去了。

和置顶帖那位写脚本的大佬一样,直接将代码复制到油猴里启用即可。

 

注意:我设置了最长只显示350px高度的详情,防止某个详情页内容过多导致占太多空间。

 

 

 

// ==UserScript==
// @name     dalao.net单页显示
// @version  1
// @grant    none
// @description  首页直接展示信息,无需点进详情页
// @author       Damon
// @match       *://dalao.net/*
// @match       *://www.dalao.net/*
// ==/UserScript==


(function() {
   if (window.location.href.indexOf("dalao.net") != -1){
   function fetchData(url,liElement) {
        fetch(url).then(response =>{
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.text()
        }).then(data =>{
            const parser = new DOMParser();
            const doc = parser.parseFromString(data, 'text/html');
            const element = doc.getElementById('pangu');
            if (element) {
                const innerHTML = element.innerHTML;
                var newElement = document.createElement('div');
                var subElement = document.createElement('div');
                subElement.innerHTML = innerHTML; // 设置新元素节点的内容
                newElement.appendChild(subElement);
                // 设置div元素的样式
                // newElement.style.border = "5px solid black";
                liElement.setAttribute("style","border-bottom:1px dashed #7b8adf;border-top:1px solid #7b8adf;border-left:1px solid #7b8adf;border-right:1px solid #7b8adf;padding:5px;border-radius:5px 5px 0px 0px");
                newElement.setAttribute("style","border-bottom:1px solid #7b8adf;border-left:1px solid #7b8adf;border-right:1px solid #7b8adf;margin-bottom:20px;padding:5px;max-height:350px;overflow:hidden;border-radius:0px 0px 5px 5px");
                liElement.insertAdjacentElement("afterend", newElement)

            } else {
                console.log('Element with id "pangu" not found.')
            }
        }).
        catch(error =>{
            console.error('Error:', error)
        })
    }

    var liElements = document.querySelectorAll('li.media.thread.tap');
    // document.head.insertAdjacentHTML("beforeend", csss); // 将HTML代码追加到目标节点的结尾
    liElements.forEach(function(liElement) {
        var href = liElement.getAttribute('data-href');
        var url = "https://dalao.net/" + href;
        console.log(url);
        fetchData(url,liElement);
    });
     
   }

})();
TG:@gohahaha
已有评论 (22)
  • V wangqian (UID: 2425) @Ta
    9月前 (随便逛逛)
    1

    好东西!谢谢分享!

  • V hahahaha (UID: 2041) @Ta
    9月前 (TG:@gohahaha )
    2
    wangqian 好东西!谢谢分享!

    我发现这样看论坛真是太爽了,相当于首页直接看到每条的摘要了。

  • V wangqian (UID: 2425) @Ta
    9月前 (随便逛逛)
    3
    hahahaha 我发现这样看论坛真是太爽了,相当于首页直接看到每条的摘要了。

    我按照你的思路,也做了一个,永久关闭帖子的按钮,不想看的内容,按关闭以后,永久消失。

    代码如下:

    // ==UserScript==
    // @name     dalao.net增加关闭按钮
    // @version  1
    // @grant    none
    // @description  点击关闭后,帖子永远消失
    // @author       Damon
    // @match       *://dalao.net/*
    // @match       *://www.dalao.net/*
    // ==/UserScript==
    
    (function() {
        function fetchData(url, liElement) {
        }
    
        function addCloseButton(liElement, postId) {
            var closeButton = document.createElement('span');
            closeButton.textContent = '关闭';
            closeButton.style.color = 'red';
            closeButton.style.marginLeft = '10px';
            closeButton.style.cursor = 'pointer';
            closeButton.addEventListener('click', function(event) {
                event.stopPropagation();
                liElement.style.display = 'none'; // 隐藏帖子
                markPostAsClosed(postId); // 记录帖子ID,永久屏蔽
            });
            liElement.appendChild(closeButton);
        }
    
        function markPostAsClosed(postId) {
            var closedPosts = JSON.parse(localStorage.getItem('closedPosts')) || [];
            if (!closedPosts.includes(postId)) {
                closedPosts.push(postId);
                localStorage.setItem('closedPosts', JSON.stringify(closedPosts));
            }
        }
    
        function isPostClosed(postId) {
            var closedPosts = JSON.parse(localStorage.getItem('closedPosts')) || [];
            return closedPosts.includes(postId);
        }
    
        if (window.location.href.indexOf('dalao.net') !== -1) {
            var liElements = document.querySelectorAll('li.media.thread.tap');
            liElements.forEach(function(liElement) {
                var postId = liElement.getAttribute('data-href');
                var url = window.location.href + postId;
                console.log(url);
                fetchData(url, liElement);
                addCloseButton(liElement, postId);
                if (isPostClosed(postId)) {
                    liElement.style.display = 'none'; // 隐藏已关闭的帖子
                }
            });
        }
    
        // 在页面加载完成后,再次检查已关闭的帖子并隐藏
        window.addEventListener('load', function() {
            var liElements = document.querySelectorAll('li.media.thread.tap');
            liElements.forEach(function(liElement) {
                var postId = liElement.getAttribute('data-href');
                if (isPostClosed(postId)) {
                    liElement.style.display = 'none'; // 隐藏已关闭的帖子
                }
            });
        });
    })();
    
  • V hahahaha (UID: 2041) @Ta
    9月前 (TG:@gohahaha )
    4
    wangqian 我按照你的思路,也做了一个,永久关闭帖子的按钮,不想看的内容,按关闭以后,永久消失。 代码如下: // ==UserScript== // @name dalao.net增加关闭按钮 ...

    哈哈哈哈哈厉害厉害,还想到了用存储,代码写的也规整。

  • V wangqian (UID: 2425) @Ta
    9月前 (随便逛逛)
    5
    米不米 哈哈哈哈哈厉害厉害,还想到了用存储,代码写的也规整。

    你的也很厉害,直击网站交互的痛点,大大提升了观帖体验,效果很好!感觉进入了大佬论坛2.0,真好还要向你多多学习才行!你是个很不错的产品经理!

  • V happy (UID: 2144) @Ta
    9月前 (域名司短域名:sell.ym.si)
    6

    都是大佬👍

  • V Y (UID: 680) 设计达人 @Ta
    9月前
    7

    这个  很好用诶

  • 超哥 (UID: 1784) @Ta
    9月前 (只想默默当个普通人。)
    8

    赶紧讨论域名,你们这些我看不懂

  • yunfan (UID: 59) @Ta
    9月前 (http://letsgo.fun)
    9

    这个论坛  真是藏龙卧虎啊 

  • V Y (UID: 680) 设计达人 @Ta
    9月前
    10
                    liElement.setAttribute("style","margin-bottom: 0;border-bottom:1px dashed #009a61;border-top:2px solid #009a61;border-left:2px solid #009a61;border-right:2px solid #009a61;padding:5px;border-radius:5px 5px 0px 0px");
                    newElement.setAttribute("style","border-bottom:2px solid #009a61;border-left:2px solid #009a61;border-right:2px solid #009a61;margin-bottom:25px;padding:5px;max-height:350px;overflow:hidden;border-radius:0px 0px 5px 5px");

    美化了一下

  • V hahahaha (UID: 2041) @Ta
    9月前 (TG:@gohahaha )
    11
    Y liElement.setAttribute("style","margin-bottom: 0;border-bottom:1px da ...

    很棒很棒,圆角看起来就是好看

  • V hahahaha (UID: 2041) @Ta
    9月前 (TG:@gohahaha )
    12
    Y 这个 很好用诶

    哈哈哈我感觉也挺好用的~

  • 逍遥 (UID: 1287) @Ta
    9月前
    13

    支持  这个比较棒   

  • V Y (UID: 680) 设计达人 @Ta
    9月前
    14
    米不米 哈哈哈我感觉也挺好用的~

    好用的

  • 逍遥 (UID: 1287) @Ta
    9月前
    15

    能不能改一下    让discuz论坛也能用?现在很多论坛都是discuz的

  • Web (UID: 798) @Ta
    9月前
    16

    好耶!

  • V Y (UID: 680) 设计达人 @Ta
    9月前
    17

    真的好喜欢这个功能

  • V hahahaha (UID: 2041) @Ta
    9月前 (TG:@gohahaha )
    18
    Y 真的好喜欢这个功能

    哈哈哈哈哈,喜欢就好,不然点进去点出来太麻烦了

  • V hahahaha (UID: 2041) @Ta
    9月前 (TG:@gohahaha )
    19

    改天Nodeseek论坛我也搞一下

  • V Y (UID: 680) 设计达人 @Ta
    9月前
    20
    米不米 哈哈哈哈哈,喜欢就好,不然点进去点出来太麻烦了

    就是很方便

  • 番茄丁 (UID: 111) @Ta
    4月前 (fqd.net)
    21

    Y哥美化2.1的基础上,进行了优化2.2,减淡边框颜色,增加标题背景颜色,单个主题贴子区分更明显,整体更清爽~

    liElement.setAttribute("style","margin-bottom: 0;border-bottom:1px dashed #009a6170;border-top:1px solid #009a6170;border-left:1px solid #009a6170;border-right:1px solid #009a6170;padding:5px;border-radius:5px 5px 0px 0px;background-color: #009a6112;");
    newElement.setAttribute("style","border-bottom:1px solid #009a6170;border-left:1px solid #009a6170;border-right:1px solid #009a6170;margin-bottom:25px;padding:5px;max-height:350px;overflow:hidden;border-radius:0px 0px 5px 5px");
  • V Y (UID: 680) 设计达人 @Ta
    3月前
    22
    番茄丁 在Y哥美化2.1的基础上,进行了优化2.2,减淡边框颜色,增加标题背景颜色,单个主题贴子区分更明显,整体更清爽~ liElement.setAttribute("style",& ...

    so  cool  ~

    • 大佬论坛
      23
      登录后才可回帖  登录 注册

本站同款香港KC服务器 酷盾安全联盟 智能优化防护 ROOVPS 便宜服务器 盾云SCDN 月付只需1元起