Greasy Fork

Greasy Fork is available in English.

文档选中美化

MDN/Mysql 文档选中美化

当前为 2022-06-13 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         文档选中美化
// @namespace    http://tampermonkey.net/
// @version      2.0.2
// @description  MDN/Mysql 文档选中美化
// @author       itxve
// @match        https://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      MIT
// ==/UserScript==

function Load(predicate, fn) {
  predicate() && fn();
}
//平滑滚动至浏览目录
const scrollRootElement = (rootEle, top) => {
  rootEle.scrollTo({ top, behavior: "smooth" });
};

const MysqlDocSelct = (cssText) => {
  let path = location.pathname.split("/");
  let dom = document.querySelector("a[href='" + path[path.length - 1] + "']");
  dom.style.cssText = cssText;
  scrollRootElement(
    document.querySelector("#docs-sidebar-toc"),
    dom.offsetTop - 50
  );
};

const MDnDocSelect = () => {
  const getPath = (href) => {
    const path = new URL(href).pathname
      .split("/")
      .filter((_, i) => i > 1)
      .join("/");
    return "/" + path;
  };

  const task = () => {
    // Your code here...
    const activeStyle = `color: var(--text-primary);font-weight: 600;
       border-left: 4px solid #1870F0;
       background-color: var(--category-color-background);`;

    const detailsStyle = `color: var(--text-primary);font-weight: 800;
       border-left: 3px solid var(--text-link);
       background-color: var(--category-color-background);`;

    const rootElement = document.querySelector("#sidebar-quicklinks");

    const offTop = 120;

    //展开
    const openTheParent = (adom) => {
      const maxFind = 5;
      let findCOunt = 0;
      while (findCOunt < maxFind) {
        if (adom && adom.tagName !== "DETAILS") {
          adom = adom.parentElement;
        }
        findCOunt++;
      }
      if (adom == rootElement) return;
      //没有DETAILS的不做open
      if (adom && adom.tagName == "DETAILS") {
        adom.setAttribute("open", "open");
        adom.style = detailsStyle;
      }
    };

    const as = rootElement.querySelectorAll("li a");
    for (var i = 0; i < as.length; i++) {
      const adom = as[i];
      const domHref = getPath(adom.href);
      const localHref = getPath(location.href);
      if (!~adom.href.indexOf("#") && domHref === localHref) {
        adom.style = activeStyle;
        openTheParent(adom);
        scrollRootElement(rootElement, adom.offsetTop - offTop);
        break;
      }
    }

    //非A标签的选中MDN的接口属性(event)
    const emDom = rootElement.querySelector("li em");
    if (emDom) {
      emDom.style = activeStyle;
      scrollRootElement(rootElement, emDom.offsetTop - offTop);
    }
  };

  setTimeout(() => task());
  //切换语言不选中
  const pushState = window.history.pushState;
  window.history.pushState = (...args) => {
    pushState(args);
    task();
  };
};

(function () {
  "use strict";
  // mysql
  Load(
    () => location.host == "dev.mysql.com",
    () => MysqlDocSelct("font-size:16px;color:#f29221;")
  );

  
})();