Baidu x Stylish Fix

Restore Stylish styles from deletion on baidu.com

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Baidu x Stylish Fix
// @namespace       https://greasyfork.org/users/4
// @description     Restore Stylish styles from deletion on baidu.com
// @version         1.1
// @author          LouCypher, modify by Maplerecall
// @match           *://www.baidu.com/*
// @run-at          document-end
// ==/UserScript==

function addStyle(id, css) {
  var style = document.createElement("style");
  style.type = "text/css";
  style.id = id;
  style.textContent = css;
  document.head.appendChild(style);
}

function checkForStylish() {
  return document.head.querySelectorAll('style[id^="stylish"]');
}

var styles = checkForStylish();
//console.log(styles.length);
if (styles.length) {
  var stylish = [];
  for (var i = 0; i < styles.length; i++) {
    stylish[i] = {};
    stylish[i].id = styles[i].id;
    stylish[i].css = styles[i].textContent;
  }
}

new (WebKitMutationObserver)(function(aMutations) {
  aMutations.forEach(function(aMutation) {
    if (aMutation.removedNodes.length)
      if (styles.length && !checkForStylish().length) {
        for (var i in stylish) {
          addStyle(stylish[i].id, stylish[i].css);
        }
      }
  });
}).observe(document.head, {childList:true});