// ==UserScript== // @name 力扣新版UI评论区 // @namespace Ocyss // @version 0.3 // @description 杀软,tm的会不会设计? // @author Ocyss // @source https://github.com/Ocyss/Tampermonkey // @homepage https://github.com/Ocyss // @match https://leetcode.cn/problems/* // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.cn // @license MIT // @grant none // @downloadURL https://update.greasyfork.icu/scripts/475363/%E5%8A%9B%E6%89%A3%E6%96%B0%E7%89%88UI%E8%AF%84%E8%AE%BA%E5%8C%BA.user.js // @updateURL https://update.greasyfork.icu/scripts/475363/%E5%8A%9B%E6%89%A3%E6%96%B0%E7%89%88UI%E8%AF%84%E8%AE%BA%E5%8C%BA.meta.js // ==/UserScript== (function () { const t = setInterval(() => { const comment = document.querySelector('.mt-6.flex.flex-col.gap-3 .flex.flex-col:has( svg[data-icon="comment"])') const header = document.querySelector(".flexlayout__tabset_tabbar_inner_tab_container.flexlayout__tabset_tabbar_inner_tab_container_top") const layout = document.querySelector(".flexlayout__layout") if (!layout || !comment || !header) { return } clearInterval(t) const l = new leetcode(comment, header, layout) l.main() }, 1000) })(); class DOMApi { static createElement(tag, _class = "", id = "", content = "", style = "") { const el = document.createElement(tag) if (_class) { el.className = _class } if (id) { el.id = id } if (style) { el.style.cssText = style } el.innerHTML = content return el } static click(el, func) { el.addEventListener("click", func) } static mousedown(el, func) { el.addEventListener("mousedown", (e) => { if (e.button === 0) { func(e) } }) } static remove(cls, parent = document) { const t = setInterval(() => { const el = parent.querySelector(cls) if (!el) { return } el.remove() clearInterval(t) }, 200) } } class leetcode { constructor(comment, header, layout) { this.comment = comment // 评论区 this.header = header this.layout = layout this.flexlayout = document.querySelectorAll(".flexlayout__tab") this.content = null } main() { this.buildComment() this.buildTagBut() this.additionalEventHandler() } // 编译评论页面视图 buildComment() { this.content = DOMApi.createElement("div", "flexlayout__tab", "", "", "left: 0px; top: 32px; width: 595px; height: 928.391px; position: absolute;display: none;") const overflowTransition = this.comment.querySelector(".overflow-hidden.transition-all") overflowTransition.style.paddingBottom = "80px" this.content.appendChild(overflowTransition) DOMApi.remove(".w-full.border.p-4.bg-fill-4.border-divider-4", this.content) this.layout.appendChild(this.content) } // 编译评论标签按钮 buildTagBut() { const commentBut = DOMApi.createElement("div", "flexlayout__tab_button flexlayout__tab_button_top flexlayout__tab_button--unselected", "", `
`) const svg = this.comment.querySelector(".origin-center.transition-transform") const buts = document.querySelectorAll(".flexlayout__tab_button.flexlayout__tab_button_top") let pre const syncWidth = () => { if (!pre) { return } this.content.style.width = pre.style.width; } this.commentButEv = (e) => { if (!svg.classList.contains("rotate-180")) { this.comment.firstElementChild.click() } this.flexlayout.forEach((flex) => { if (flex.dataset.layoutPath.startsWith("/ts0") && flex.style.display == "") { pre = flex flex.style.display = "none" syncWidth() } }) buts.forEach((but) => { if (but.dataset.layoutPath.startsWith("/ts0") && but.classList.contains("flexlayout__tab_button--selected")) { but.classList.replace("flexlayout__tab_button--selected", "flexlayout__tab_button--unselected"); } }) commentBut.classList.replace("flexlayout__tab_button--unselected", "flexlayout__tab_button--selected"); this.content.style.display = "" } DOMApi.mousedown(commentBut, this.commentButEv) buts.forEach((but) => { if (but.dataset.layoutPath.startsWith("/ts0")) { DOMApi.mousedown(but, (e) => { if (pre && pre.dataset.layoutPath.replace(/\/t([^\/]*)$/, '/tb$1') == but.dataset.layoutPath) { pre.style.display = "" pre.classList.replace("flexlayout__tab_button--unselected", "flexlayout__tab_button--selected"); pre = void 0 } commentBut.classList.replace("flexlayout__tab_button--selected", "flexlayout__tab_button--unselected"); this.content.style.display = "none" }) } }) this.header.appendChild(DOMApi.createElement("div", "flexlayout__tabset_tab_divider")) this.header.appendChild(commentBut) document.addEventListener("mousemove", function (event) { syncWidth() }); setTimeout(() => { commentBut.innerHTML = commentBut.innerHTML.replaceAll("评论", this.comment.firstElementChild.firstElementChild.textContent) }, 2000) } // 进行额外事件绑定 additionalEventHandler() { // 题目描述页评论按钮重定向 const commentBug = document.querySelector("svg.fa-comment").closest("button") commentBug.onclick = () => { this.commentButEv() } this.comment.style.display = "none" } }