lib-add-stylish-string

Library: addStylish(string)

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/24621/156779/lib-add-stylish-string.js

  1. // ==UserScript==
  2. // @author @leoncastro
  3. // @namespace https://github.com/leoncastro
  4. // @name lib-add-stylish-string
  5. // @version 1.00
  6. // @description Library: addStylish(string)
  7. // @compatible firefox+greasemonkey
  8. // @compatible chrome+tampermonkey
  9. // ==/UserScript==
  10. //
  11. !(function(){
  12. window.addStylish = function addStylish(str)
  13. {
  14. var s = str;
  15. var a, b, c, d;
  16. // s = s.replace(/\/\*(?:[\s\S]?)+?\*\//g, '');
  17. s = s.replace(/@namespace.*$/m, '');
  18. a = [];
  19. b = s.split('@-moz-document');
  20. a[0] = { url: [''], css: b[0].trim() };
  21. for(var i = 1; i < b.length; i++)
  22. {
  23. a[i] = {};
  24. c = b[i].match(/[^{]*/)[0];
  25. c = c.split(',');
  26. a[i].url = [];
  27. for(var j = 0; j < c.length; j++)
  28. {
  29. d = c[j].match(/(domain|url|url-prefix|regexp)\((.*)\)/);
  30. if(d && d[2])
  31. {
  32. if( d[2][0] == '"' || d[2][0] == "'" )
  33. d[2] = d[2].slice(1, -1);
  34. switch(d[1])
  35. {
  36. case 'url':
  37. a[i].url[j] = new RegExp(d[2].replace(/\./g, '\\.'), 'i');
  38. break;
  39. case 'url-prefix':
  40. a[i].url[j] = new RegExp(d[2].replace(/\./g, '\\.') + '.*', 'i');
  41. break;
  42. case 'domain':
  43. a[i].url[j] = new RegExp('(https?:)?\/\/(www\.)?' + d[2].replace(/\./g, '\\.') + '\/.*', 'i');
  44. break;
  45. case 'regexp':
  46. a[i].url[j] = new RegExp(d[2], 'i');
  47. break;
  48. }
  49. }
  50. }
  51. c = b[i].match(/{([\s\S]*)}/);
  52. a[i].css = (c[1] || '').trim();
  53. }
  54. for(var i = 0; i < b.length; i++)
  55. {
  56. if(a[i].css)
  57. {
  58. for(var j = 0; j < a[i].url.length; j++)
  59. {
  60. if(a[i].url[j] == '' || window.location.href.match(a[i].url[j]))
  61. {
  62. var dad, obj = document.createElement('style');
  63. obj.setAttribute('type', 'text/css');
  64. obj.innerHTML = a[i].css;
  65. dad = document.getElementsByTagName('head');
  66. (dad && dad.length ? dad[0] : document).appendChild(obj);
  67. if(document.readyState == 'loading')
  68. document.addEventListener('DOMContentLoaded', function(){obj.parentNode.appendChild(obj)});
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. })();