巴哈姆特Dx

整合圖片懸浮顯示留言、展開留言功能

安裝腳本?
作者推薦腳本

您可能也會喜歡 巴哈姆特

以使用者樣式安裝
// ==UserScript==
// @name         巴哈姆特Dx
// @namespace    https://forum.gamer.com.tw/
// @version      2025.0606
// @description  整合圖片懸浮顯示留言、展開留言功能
// @match        https://forum.gamer.com.tw/C.php?*
// @match        https://forum.gamer.com.tw/Co.php?*
// @require      https://unpkg.com/popper.js@1
// @require      https://unpkg.com/tippy.js@5
// @resource     TIPPY_LIGHT_THEME https://unpkg.com/tippy.js@5/themes/light.css
// @grant        GM_getResourceText
// @grant        GM_addStyle
// @run-at       document-end
// @license      MIT
// ==/UserScript==
(function(){'use strict';let overlay,enlargedImg,activeImg,hoverTimeout,replyMap=new Map,abortController=new AbortController;GM_addStyle(GM_getResourceText("TIPPY_LIGHT_THEME")+`.c-post.c-section__main img{image-rendering: -webkit-optimize-contrast;}`);function createOverlay(){overlay=document.createElement('div');Object.assign(overlay.style,{position:'fixed',top:0,left:0,width:'100%',height:'100%',display:'none',justifyContent:'center',alignItems:'center',zIndex:'9999',pointerEvents:'none'});document.body.appendChild(overlay);enlargedImg=document.createElement('img');Object.assign(enlargedImg.style,{maxWidth:'99vw',maxHeight:'99vh',objectFit:'contain',pointerEvents:'none'});overlay.appendChild(enlargedImg);}
function showEnlargedImage(img){if(!overlay)createOverlay();const rawSrc=img.src.split('?')[0];const viewportWidth=window.innerWidth,viewportHeight=window.innerHeight;let displayWidth=img.naturalWidth,displayHeight=img.naturalHeight;if(img.naturalWidth>viewportWidth||img.naturalHeight>viewportHeight){const imgAspectRatio=img.naturalWidth/img.naturalHeight;imgAspectRatio>viewportWidth/viewportHeight?(displayWidth=viewportWidth*0.99,displayHeight=displayWidth/imgAspectRatio):(displayHeight=viewportHeight*0.99,displayWidth=displayHeight*imgAspectRatio);}else{displayWidth=img.naturalWidth;displayHeight=img.naturalHeight;}
enlargedImg.src=rawSrc;enlargedImg.style.width=`${displayWidth}px`;enlargedImg.style.height=`${displayHeight}px`;overlay.style.display='flex';}
function hideEnlargedImage(){overlay&&(overlay.style.display='none');}
function processImage(img){if(img.dataset.processed)return;const excludePattern=/(editor\/emotion|avataruserpic)/;if(excludePattern.test(img.src))return;img.dataset.processed=true;img.style.cursor='zoom-in';if(img.src.includes('?')){const rawSrc=img.src.split('?')[0];img.dataset.originalSrc=img.src;img.src=rawSrc;}
const handler=function(e){clearTimeout(hoverTimeout);if(e.type==='mouseenter'){activeImg=img;hoverTimeout=setTimeout(()=>showEnlargedImage(img),100);}else{if(activeImg===img){hoverTimeout=setTimeout(()=>{hideEnlargedImage();activeImg=null;},100);}}};img.addEventListener('mouseenter',handler);img.addEventListener('mouseleave',handler);}
function checkForNewImages(){document.querySelectorAll('.c-post.c-section__main img:not([data-processed])').forEach(img=>{if(img.complete&&img.naturalHeight>0){processImage(img);}else{const loadHandler=()=>{processImage(img);img.removeEventListener('load',loadHandler);};img.addEventListener('load',loadHandler);}});}
function waitElement(selector){return new Promise(resolve=>{if(document.querySelector(selector))return resolve(document.querySelector(selector));const observer=new MutationObserver(()=>{if(document.querySelector(selector)){observer.disconnect();resolve(document.querySelector(selector));}});observer.observe(document.body,{childList:true,subtree:true});});}
function addReplyMouseoverListener(node){const commentEles=new Set;node.querySelectorAll('.reply-content a[href^="javascript:Forum.C.openCommentDialog"]').forEach(replyEle=>{const commentEle=replyEle.closest(".c-reply__item");if(!commentEles.has(commentEle)){commentEles.add(commentEle);addReplyTooltip(commentEle);}});}
function addReplyTooltip(commentEle){commentEle.querySelectorAll('a[href^="javascript:Forum.C.openCommentDialog"]').forEach(replyEle=>{const[bsn,snB,snC]=replyEle.getAttribute("href").match(/\d+/g);getReplyApiEle(bsn,snB,snC).then(replyApiEle=>{replyEle.dataset.placement="top";replyEle.dataset.toggle="tooltip";replyEle.dataset.tooltipped="";replyEle.dataset.tippyContent=`<div class="dialogify" style="display: contents; text-align:left;"><div class="dialogify__body">${replyApiEle}</div></div>`;const mouseHandler=()=>{showReplyTooltip(replyEle);replyEle.removeEventListener('mouseover',mouseHandler);};replyEle.addEventListener('mouseover',mouseHandler,{once:true});});});}
function showReplyTooltip(replyEle){const instance=tippy(replyEle,{maxWidth:560,interactive:true,appendTo:document.body,theme:document.querySelector("html").dataset.theme===undefined?'light':'dark',onShow(instance){instance.setContent(instance.reference.dataset.tippyContent);Forum.C.commentFormatter(instance.popper.querySelector("span"));},onHidden(instance){instance.destroy();}});}
function getReplyApiEle(bsn,snB,snC){return new Promise((resolve,reject)=>{if(replyMap.size>50)replyMap.clear();if(replyMap.has(snC))return resolve(replyMap.get(snC));fetch(`https://api.gamer.com.tw/forum/v1/comment_get.php?bsn=${bsn}&snB=${snB}&snC=${snC}&type=pc`,{credentials:"include",signal:abortController.signal}).then(res=>res.json().then(json=>{replyMap.set(snC,json.data.comment.html);resolve(replyMap.get(snC));}));});}
const observer=new MutationObserver(mutations=>{mutations.forEach(mutation=>{if(mutation.addedNodes.length>0)checkForNewImages();});});observer.observe(document.body,{childList:true,subtree:false});checkForNewImages();const checkInterval=setInterval(checkForNewImages,3000);window.addEventListener('beforeunload',()=>{observer.disconnect();clearInterval(checkInterval);abortController.abort();document.querySelectorAll('img[data-original-src]').forEach(img=>{img.src=img.dataset.originalSrc;});});switch(window.location.host){case"forum.gamer.com.tw":document.querySelectorAll("[id^=showoldCommend_]").forEach(el=>el.click());document.querySelectorAll("[id^=Commendlist]").forEach(commentList=>{const listObserver=new MutationObserver(mutations=>{mutations.forEach(mutation=>addReplyMouseoverListener(mutation.target));});listObserver.observe(commentList,{childList:true});});break;case"gnn.gamer.com.tw":waitElement('#comment a[href^="javascript:get_all_comment"]').then(e=>{new Function(e.getAttribute('href').split(':')[1])();});break;}
addReplyMouseoverListener(document);})();

QingJ © 2025

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