// ==UserScript== // @name Ant Design 组件菜单 (4x) // @namespace https://xianghongai.github.io/ // @version 0.0.1 // @description 快速预览 Ant Design 组件菜单面板 // @author Nicholas Hsiang / 山茶树和葡萄树 // @icon https://xinlu.ink/favicon.ico // @match https://ant.design/components/* // @grant none // @downloadURL none // ==/UserScript== (function () { "use strict"; const APP = "#react-content"; const API = "#API"; function init() { const STYLE = ` .fixed-widgets { z-index: 9; } body[data-theme="dark"] .hs-menu-wrapper.ant-menu{ background-color: #000; } .hs-menu-title { position: fixed; font-size: 16px; top: 30px; right: 0; left: 0; margin: 0; padding: 0; text-align: center; color: #333; font-weight: normal; } .hs-overflow-hide { height: 100%; overflow: hidden; } #hs-menu-toggle, #hs-goto-api { position: fixed; z-index: 99999; top: 15px; right: 5px; cursor: pointer; width: 28px; height: 28px; overflow: hidden; line-height: 28px; border-radius: 50%; border: 1px solid #ccc; text-align: center; vertical-align: middle; color: #333; background-color: #fff; } #hs-goto-api { top: 55px; } .hs-menu-wrapper.ant-menu { position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 99998; display: flex; justify-content: space-evenly; margin: 0; padding: 50px 0 ; overflow-y: auto; background-color: #fff; } /* #region scrollbar */ .hs-menu-wrapper::-webkit-scrollbar { width: 8px; height: 6px; background: rgba(0, 0, 0, 0.1); } .hs-menu-wrapper::-webkit-scrollbar-thumb { background: rgba(0, 0, 0, 0.3); } .hs-menu-wrapper::-webkit-scrollbar-track { background: rgba(0, 0, 0, 0.1); } /* #endregion */ .hs-menu-wrapper.ant-menu li { list-style: none; } .hs-menu-wrapper.ant-menu .ant-menu-item-group-title { padding-left: 0; } .hs-menu-wrapper.ant-menu .ant-menu-item-group-list { margin: 0; padding: 0; } .hs-menu-wrapper.ant-menu .ant-menu-item-group-list .ant-menu-item { margin-bottom: 0 !important; padding-left: 0 !important; height: 38px !important; line-height: 38px !important; } .hs-hide,.hs-menu-wrapper-hide { display: none !important; } `; const TOGGLE_TPL = `
`; const bodyContainer = document.querySelector("body"); const appContainer = document.querySelector(APP); // #region COMMON function hasClass(el, className) { if (el.classList) { return el.classList.contains(className); } else { return !!el.className.match(new RegExp("(\\s|^)" + className + "(\\s|$)")); } } function getParents(elem, selector) { // Element.matches() polyfill if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector || Element.prototype.webkitMatchesSelector || function (s) { var matches = (this.document || this.ownerDocument).querySelectorAll(s), i = matches.length; while (--i >= 0 && matches.item(i) !== this) {} return i > -1; }; } // Get the closest matching element for (; elem && elem !== document; elem = elem.parentNode) { if (elem.matches(selector)) return elem; } return null; } // #endregion // #region ADD STYLE const head = document.head || document.getElementsByTagName("head")[0]; const style = document.createElement("style"); style.type = "text/css"; if (style.styleSheet) { style.styleSheet.cssText = STYLE; } else { style.appendChild(document.createTextNode(STYLE)); } head.appendChild(style); // #endregion // #region Create Toggle const toggleContainer = document.createElement("section"); toggleContainer.setAttribute("id", "component-menu-wrapper"); toggleContainer.innerHTML = TOGGLE_TPL; appContainer.appendChild(toggleContainer); // #endregion // #region Create Menu const menuContainer = document.querySelector(".aside-container.menu-site"); //.cloneNode(true); menuContainer.setAttribute("id", "Components$Menu2"); menuContainer.classList.add("hs-menu-wrapper"); const titleEle = document.createElement("h1"); titleEle.classList.add("hs-menu-title"); titleEle.innerText = "Ant Design 组件"; menuContainer.appendChild(titleEle); appContainer.appendChild(menuContainer); // #endregion // #region Add Event const menuToggleEle = document.getElementById("hs-menu-toggle"); const gotoAPI = document.getElementById("hs-goto-api"); const menuWrapper = document.querySelector(".hs-menu-wrapper"); menuWrapper.classList.add("hs-menu-wrapper-hide"); menuToggleEle.addEventListener("click", (event) => { menuWrapper.classList.toggle("hs-menu-wrapper-hide"); bodyContainer.classList.toggle("hs-overflow-hide"); }); menuWrapper.addEventListener("click", (event) => { const targetEle = event.target; if (getParents(targetEle, ".ant-menu-item") || hasClass(targetEle, "ant-menu-item")) { menuWrapper.classList.add("hs-menu-wrapper-hide"); bodyContainer.classList.remove("hs-overflow-hide"); } }); gotoAPI.addEventListener("click", (event) => { const targetEle = document.querySelector(API); const top = targetEle.getBoundingClientRect().top + window.pageYOffset; window.scrollTo({ top, behavior: "smooth", }); }); // #endregion } function resetLayout() { let pageSider = document.querySelector(".main-wrapper>.ant-row>.main-menu"); let pageContainer = document.querySelector(".main-wrapper>.ant-row>.ant-col+.ant-col"); pageSider.classList.add("hs-hide"); pageContainer.classList.remove("ant-col-md-18", "ant-col-lg-18", "ant-col-xl-19", "ant-col-xxl-20"); pageContainer.classList.add("ant-col-md-24", "ant-col-lg-24", "ant-col-xl-24", "ant-col-xxl-24"); } let interval = null; function isLoaded(params) { let appContainer = document.querySelector(APP); if (appContainer) { clearInterval(interval); init(); resetLayout(); } } interval = setInterval(isLoaded, 1000); })();