change_style

一些网站的配色方案非常不适合阅读,比如知乎专栏白色背景黑色字体,看一会就非常刺眼,故此写个脚本,方便以后遇到这种网站直接自动修改样式。

当前为 2022-09-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name change_style
  3. // @namespace https://netoday.cn
  4. // @version 0.1.8
  5. // @description 一些网站的配色方案非常不适合阅读,比如知乎专栏白色背景黑色字体,看一会就非常刺眼,故此写个脚本,方便以后遇到这种网站直接自动修改样式。
  6. // @author crazy_pig
  7. // @match https://zhuanlan.zhihu.com/*
  8. // @match https://www.zhihu.com/*
  9. // @match https://blog.csdn.net/*
  10. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. // default urls and style to use this script: 0=url,1=bgcolor,2=font color,3=font family, 4=btn names 2 click, 5=elements 2 remove by class, 6=div 2 maximum by classes(1) or by tag(2)
  16. const _default_font_family = "gitbook-content-font,-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif, 微软雅黑";
  17. const _url_array = [
  18. ["zhuanlan.zhihu.com", "#181C1F", "#EAF2F7",_default_font_family , "Modal-closeButton","css-1ynzxqw Recommendations-Main","Post-RichTextContainer Post-SideActions", "90%"],
  19. ["zhihu.com", "#181C1F", "#EAF2F7",_default_font_family , "Modal-closeButton","Question-sideColumn Question-sideColumn--sticky css-1qyytj7 css-1ynzxqw","List-item Question-mainColumn", "90%"],
  20. ["blog.csdn.net", "", "", "", "","blog_container_aside blog-footer-bottom more-toolbox-new recommend-box template-box recommend-right recommend-nps-box csdn-side-toolbar","main_father main container nodata", "100%"]
  21. ];
  22.  
  23. const URL_INDEX = 0;
  24. const BGCOLOR_INDEX = 1;
  25. const FNTCOLOR_INDEX = 2;
  26. const FNTFML_INDEX = 3;
  27. const BTN_INDEX = 4;
  28. const DELETE_INDEX = 5;
  29. const MAXIMUM_INDEX = 6;
  30. const RESIZE_INDEX = 7;
  31. const OP_MAXIMUM_BY_CLASSES = 1;
  32. const OP_MAXIMUM_BY_TAG = 2;
  33.  
  34. (function() {
  35. 'use strict';
  36.  
  37. // get url user visited
  38. var _url = (window.location + "").toLowerCase();
  39.  
  40. // if need active script
  41. var _active_index = -1;
  42. var i;
  43. for (i = 0; i < _url_array.length; i++){
  44. if (_url.indexOf(_url_array[i][URL_INDEX]) > 0){
  45. _active_index = i;
  46. break;
  47. }
  48. }
  49.  
  50. if (_active_index >= 0){
  51. // set color
  52. _recursion_set_color(document.body,
  53. _url_array[_active_index][BGCOLOR_INDEX],
  54. _url_array[_active_index][FNTCOLOR_INDEX],
  55. _url_array[_active_index][FNTFML_INDEX]);
  56. var csdnContentBox = document.getElementsByClassName("blog-content-box")[0];
  57. if (typeof(csdnContentBox) !== "undefined"){
  58. csdnContentBox.style.background = "#8DA399";
  59. var links = document.getElementsByTagName("a");
  60. for (i = 0; i < links.length; i++){
  61. links[i].style.color = "#0014ff";
  62. }
  63. }
  64. var zhihuContentBox = document.getElementsByClassName("QuestionHeader-title")[1];
  65. if (typeof(zhihuContentBox) !== "undefined"){
  66. zhihuContentBox.style.marginTop = "30px";
  67. }
  68.  
  69. // remove mask div
  70. setInterval(function (){
  71. // click button
  72. var _element_array = _url_array[_active_index][BTN_INDEX].split(" ");
  73. var m,i,_btns;
  74. for(m=0;m<_element_array.length;m++){
  75. if (""!==_element_array[m].trim()){
  76. _element_array[m] = _element_array[m].trim();
  77. _btns = document.getElementsByClassName(_element_array[m]);
  78. if(typeof(_btns) !== 'undefined'){
  79. for(i=0;i<_btns.length;i++){
  80. if('BUTTON' === _btns[i].tagName){
  81. // click the `close` button to close the mask div
  82. _btns[i].click();
  83. }
  84. }
  85. }
  86. }
  87. }
  88. _btns = document.getElementById("passportbox");
  89. if(null !== _btns && typeof(_btns) !== 'undefined' && _btns.children.length > 0){
  90. _btns.children[1].click();
  91. }
  92.  
  93.  
  94. // remove elements by class name
  95. _element_array = _url_array[_active_index][DELETE_INDEX].split(" ");
  96. for(m=0;m<_element_array.length;m++){
  97. if (""!==_element_array[m].trim()){
  98. _element_array[m] = _element_array[m].trim();
  99. _btns = document.getElementsByClassName(_element_array[m]);
  100. if(typeof(_btns) !== 'undefined'){
  101. for(i=0;i<_btns.length;i++){
  102. _btns[i].remove();
  103. }
  104. }
  105. }
  106. }
  107.  
  108. // resize divs
  109. _resize_div(_url_array[_active_index][MAXIMUM_INDEX].split(" "), OP_MAXIMUM_BY_CLASSES, _url_array[_active_index][RESIZE_INDEX]);
  110. _resize_div(_url_array[_active_index][MAXIMUM_INDEX].split(" "), OP_MAXIMUM_BY_TAG, _url_array[_active_index][RESIZE_INDEX]);
  111.  
  112. // open hidden divs
  113. var hiddenDivArray = document.getElementsByClassName("hide-preCode-bt");
  114. if (typeof(hiddenDivArray) !== "undefined" && hiddenDivArray.length > 0){
  115. for (i = 0; i < hiddenDivArray.length; i++){
  116. hiddenDivArray[i].click();
  117. }
  118. }
  119. }, 1000);
  120. }
  121.  
  122. })();
  123.  
  124. function _resize_div(_div_names, _op, _resize_rate){
  125. var i,j;
  126. if(typeof(_div_names) !== 'undefined'){
  127. for(i=0;i<_div_names.length;i++){
  128. if(""!==_div_names[i]){
  129. var _elements;
  130. if (_op == 1){
  131. _elements = document.getElementsByClassName(_div_names[i]);
  132. }else{
  133. _elements = document.getElementsByTagName(_div_names[i]);
  134. }
  135. if(typeof(_elements) !== 'undefined'){
  136. for(j=0;j<_elements.length;j++){
  137. _elements[j].style.width = _resize_rate;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144.  
  145. /**
  146. * set font \ background-color \ font-family
  147. */
  148. function _recursion_set_color(parent, _bg_color, _fnt_color, _fnt_family){
  149. if (typeof(parent.children) !== 'undefined'){
  150. if (parent.children.length > 0){
  151. var i;
  152. for(i=0;i<parent.children.length;i++){
  153. _recursion_set_color(parent.children[i], _bg_color, _fnt_color, _fnt_family);
  154. }
  155. }
  156. if (""!==_bg_color){
  157. parent.style.backgroundColor = _bg_color;
  158. }
  159. if (""!==_fnt_color){
  160. parent.style.color = _fnt_color;
  161. }
  162. if (""!==_fnt_family){
  163. parent.style.fontFamily = _fnt_family;
  164. }
  165. }
  166. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址