一秒快速复制自己获得勋章的教程_派派后花园

用户中心 游戏论坛 社区服务
发帖 回复
阅读:2 回复:1

[家长里短] 一秒快速复制自己获得勋章的教程

刷新数据 楼层直达
海笑0126

ZxID:49579737


等级: 派派版主
举报 只看楼主 使用道具 楼主   发表于: 昨天 21:05 0
前言:好久没冒泡了,这个是裴之之前出的教程一分钟快速了解自己获得;未拥有的勋章;绝版勋章位置升级版
旧教程其实不错的,为了迁就新人,所以升级了教程,改用脚本来复制自己的勋章。
感谢朋友Leon_H的帮助,把脚本写出来以及调试。



步骤:
1.点复制代码
2.篡改猴脚本里面,点加号新建脚本,然后ctrl c,ctrl v,ctrl s,就可以把复制的代码保存为脚本了。
3.脚本保存成功,帖子页面就会显示导出勋章的复制按钮。


效果图以及复制后的效果如图:





备注:复制后如不喜欢顿号把文字连一起的排版,可以顿号替换\n。txtformat正则表达式打钩,就可以分行。这小事,问题不大。


  1. // ==UserScript==
  2. // [url=/u.php?uid=23627]@name[/url]           派派ZxID导出勋章按钮(点击复制信息)
  3. // [url=/u.php?uid=23627]@name[/url]  space    http://tampermonkey.net/
  4. // [url=/u.php?uid=159887]@version[/url]        2026-08-27 20:29:50
  5. // @description  列表页+个人主页通用,支持在线/离线图标,点击复制用户名、勋章列表
  6. // @author       Leon_H
  7. // [url=/u.php?uid=561561]@match[/url]          https://www.paipai.fm/*
  8. // [url=/u.php?uid=34399]@grant[/url]          none
  9. // @run-at       document-end
  10. // ==/UserScript==
  11. (function () {
  12.     'use strict';
  13.     // 向上查找包含指定选择器的最近祖先元素内的目标
  14.     function findClosestElement(element, selector) {
  15.         let current = element.parentElement;
  16.         while (current && current !== document.body) {
  17.             const target = current.querySelector(selector);
  18.             if (target) return target;
  19.             current = current.parentElement;
  20.         }
  21.         return null;
  22.     }
  23.     // ========== 列表页功能(原有逻辑完全保留) ==========
  24.     function addExportButton(zxidP) {
  25.         // 防止重复添加
  26.         if (zxidP.previousElementSibling?.classList.contains('zxid-export-btn')) return;
  27.         // 创建导出按钮
  28.         const exportBtn = document.createElement('button');
  29.         exportBtn.textContent = '导出勋章';
  30.         exportBtn.className = 'zxid-export-btn';
  31.         // 按钮样式,放在a与p之间,垂直对齐
  32.         exportBtn.style.display = 'inline-block';
  33.         exportBtn.style.marginRight = '8px';
  34.         exportBtn.style.verticalAlign = 'middle';
  35.         exportBtn.style.padding = '2px 8px';
  36.         exportBtn.style.fontSize = '12px';
  37.         exportBtn.style.border = '1px solid #ccc';
  38.         exportBtn.style.borderRadius = '3px';
  39.         exportBtn.style.background = '#ffffff';
  40.         exportBtn.style.cursor = 'pointer';
  41.         // 点击复制功能
  42.         exportBtn.addEventListener('click', function (e) {
  43.             e.preventDefault(); // 阻止默认行为,彻底避免页面刷新
  44.             // 定位当前条目对应的三个元素
  45.             const userLink = exportBtn.previousElementSibling; // 前面的用户名a标签
  46.             const zxidParagraph = exportBtn.nextElementSibling; // 后面的ZxID的p标签
  47.             const medalsDiv = findClosestElement(zxidParagraph, '.medals-list'); // 对应用户的勋章容器
  48.             // ① 提取用户名(a标签内的文本)
  49.             const username = userLink?.textContent?.trim() || '未知用户名';
  50.             // ② 提取ZxID数字
  51.             const zxid = zxidParagraph?.textContent?.replace('ZxID:', '').trim() || '未知ID';
  52.             // ③ 提取所有勋章的title
  53.             const medalTitles = [];
  54.             if (medalsDiv) {
  55.                 const medalImgs = medalsDiv.querySelectorAll('img[title]');
  56.                 medalImgs.forEach(img => {
  57.                     const title = img.title.trim();
  58.                     if (title) medalTitles.push(title);
  59.                 });
  60.             }
  61.             const medalText = medalTitles.length ? medalTitles.join('、') : '无勋章';
  62.             // 拼接最终复制内容
  63.             const copyContent = `用户名:${username}\nZxID:${zxid}\n勋章:${medalText}`;
  64.             // 写入剪贴板 + 点击反馈
  65.             navigator.clipboard.writeText(copyContent).then(() => {
  66.                 const original = exportBtn.textContent;
  67.                 exportBtn.textContent = '已复制';
  68.                 setTimeout(() => exportBtn.textContent = original, 1200);
  69.             }).catch(() => {
  70.                 exportBtn.textContent = '复制失败';
  71.                 setTimeout(() => exportBtn.textContent = '导出', 1200);
  72.             });
  73.         });
  74.         // 插入到a标签和p标签之间(zxidP的前方)
  75.         zxidP.parentNode.insertBefore(exportBtn, zxidP);
  76.     }
  77.     function initAllZxid() {
  78.         document.querySelectorAll('p').forEach(p => {
  79.             if (p.textContent.includes('ZxID:')) {
  80.                 addExportButton(p);
  81.             }
  82.         });
  83.     }
  84.     // ========== 个人主页功能(已移除头衔) ==========
  85.     function addProfileExportButton() {
  86.         // 防止重复添加
  87.         if (document.querySelector('.profile-export-btn')) return;
  88.         // 定位页面元素
  89.         const userNameStrong = document.querySelector('strong.f14.b');
  90.         // 同时匹配在线图标online.png、离线图标stealth.png
  91.         const statusImg = document.querySelector('img[src="//www.paipai.fm/p_w_picpath/stealth.png"],img[src="//www.paipai.fm/p_w_picpath/online.png"]');
  92.         if (!userNameStrong || !statusImg) return;
  93.         // 创建导出按钮,复用原有样式类
  94.         const exportBtn = document.createElement('button');
  95.         exportBtn.textContent = '导出勋章';
  96.         exportBtn.className = 'profile-export-btn zxid-export-btn';
  97.         // 按钮样式与列表页保持统一
  98.         exportBtn.style.display = 'inline-block';
  99.         exportBtn.style.marginLeft = '8px';
  100.         exportBtn.style.marginRight = '8px';
  101.         exportBtn.style.verticalAlign = 'middle';
  102.         exportBtn.style.padding = '2px 8px';
  103.         exportBtn.style.fontSize = '12px';
  104.         exportBtn.style.border = '1px solid #ccc';
  105.         exportBtn.style.borderRadius = '3px';
  106.         exportBtn.style.background = '#ffffff';
  107.         exportBtn.style.cursor = 'pointer';
  108.         // 点击复制功能
  109.         exportBtn.addEventListener('click', function (e) {
  110.             e.preventDefault();
  111.             // ① 提取用户名(strong.f14.b 内的文本)
  112.             const username = userNameStrong.textContent.trim() || '未知用户名';
  113.             // ② 提取所有勋章的 title
  114.             const medalImgs = document.querySelectorAll('img[src*="/hack/medal/image/"]');
  115.             const medalTitles = [];
  116.             medalImgs.forEach(img => {
  117.                 const title = img.title.trim();
  118.                 if (title) medalTitles.push(title);
  119.             });
  120.             const medalText = medalTitles.length ? medalTitles.join('、') : '无勋章';
  121.             // 拼接最终复制内容(已移除头衔)
  122.             const copyContent = `用户名:${username}\n勋章:${medalText}`;
  123.             // 写入剪贴板 + 点击反馈
  124.             navigator.clipboard.writeText(copyContent).then(() => {
  125.                 const original = exportBtn.textContent;
  126.                 exportBtn.textContent = '已复制';
  127.                 setTimeout(() => exportBtn.textContent = original, 1200);
  128.             }).catch(() => {
  129.                 exportBtn.textContent = '复制失败';
  130.                 setTimeout(() => exportBtn.textContent = '导出', 1200);
  131.             });
  132.         });
  133.         // 插入到 strong 与在线/离线图标之间
  134.         statusImg.parentNode.insertBefore(exportBtn, statusImg);
  135.     }
  136.     // ========== 页面初始化 ==========
  137.     function initPage() {
  138.         initAllZxid();       // 列表页逻辑
  139.         addProfileExportButton(); // 个人主页逻辑
  140.     }
  141.     if (document.readyState === 'loading') {
  142.         document.addEventListener('DOMContentLoaded', initPage);
  143.     } else {
  144.         initPage();
  145.     }
  146.     // 监听动态加载的内容,滚动/翻页新增的ZxID也会自动加按钮
  147.     const observer = new MutationObserver(mutations => {
  148.         mutations.forEach(mutation => {
  149.             mutation.addedNodes.forEach(node => {
  150.                 if (node.nodeType !== Node.ELEMENT_NODE) return;
  151.                 // 列表页动态内容处理
  152.                 if (node.tagName === 'P' && node.textContent.includes('ZxID:')) {
  153.                     addExportButton(node);
  154.                 }
  155.                 node.querySelectorAll?.('p').forEach(p => {
  156.                     if (p.textContent.includes('ZxID:')) {
  157.                         addExportButton(p);
  158.                     }
  159.                 });
  160.                 // 个人主页动态加载时重试
  161.                 addProfileExportButton();
  162.             });
  163.         });
  164.     });
  165.     observer.observe(document.body, {
  166.         childList: true,
  167.         subtree: true
  168.     });
  169. })();



白绫薇

ZxID:10585359


等级: 热心会员
欢迎回家,长长久久
举报 只看该作者 沙发   发表于: 昨天 23:40 0
感谢教程!
发帖 回复