AddYourStyleToWeb

通过给网页添加CSS来自定义网页样式

当前为 2022-10-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AddYourStyleToWeb
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3.2
  5. // @description 通过给网页添加CSS来自定义网页样式
  6. // @author 刘家旺
  7. // @icon http://www.weather.com.cn/favicon.ico
  8. // @match *://wenku.baidu.com/*
  9. // @match *://*.blog.csdn.net/*
  10. // @match *://*.csdn.net/*
  11. // @match *://fanyi.baidu.com/*
  12. // @match *://www.bilibili.com/*
  13. // @match *://www.cnblogs.com/*
  14. // @match *://www.it1352.com/*
  15. // @match *://www.logosc.cn/*
  16. // @match *://www.douyin.com/*
  17. // @license MIT
  18. // @run-at document-start
  19. // ==/UserScript==
  20.  
  21. /*
  22. 脚本介绍:
  23.  
  24. 使用门槛:
  25. 1.有一台电脑,电脑上的浏览器能安装油猴插件。
  26. 2.会使用tampermonkey油猴插件,不会的去百度。
  27. 3.有一点点前端编程基础,只要一点点哦!
  28.  
  29. 使用方法:
  30. 比如你要修改百度的样式,只要进行下面几步即可。
  31.  
  32. 1.将此脚本添加到油猴插件里并打开。
  33. 2.在上面添加你要匹配的网站: // @match *://www.baidu.com/*
  34. 3.在下面addStyleArr函数内添加要改变的样式,你可以将我原来的样式删除,再像我那样添加新的样式。
  35. //示例代码:
  36. else if (web("www.baidu.com")) {
  37.  
  38. addCssSelector( //至于用哪个函数请阅读脚本原理
  39. "div#head", //参数1 选择器字符串
  40. "background-color: black !important;" //参数2 CSS字符串
  41. );
  42.  
  43. }
  44.  
  45. 4.OK了,刷新百度看看效果。
  46.  
  47. 脚本原理:
  48. 此JS脚本为向网页添加CSS改变网页样式提供了便利的接口,帮你解决了JS直接修改网页样式无效的两种情况。
  49.  
  50. 情况一:
  51. js修改的元素还没加载出来,或者元素不定时出现,再或者元素具有内联样式
  52. 解决方法:
  53. 1.addCssSelector(selectorStr, cssStr);
  54. 将添加的选择器和CSS保存到数组里,之后会生成两个相同的style标签并分别添加到html起始和末尾,
  55. 以保证CSS持续作用于整个网页。这种方式对没有内联样式的元素有效。
  56.  
  57. 2.changeInlineStyle(selectorStr, cssStr);
  58. 有些元素具有内联样式并且带有!important,导致上面的方法失效,这时我们用changeInlineStyle
  59. 来修改元素的内联样式。这个函数同样将添加的选择器和CSS保存到数组里,之后先设置最短和最长修改
  60. 时间,再用定时器循环修改内联样式。每次循环若找到元素就修改,再判断是否到达最短修改时间,是
  61. 就停止循环,否就继续循环,若未找到元素就持续循环,直到达到最长修改时间。这样既防止修改一次
  62. 后被后加载的CSS覆盖,又防止找不到元素后一直循环检测,浪费CPU资源。
  63.  
  64. 3.keepChangeInlineStyle(selectorStr, cssStr);
  65. 而有些元素不但具有内联样式加!important而且会不定时改变内联样式,这时第二个函数就失效了,那
  66. 我们就用第三个函数keepChangeInlineStyle。这个函数类似于第二个函数,不同点是他会不停地循环,
  67. 一旦检测到元素的内联样式与指定的样式不符,就将其修改。
  68. 情况二:
  69. 目标元素在iframe框架内,js无法找到元素
  70. 解决方法:
  71. match 后面匹配的网址换成 iframe 内的网址即可
  72. */
  73.  
  74. "use strict";
  75.  
  76. // 在这里添加要改变的样式 ==============================================================================================
  77. function addStyleArr() {
  78. //百度文库样式
  79. if (web("wenku.baidu.com")) {
  80. //百度文库的第一种页面
  81. if (document.querySelector("body.tpl-xreader")) {
  82. changeInlineStyle(
  83. "div#app-right,.menubar,.tool-bar-wrap,.bg-theme-wrap.no-full-screen,.page-icon-pos.pos,.page-icon-on.pos,#app-left,#app-top-right-tool",
  84. "display: none !important;"
  85. );
  86. changeInlineStyle(
  87. ".center-wrapper.zoom-scale.classic",
  88. "width: calc(100% - 280px) !important;height: 120% !important;"
  89. );
  90. addCssSelector(
  91. ".creader-canvas",
  92. "width: 1000px !important;height: 100% !important;"
  93. );
  94. }
  95. //百度文库的第二种页面,office文档
  96. else if (document.querySelector(".catalog-main")) {
  97. changeInlineStyle(
  98. ".tool-bar-wrapper.wk-web-4774.fade-in,.theme-enter-wrap,.sidebar-wrapper",
  99. "display: none !important;"
  100. );
  101. }
  102. //百度文库的第三种页面,几百页的书
  103. else if (document.querySelector(".doc-info-wrapper")) {
  104. changeInlineStyle(
  105. ".tool-bar-wrapper.wk-web-4774.fade-in,.theme-enter-wrap,.sidebar-wrapper",
  106. "display: none !important;"
  107. );
  108. }
  109. }
  110. //CSDN样式
  111. else if (web("csdn.net")) {
  112. addCssSelector(
  113. ".main_father.clearfix.d-flex.justify-content-center,body,#userSkin",
  114. "background: #06090a !important;"
  115. );
  116. addCssSelector(".column_info_box", "position: unset !important;");
  117. if (1) {
  118. // CSDN 打印保存样式
  119. addCssSelector(
  120. "aside.blog_container_aside,div#blogColumnPayAdvert,div#toolBarBox,div#pcCommentBox,div#recommendNps,.template-box,\
  121. .blog-footer-bottom,.csdn-side-toolbar,.top-banner,.ag-tab-bar,.satisfied-component",
  122. "display: none !important;"
  123. );
  124. addCssSelector(
  125. "main,div#mainBox,.main_father.clearfix.d-flex.justify-content-center",
  126. "width: 100% !important;margin: 0 !important;padding: 0 !important;float: left !important;width: 100% !important;"
  127. );
  128. addCssSelector(
  129. "main div.blog-content-box",
  130. "padding: 0 10% !important;"
  131. );
  132. addCssSelector(".con-l-tag", "width: 100% !important;");
  133. }
  134. }
  135. //百度翻译样式
  136. else if (web("fanyi.baidu.com")) {
  137. addCssSelector(
  138. "#transOtherRight,.vip-btn-activity",
  139. "display:none !important;"
  140. );
  141. }
  142. //bilibili样式
  143. else if (web("www.bilibili.com")) {
  144. addCssSelector(
  145. ".bpx-player-container[data-screen=full] .bpx-player-shadow-progress-area,.bpx-player-shadow-progress-area",
  146. "height: 5px !important; visibility:visible !important; opacity: 1 !important;"
  147. );
  148. }
  149. //博客园样式
  150. else if (web("www.cnblogs.com")) {
  151. if (1) {
  152. // 博客园打印保存样式
  153. addCssSelector(
  154. "#notHomeTopCanvas,#articleInfo > a,#open-button,#dayNightSwitch,#aplayer,#cnblogs_post_body > div.essaySuffix-box,#blog_post_info_block,#cnblogs_post_body > p.essaySuffix-eof,\
  155. #topics > div > div.postDesc,#comment_form,#footer",
  156. "display: none !important;"
  157. );
  158. addCssSelector(
  159. "*",
  160. "color: #bbb !important; background: black !important"
  161. );
  162. }
  163. }
  164. //it1352样式
  165. else if (web("www.it1352.com")) {
  166. // 展开全部内容
  167. addCssSelector(".arc-body-main", "height: auto !important;");
  168. addCssSelector(".arc-body-main-more", "display: none !important;");
  169. }
  170. // 抖音样式
  171. else if (web("www.douyin.com")) {
  172. // 进度条颜色
  173. addCssSelector(
  174. "xg-played.xgplayer-progress-played",
  175. "background-color: white !important;"
  176. );
  177. }
  178. // 标小智Logo
  179. else if (web("www.logosc.cn")) {
  180. // logo去水印放大
  181. if (1) {
  182. addCssSelector(
  183. "rect.watermarklayer,html::after",
  184. "display: none !important;"
  185. );
  186. addCssSelector(
  187. ".flex-1 .swiper-slide-active .svg-card svg",
  188. "position: fixed;width: 1350px;height: 1350px;top: -63%;left: 10%;z-index: 99999999 !important;"
  189. );
  190. }
  191. }
  192. // 添加新网站
  193. // else if (web("xxx.xxx.xxx")) {
  194. // // 添加样式
  195. // addCssSelector(
  196. // "选择器",
  197. // "CSS样式"
  198. // );
  199. // }
  200.  
  201. }
  202. // 添加完毕 ===========================================================================================================
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. //以下是功能实现,请慎重修改!
  212.  
  213. //jsInserted标志JS代码是否已全部注入,如果未完全注入就开始调用函数会出现函数未定义错误
  214. var jsInserted = false;
  215. var checkInserted = setInterval(() => {
  216. if (jsInserted) {
  217. // 请在这里调用 StartToChange
  218. StartToChange();
  219. clearInterval(checkInserted);
  220. }
  221. }, 100);
  222.  
  223. //----------------------------------------------------- 参数设置
  224. var initTime = 1000 * 6; //初始识别网页时间
  225. //普通检测参数:
  226. var commonCycleTime = 300; //周期
  227. var minTime = 1000 * 6; //最短检测时间
  228. var maxTime = 1000 * 15; //最长检测时间
  229. //持续监测参数:
  230. var keepCycleTime = 500; //周期
  231. //-----------------------------------------------------
  232. //普通元素列表
  233. var commonEleArr = [];
  234.  
  235. //需要持续检测的元素列表
  236. var keepSetArr = [];
  237.  
  238. //添加css选择器的元素列表
  239. var CSSArr = [];
  240.  
  241. //添加style标签,适用于没有内联样式的元素
  242. function addCssSelector(selectorStr, cssStr) {
  243. CSSArr.push({ selector: selectorStr, css: cssStr });
  244. }
  245. //改变内敛样式,适用于具有内联样式并且不会变动的元素
  246. //参数1:选择器,参数2:样式
  247. function changeInlineStyle(selectorStr, cssStr) {
  248. //保存每一个 选择器字符串 和 样式字符串 为一个 对象,添加到数组里
  249. commonEleArr.push({ selector: selectorStr, css: cssStr });
  250. }
  251. //持续改变内敛样式,适用于具有内联样式并且不定时变动的元素
  252. function keepChangeInlineStyle(selectorStr, cssStr) {
  253. keepSetArr.push({ selector: selectorStr, css: cssStr });
  254. }
  255.  
  256. //判断网址
  257. function web(urlStr) {
  258. if (RegExp(urlStr, "i").test(location.href)) {
  259. return true;
  260. } else {
  261. return false;
  262. }
  263. }
  264.  
  265. //循环检测,直到出现或超时
  266. function setStyle() {
  267. var isEndLoop = false; //是否结束循环的标记
  268. var laterStop = true; //是否长时间未找到,需要停止循环
  269. var overMinTime = false; //在最短时间内,不管找没找到都不许停,否则会被后来加载的css覆盖
  270.  
  271. setTimeout(function () {
  272. overMinTime = true;
  273. }, minTime);
  274.  
  275. var checkLoop = setInterval(function () {
  276. //达到最短检测时间才开始判断是否停止,没到最短时间isEndTime始终是false
  277. if (overMinTime) {
  278. isEndLoop = true;
  279. }
  280. //遍历检测元素是否存在
  281. for (var i = 0; i < commonEleArr.length; i++) {
  282. var objList = document.querySelectorAll(commonEleArr[i].selector);
  283. //如果找到了就改样式
  284. if (objList.length > 0) {
  285. for (var j = 0; j < objList.length; j++) {
  286. //如果改过了就不要重复改了
  287. if (objList[j].style.cssText != commonEleArr[i].css) {
  288. objList[j].style.cssText = commonEleArr[i].css;
  289. }
  290. }
  291. }
  292. //没有找到就标记false,继续检测
  293. else {
  294. isEndLoop = false;
  295. }
  296. }
  297. if (isEndLoop) {
  298. clearInterval(checkLoop);
  299. laterStop = false; //已经全部找到并停止了,不需要再次停止
  300. console.log("Jawon: 普通循环检测已结束");
  301. }
  302. }, commonCycleTime);
  303. //到最长检测时间后还没找到就不找了
  304. setTimeout(function () {
  305. if (laterStop) {
  306. clearInterval(checkLoop);
  307. console.log("Jawon: 普通循环检测已结束,但仍有元素未找到!");
  308. }
  309. }, maxTime);
  310. }
  311.  
  312. //持续循环检测
  313. function keepSetStyle() {
  314. setInterval(function () {
  315. //遍历检测元素
  316. for (var i = 0; i < keepSetArr.length; i++) {
  317. var objList = document.querySelectorAll(keepSetArr[i].selector);
  318.  
  319. for (var j = 0; j < objList.length; j++) {
  320. if (objList[j].style.cssText != keepSetArr[i].css) {
  321. objList[j].style.cssText = keepSetArr[i].css;
  322. }
  323. }
  324. }
  325. }, keepCycleTime);
  326. }
  327.  
  328. //在html起始和末尾分别添加style标签
  329. function setCssSelector() {
  330. var myStyleFirst = document.createElement("style");
  331. var myStyleLast = document.createElement("style");
  332. myStyleFirst.className = "CssByJawon";
  333. myStyleLast.className = "CssByJawon";
  334. for (let i = 0; i < CSSArr.length; i++) {
  335. myStyleFirst.innerHTML +=
  336. CSSArr[i].selector + "{" + CSSArr[i].css + "}";
  337. myStyleLast.innerHTML += CSSArr[i].selector + "{" + CSSArr[i].css + "}";
  338. }
  339. var htmlObj = document.querySelector("html");
  340. htmlObj.insertBefore(myStyleFirst, htmlObj.firstChild);
  341. htmlObj.appendChild(myStyleLast);
  342. }
  343.  
  344. //有些网站有多种样式布局,需要通过查找某个元素来辨别,为了保证找到该元素,需要多找几次,避免该元素还没加载出来。
  345. function StartToChange() {
  346. var laterStop = true;
  347. var addEleLoop = setInterval(function () {
  348. if (
  349. commonEleArr.length == 0 &&
  350. keepSetArr.length == 0 &&
  351. CSSArr.length == 0
  352. ) {
  353. //如果没有元素就添加
  354. addStyleArr();
  355. //一旦添加成功,立即停止识别,开始循环检测,修改样式
  356. if (
  357. commonEleArr.length != 0 ||
  358. keepSetArr.length != 0 ||
  359. CSSArr.length != 0
  360. ) {
  361. clearInterval(addEleLoop);
  362. laterStop = false;
  363. if (commonEleArr.length > 0) {
  364. setStyle();
  365. console.log("Jawon: 普通循环检测已开启");
  366. }
  367. if (keepSetArr.length > 0) {
  368. keepSetStyle();
  369. console.log("Jawon: 持续循环检测已开启");
  370. }
  371. if (CSSArr.length > 0) {
  372. setCssSelector();
  373. console.log("Jawon: 已添加选择器");
  374. }
  375. }
  376. }
  377. }, 200);
  378. //initTime之内未添加元素就放弃,说明未识别到网站
  379. setTimeout(function () {
  380. if (laterStop) {
  381. clearInterval(addEleLoop);
  382. console.log("Jawon: 未识别到网站");
  383. }
  384. }, initTime);
  385. }
  386.  
  387. // js代码插入完毕的标志性
  388. jsInserted = true;

QingJ © 2025

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