CSS rules utilities

functions allowing to get the CSS rules applied to an element

目前为 2020-01-15 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/394970/765728/CSS%20rules%20utilities.js

  1. // ==UserScript==
  2. // @name CSS rules utilities
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description functions allowing to get the CSS rules applied to an element
  6. // @author CoStiC
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. function findCssRules(el) {
  12. var sheets = document.styleSheets, ret = [];
  13. el.matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector || el.oMatchesSelector;
  14. for (var i in sheets) {
  15. var rules = sheets[i].rules || sheets[i].cssRules;
  16. for (var r in rules) {
  17. if (el.matches(rules[r].selectorText)) {
  18. ret.push(rules[r].cssText);
  19. }
  20. }
  21. }
  22. return ret;
  23. }
  24.  
  25. function modCssRules(el, newRule) {
  26. var elHover = "";
  27. var sheets = document.styleSheets;
  28. if (el !== null) {
  29. el.matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector || el.oMatchesSelector;
  30. for (var sheet in sheets) {
  31. var rules = sheets[sheet].rules || sheets[sheet].cssRules;
  32. for (var rule in rules) {
  33. if (el.matches(rules[rule].selectorText)) {
  34.  
  35. for (let prop in newRule.cssNormal) { rules[rule].style[prop] = newRule.cssNormal[prop] }
  36. if (typeof newRule.cssHover !== 'undefined') {
  37. var rul = "";
  38. for (let propHover in newRule.cssHover) {
  39. rul += `${propHover}:${newRule.cssHover[propHover]} !important;`;
  40. };
  41. elHover = `${rules[rule].selectorText}:hover{${rul}}`;
  42. }
  43. } else {};
  44. }
  45. }
  46. }
  47.  
  48. if (elHover !== "") {
  49. let newHoverStyle = document.createElement('style'),
  50. hoverRule = document.createTextNode(elHover);
  51. newHoverStyle.appendChild(hoverRule);
  52. document.head.appendChild(newHoverStyle)
  53. //sheets[sheets.length - 1].insertRule(elHover);
  54. };
  55. }