// ==UserScript== // @name bgm相关回复跳转 // @version 0.3 // @description 在web自动识别贴贴过的帖子,提供跳转。 // @match https://bgm.tv/ep/* // @match https://bangumi.tv/ep/* // @match https://chii.in/ep/* // @grant none // @license MIT // @namespace bgm_jump_related_post // @downloadURL none // ==/UserScript== // 几个todo,目前自己没这个需求,暂时简化。如果有人需要可以说一声: // - [ ] 除了贴贴也支持回复的跳转 // - [ ] 暂时只支持番剧吐槽页面,主要是觉得别的页面也没必要。 // - [ ] 暂时只处理每层回复的一楼,这个主要是暂时懒得做先简化一下。也就是不支持比如说第二楼的所有回复贴贴。 (function () { 'use strict'; const selfUserId = $('.avatar').attr('href').split('/').pop(); // 先完成遍历处理第一层的逻辑 const replyList = $('#comment_list>div'); // 得到所有首层回复 let relatedPostIds = []; // 筛选最终跟当前用户相关的id // replyList.slice(0, 2).each(function () { // 调试场景只看前三个帖子 replyList.each(function () { // 逐个过滤 if (checkReplyElementRelated(selfUserId, $(this))) { relatedPostIds.push($(this).attr('id')); } }); // console.log(relatedPostIds); const component = createJumpComponent(relatedPostIds); const columnEPB = $('#columnEpB') columnEPB.append(component); })(); // 检查当前回复元素的「贴贴」里边是否包含当前用户id function checkReplyElementRelated(userId, element) { // 先在这个元素内继续选择 `a.item.selected` // const likesGridItem = element.find('a.item.selected'); const innerDiv = element.children().eq(2) // 这个元素里的 title 包含所有 贴贴元素的 HTML 文本,每个 `` 就是一种类型的贴贴 const likesGridItemAs = innerDiv.find('.reply_content').children().eq(1).children(); if (likesGridItemAs.length === 0) { return false; } let found = false; likesGridItemAs.each(function () { const title = $(this).attr('title'); if (checkUserIdInTitle(userId, title)) { found = true; return false; // break loop // ! 这里才是坑,里边有个回调函数,return true 会直接返回到这个回调函数,而不是外层的函数 } }); return found; } function checkUserIdInTitle(userId, title) { const regex = /\/user\/(\w+)/g; let match; while ((match = regex.exec(title)) !== null) { if (match[1] === userId) { return true; } } return false; } function createJumpComponent(postIds) { // Basic CSS for styling the component const styles = ` `; // HTML for the component const linksHtml = postIds.map(link => `
  • ${link}
  • `).join(''); const componentHtml = `

    贴贴过的回复跳转

    `; return styles + componentHtml; }