change_style

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

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

  1. // ==UserScript==
  2. // @name change_style
  3. // @namespace https://netoday.cn
  4. // @version 0.1.4
  5. // @description 一些网站的配色方案非常不适合阅读,比如知乎专栏白色背景黑色字体,看一会就非常刺眼,故此写个脚本,方便以后遇到这种网站直接自动修改样式。
  6. // @author crazy_pig
  7. // @match https://zhuanlan.zhihu.com/*
  8. // @match https://blog.csdn.net/*
  9. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. // 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)
  15. const _default_font_family = "gitbook-content-font,-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif, 微软雅黑";
  16. const _url_array = [
  17. ["zhuanlan.zhihu.com", "#181C1F", "#EAF2F7",_default_font_family , "Modal-closeButton","css-1ynzxqw Recommendations-Main","Post-RichTextContainer Post-SideActions", "90%"],
  18. ["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%"]
  19. ];
  20.  
  21. const URL_INDEX = 0;
  22. const BGCOLOR_INDEX = 1;
  23. const FNTCOLOR_INDEX = 2;
  24. const FNTFML_INDEX = 3;
  25. const BTN_INDEX = 4;
  26. const DELETE_INDEX = 5;
  27. const MAXIMUM_INDEX = 6;
  28. const RESIZE_INDEX = 7;
  29. const OP_MAXIMUM_BY_CLASSES = 1;
  30. const OP_MAXIMUM_BY_TAG = 2;
  31.  
  32. (function() {
  33. 'use strict';
  34.  
  35. // get url user visited
  36. var _url = (window.location + "").toLowerCase();
  37.  
  38. // if need active script
  39. var _active_index = -1;
  40. var i;
  41. for (i = 0; i < _url_array.length; i++){
  42. if (_url.indexOf(_url_array[i][URL_INDEX]) > 0){
  43. _active_index = i;
  44. break;
  45. }
  46. }
  47.  
  48. if (_active_index >= 0){
  49. // set color
  50. _recursion_set_color(document.body,
  51. _url_array[_active_index][BGCOLOR_INDEX],
  52. _url_array[_active_index][FNTCOLOR_INDEX],
  53. _url_array[_active_index][FNTFML_INDEX]);
  54.  
  55. // remove mask div
  56. setInterval(function (){
  57. // click button
  58. var _element_array = _url_array[_active_index][BTN_INDEX].split(" ");
  59. var m,i,_btns;
  60. for(m=0;m<_element_array.length;m++){
  61. if (""!==_element_array[m].trim()){
  62. _element_array[m] = _element_array[m].trim();
  63. _btns = document.getElementsByClassName(_element_array[m]);
  64. if(typeof(_btns) !== 'undefined'){
  65. for(i=0;i<_btns.length;i++){
  66. if('BUTTON' === _btns[i].tagName){
  67. // click the `close` button to close the mask div
  68. _btns[i].click();
  69. }
  70. }
  71. }
  72. }
  73. }
  74.  
  75. // remove elements by class name
  76. _element_array = _url_array[_active_index][DELETE_INDEX].split(" ");
  77. for(m=0;m<_element_array.length;m++){
  78. if (""!==_element_array[m].trim()){
  79. _element_array[m] = _element_array[m].trim();
  80. _btns = document.getElementsByClassName(_element_array[m]);
  81. if(typeof(_btns) !== 'undefined'){
  82. for(i=0;i<_btns.length;i++){
  83. _btns[i].remove();
  84. }
  85. }
  86. }
  87. }
  88.  
  89. // resize divs
  90. _resize_div(_url_array[_active_index][MAXIMUM_INDEX].split(" "), OP_MAXIMUM_BY_CLASSES, _url_array[_active_index][RESIZE_INDEX]);
  91. _resize_div(_url_array[_active_index][MAXIMUM_INDEX].split(" "), OP_MAXIMUM_BY_TAG, _url_array[_active_index][RESIZE_INDEX]);
  92. }, 1000);
  93. }
  94.  
  95. })();
  96.  
  97. function _resize_div(_div_names, _op, _resize_rate){
  98. var i,j;
  99. if(typeof(_div_names) !== 'undefined'){
  100. for(i=0;i<_div_names.length;i++){
  101. if(""!==_div_names[i]){
  102. var _elements;
  103. if (_op == 1){
  104. _elements = document.getElementsByClassName(_div_names[i]);
  105. }else{
  106. _elements = document.getElementsByTagName(_div_names[i]);
  107. }
  108. if(typeof(_elements) !== 'undefined'){
  109. for(j=0;j<_elements.length;j++){
  110. _elements[j].style.width = _resize_rate;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117.  
  118. /**
  119. * set font \ background-color \ font-family
  120. */
  121. function _recursion_set_color(parent, _bg_color, _fnt_color, _fnt_family){
  122. if (typeof(parent.children) !== 'undefined'){
  123. if (parent.children.length > 0){
  124. var i;
  125. for(i=0;i<parent.children.length;i++){
  126. _recursion_set_color(parent.children[i], _bg_color, _fnt_color, _fnt_family);
  127. }
  128. }
  129. if (""!==_bg_color){
  130. parent.style.backgroundColor = _bg_color;
  131. }
  132. if (""!==_fnt_color){
  133. parent.style.color = _fnt_color;
  134. }
  135. if (""!==_fnt_family){
  136. parent.style.fontFamily = _fnt_family;
  137. }
  138. }
  139. }

QingJ © 2025

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