It's Not Important

At least part of the world will became a bit less important now.

目前为 2016-03-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name It's Not Important
  3. // @namespace lainscripts_it_is_not_important
  4. // @version 1.3
  5. // @description At least part of the world will became a bit less important now.
  6. // @author lainverse
  7. // @match *://*/*
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11. /* jshint esnext: true */
  12.  
  13. (function(){
  14. 'use strict';
  15.  
  16. var imptt = /((display|(margin|padding)(-top|-bottom)?)\s*:[^;!]*)!\s*important/ig,
  17. rplsf = function(str,grp){return grp;};
  18.  
  19. function unimportanter(el, si) {
  20. if (!imptt.test(si) || el.style.display == 'none')
  21. return 0; // get out if we have nothing to do here
  22. var so = si.replace(imptt, rplsf), ret = 0;
  23. if (si != so) {
  24. ret = 1;
  25. el.setAttribute('style', so);
  26. }
  27. return ret;
  28. }
  29.  
  30. function logger(c) {
  31. if (c) console.log('Some page elements became a bit less important.');
  32. }
  33.  
  34. document.addEventListener ("DOMContentLoaded", function(){
  35. var c = 0, imp = document.querySelectorAll('[style*="!"]'), i = imp.length;
  36. while(i--)
  37. c+= unimportanter(imp[i]);
  38. logger(c);
  39. });
  40.  
  41. function checkTarget(m, c) {
  42. var si = m.getAttribute ? m.getAttribute('style') : null;
  43. if (si && si.indexOf('!') > -1)
  44. c+=unimportanter(m, si);
  45. return c;
  46. }
  47.  
  48. function checkNodes(m, c) {
  49. var i = m.length;
  50. while(i--)
  51. c = checkTarget(m[i], c);
  52. return c;
  53. }
  54.  
  55. var observer = new MutationObserver(function(mutations) {
  56. setTimeout(function(m) {
  57. var i = m.length, c = 0;
  58. while(i--) {
  59. if (m[i].target)
  60. c = checkTarget(m[i].target, c);
  61. if (m[i].addedNodes.length)
  62. c = checkNodes(m[i].addedNodes, c);
  63. }
  64. logger(c);
  65. },0,mutations);
  66. });
  67.  
  68. observer.observe(document, { childList : true, attributes : true, attributeFilter : ['style'], subtree : true });
  69. })();