Greasy Fork

Greasy Fork is available in English.

字体渲染(自用脚本)

让每个页面的字体变得有质感,默认使用苹方字体加阴影,自用脚本不处理外部需求。

当前为 2020-11-24 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            字体渲染(自用脚本)
// @namespace       https://openuserjs.org/users/t3xtf0rm4tgmail.com
// @version         2020.11.24.13
// @icon            https://github.githubassets.com/favicons/favicon.svg
// @description     让每个页面的字体变得有质感,默认使用苹方字体加阴影,自用脚本不处理外部需求。
// @author          F9y4ng
// @include         *
// @grant           none
// @license         GPL-3.0-only
// @create          2020-11-24
// @copyright       2020, F9y4ng
// @run-at          document-start
// ==/UserScript==

(function () {

  let isdebug = false;
  let debug = isdebug ? console.log.bind(console) : function () {};

  const shadow_r = 6; //阴影大小:currentcolor

  let tshadow = `
     * :not(input):not(textarea):not(i) {
        text-shadow: 1px 1px ` + shadow_r + `px #999!important;
        font-family: 'PingFang SC','Microsoft YaHei'!important;
    }`;

  addStyle(tshadow, "Font_Render", "head");

  if (location.host.includes(".baidu.com")) {
    document.addEventListener('DOMNodeInserted', Callback, false);
  }

  function Callback(e) {
    if (e.target !== null && typeof (e.target.className) === "string" && e.target.className.indexOf("Font_Render") === 0) {
      return;
    }
    setTimeout(function () {
      addStyle(tshadow, "Font_Render", "head");
    }, 200);
  }

  function addStyle(css, className, addToTarget, isReload, initType) {
    RAFInterval(function () {
      let addTo = document.querySelector(addToTarget);
      if (typeof (addToTarget) === "undefined") {
        addTo = (document.head || document.body || document.documentElement || document);
      }
      isReload = isReload || false;
      initType = initType || "text/css";
      if (typeof (addToTarget) === "undefined" || (typeof (addToTarget) !== "undefined" && document.querySelector(addToTarget) !== null)) {
        if (isReload === true) {
          safeRemove("." + className);
        }
        else if (isReload === false && document.querySelector("." + className) !== null) {
          return true;
        }
        let cssNode = document.createElement("style");
        if (className !== null) {
          cssNode.className = className;
        }
        cssNode.setAttribute("type", initType);
        cssNode.innerHTML = css;
        try {
          addTo.appendChild(cssNode);
        }
        catch (e) {
          debug('//-> ' + e.name);
        }
        return true;
      }
    }, 200, true);
  }

  function safeRemove(Css) {
    safeFunction(() => {
      let removeNodes = document.querySelectorAll(Css);
      for (let i = 0; i < removeNodes.length; i++) {
        removeNodes[i].remove();
      }
    });
  }

  function safeFunction(func) {
    try {
      func();
    }
    catch (e) {
      debug('//-> ' + e.name);
    }
  }

  function RAFInterval(callback, period, runNow) {
    const needCount = period / 1000 * 60;
    let times = 0;
    if (runNow === true) {
      const shouldFinish = callback();
      if (shouldFinish) {
        return;
      }
    }

    function step() {
      if (times < needCount) {
        times++;
        requestAnimationFrame(step);
      }
      else {
        const shouldFinish = callback() || false;
        if (!shouldFinish) {
          times = 0;
          requestAnimationFrame(step);
        }
        else {
          return;
        }
      }
    }
    requestAnimationFrame(step);
  }
})();