Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. (function(){
  2. const setProps = new WeakMap;
  3. for (const ele of document.getElementsByTagName("*"))
  4. setProps.set(ele, new Set);
  5.  
  6. function parseRules(ruleSet) {
  7. for (const rule of ruleSet)
  8. if (rule.type === 1) { // STYLE_RULE
  9. for (const ele of document.querySelectorAll(rule.selectorText)) {
  10. const setObj = setProps.get(ele);
  11. for (const prop of rule.style)
  12. setObj.add(prop);
  13. }
  14. } else if (rule.type === 3 || rule.type === 4) { // import or media
  15. if (!rule.media.mediaText || matchMedia(rule.media.mediaText).matches) {
  16. if (rule.type === 4) parseRules(rule.cssRules); // MEDIA_RULE
  17. else try{ parseRules(rule.styleSheet.cssRules); }catch(e){} // IMPORT_RULE
  18. }
  19. } else if (rule.type === 6) { // PAGE_RULE
  20. // nothing to do here
  21. } else if (rule.type === 7) { // KEYFRAMES_RULE
  22. // nothing to do here
  23. } else if (rule.type === 12) { // SUPPORTS_RULE
  24. if (!rule.conditionText || CSS.supports(rule.conditionText)) {
  25. parseRules(rule.cssRules)
  26. }
  27. }
  28. }
  29.  
  30. for (const sheet of document.styleSheets)
  31. if (!sheet.disabled && sheet.cssRules)
  32. try { parseRules(sheet.cssRules); } catch(e){}
  33.  
  34. for (const ele of document.getElementsByTagName("*")) {
  35. const compStyle = getComputedStyle(ele), liveStyle = ele.style;
  36. for (const cssProp of setProps.get(ele)) {
  37. // check if this style is really needed (to compensate for CSSReset sheets and such)
  38. var trueValue = compStyle[cssProp];
  39.  
  40. liveStyle.setProperty(cssProp, "unset", "important");
  41. if (compStyle[cssProp] === trueValue) {
  42.  
  43. liveStyle.removeProperty( cssProp );
  44. } else {
  45.  
  46. liveStyle.setProperty(cssProp, "inherit", "important");
  47. if (compStyle[cssProp] === trueValue) {
  48.  
  49. liveStyle[cssProp] = "inherit";
  50. } else {
  51. // this style property really is needed
  52. liveStyle[cssProp] = trueValue;
  53. }
  54. }
  55. }
  56. }
  57.  
  58.  
  59. for (const sheet of document.styleSheets)
  60. sheet.diabled = true; // disable all stylesheets because they are no longer needed.
  61.  
  62. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement