userstyles.org css highlighter

Formats and highlights CSS code shown after [Show CSS] clicking. (works with the new userstyles.org layout).

As of 2017-12-17. See the latest version.

  1. // ==UserScript==
  2. // @name userstyles.org css highlighter
  3. // @namespace darkred
  4. // @version 2017.12.15
  5. // @license MIT
  6. // @description Formats and highlights CSS code shown after [Show CSS] clicking. (works with the new userstyles.org layout).
  7. // @author trespassersW
  8. // @contributor darkred
  9. //
  10. // @released 2013-11-20
  11. // @updated 2017-10-20
  12. //
  13. // 17.10.19 Modified by darkred to work in the new userstyles.org layout.
  14. // 16.08.28 + keeps highliting status between sessions; 16.08.28.1 - some instead of each
  15. // 2.1.1 2016-01-04 + ctrl-clik copies CSS code to clipboard
  16. // 2.0.1 cut extra empty lines
  17. // 2.0 dark grey background;
  18. // 1.3 icons for toggle button; tiny optimization
  19. // 1.1 fresh versions of formater/hiliter; switching butifying on/off; fixed </& issue
  20. //
  21. // @include http://userstyles.org/styles/*
  22. // @include https://userstyles.org/styles/*
  23. // @include http://web.archive.org/web/20170222172435/https://userstyles.org/styles/118959/darksearch-for-google
  24. // @grant GM.setClipboard
  25. // @require https://greatest.deepsurf.us/scripts/21927-arrive-js/code/arrivejs.js?version=139586
  26. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  27. // ==/UserScript==
  28.  
  29. /* globals Prism, cssbeautify */
  30.  
  31. document.arrive('#button_middle > .css_button', function() {
  32.  
  33.  
  34.  
  35. (function() {
  36. 'use strict';
  37.  
  38.  
  39. var Light = 0,
  40. Dark = 1;
  41. // choose color scheme according to your taste:
  42. var scheme = 1; // 0 for light; 1 for dark
  43.  
  44. //function _L(s){console.log(s);}
  45. var sb;
  46.  
  47. if (location.href.indexOf('s.org/styles/') < 0 ||
  48. !(sb = document.querySelector('#button_middle > .css_button'))
  49. ) { return; } // else alert();
  50.  
  51.  
  52.  
  53.  
  54. var bt, tc, sc, vc, highlighted;
  55. var hiId = '<!--userstyles.org css highlighter-->';
  56.  
  57. // CSSBeautify (C) 2013 Sencha Inc. Author: Ariya Hidayat.
  58. (function(){function a(s,f){var h,m=0,i=s.length,t,e='',p,g,q,k,o,y,u,d,r=true,j=false,n;h=arguments.length>1?f:{};if(typeof h.indent==='undefined'){h.indent=' ';}if(typeof h.openbrace==='string'){r=(h.openbrace==='end-of-line');}if(typeof h.autosemicolon==='boolean'){j=h.autosemicolon;}function l(z){return(z===' ')||(z==='\n')||(z==='\t')||(z==='\r')||(z==='\f');}function b(z){return(z==='\'')||(z==='"');}function c(z){return(p>='a'&&p<='z')||(p>='A'&&p<='Z')||(p>='0'&&p<='9')||'-_*.:#[]'.indexOf(z)>=0;}function x(){var z;for(z=y;z>0;z-=1){e+=h.indent;}}function w(){e=n(e);if(r){e+=' {';}else{e+='\n';x();e+='{';}if(g!=='\n'){e+='\n';}y+=1;}function v(){var z;y-=1;e=n(e);if(e.length>0&&j){z=e.charAt(e.length-1);if(z!==';'&&z!=='{'){e+=';';}}e+='\n';x();e+='}';t.push(e);e='';}if(String.prototype.trimRight){n=function(z){return z.trimRight();};}else{n=function(z){return z.replace(/\s+$/,'');};}o={Start:0,AtRule:1,Block:2,Selector:3,Ruleset:4,Property:5,Separator:6,Expression:7,URL:8};y=0;k=o.Start;d=false;t=[];s=s.replace(/\r\n/g,'\n');while(m<i){p=s.charAt(m);g=s.charAt(m+1);m+=1;if(b(u)){e+=p;if(p===u){u=null;}if(p==='\\'&&g===u){e+=g;m+=1;}continue;}if(b(p)){e+=p;u=p;continue;}if(d){e+=p;if(p==='*'&&g==='/'){d=false;e+=g;m+=1;}continue;}if(p==='/'&&g==='*'){d=true;e+=p;e+=g;m+=1;continue;}if(k===o.Start){if(t.length===0){if(l(p)&&e.length===0){continue;}}if(p<=' '||p.charCodeAt(0)>=128){k=o.Start;e+=p;continue;}if(c(p)||(p==='@')){q=n(e);if(q.length===0){if(t.length>0){e='\n\n';}}else{if(q.charAt(q.length-1)==='}'||q.charAt(q.length-1)===';'){e=q+'\n\n';}else{while(true){g=e.charAt(e.length-1);if(g!==' '&&g.charCodeAt(0)!==9){break;}e=e.substr(0,e.length-1);}}}e+=p;k=(p==='@')?o.AtRule:o.Selector;continue;}}if(k===o.AtRule){if(p===';'){e+=p;k=o.Start;continue;}if(p==='{'){q=n(e);w();k=(q==='@font-face')?o.Ruleset:o.Block;continue;}e+=p;continue;}if(k===o.Block){if(c(p)){q=n(e);if(q.length===0){if(t.length>0){e='\n\n';}}else{if(q.charAt(q.length-1)==='}'){e=q+'\n\n';}else{while(true){g=e.charAt(e.length-1);if(g!==' '&&g.charCodeAt(0)!==9){break;}e=e.substr(0,e.length-1);}}}x();e+=p;k=o.Selector;continue;}if(p==='}'){v();k=o.Start;continue;}e+=p;continue;}if(k===o.Selector){if(p==='{'){w();k=o.Ruleset;continue;}if(p==='}'){v();k=o.Start;continue;}e+=p;continue;}if(k===o.Ruleset){if(p==='}'){v();k=o.Start;if(y>0){k=o.Block;}continue;}if(p==='\n'){e=n(e);e+='\n';continue;}if(!l(p)){e=n(e);e+='\n';x();e+=p;k=o.Property;continue;}e+=p;continue;}if(k===o.Property){if(p===':'){e=n(e);e+=': ';k=o.Expression;if(l(g)){k=o.Separator;}continue;}if(p==='}'){v();k=o.Start;if(y>0){k=o.Block;}continue;}e+=p;continue;}if(k===o.Separator){if(!l(p)){e+=p;k=o.Expression;continue;}if(b(g)){k=o.Expression;}continue;}if(k===o.Expression){if(p==='}'){v();k=o.Start;if(y>0){k=o.Block;}continue;}if(p===';'){e=n(e);e+=';\n';k=o.Ruleset;continue;}e+=p;if(p==='('){if(e.charAt(e.length-2)==='l'&&e.charAt(e.length-3)==='r'&&e.charAt(e.length-4)==='u'){k=o.URL;continue;}}continue;}if(k===o.URL){if(p===')'&&e.charAt(e.length-1!=='\\')){e+=p;k=o.Expression;continue;}}e+=p;}e=t.join('')+e;return e;}if(typeof exports!=='undefined'){module.exports=exports=a;}else{if(typeof window==='object'){window.cssbeautify=a;}}}());
  59. // Prism (c) Lea Verou, MIT License
  60. (function(){var e=/\blang(?:uage)?-(?!\*)(\w+)\b/i,t=self.Prism={util:{type:function(e){return Object.prototype.toString.call(e).match(/\[object (\w+)\]/)[1];},clone:function(e){var n=t.util.type(e);switch(n){case'Object':var r={};for(var i in e){e.hasOwnProperty(i)&&(r[i]=t.util.clone(e[i]));}return r;case'Array':return e.slice();}return e;}},languages:{extend:function(e,n){var r=t.util.clone(t.languages[e]);for(var i in n){r[i]=n[i];}return r;},insertBefore:function(e,n,r,i){i=i||t.languages;var s=i[e],o={};for(var u in s){if(s.hasOwnProperty(u)){if(u==n){for(var a in r){r.hasOwnProperty(a)&&(o[a]=r[a]);}}o[u]=s[u];}}return i[e]=o;},DFS:function(e,n){for(var r in e){n.call(e,r,e[r]);t.util.type(e)==='Object'&&t.languages.DFS(e[r],n);}}},highlight:function(e,r){return n.stringify(t.tkize(e,r));},tkize:function(e,n){var r=t.tk,i=[e],s=n.rest;if(s){for(var o in s){n[o]=s[o];}delete n.rest;}e:for(var o in n){if(!n.hasOwnProperty(o)||!n[o]){continue;}var u=n[o],a=u.inside,f=!!u.lookbehind||0;u=u.pattern||u;for(var l=0;l<i.length;l++){var c=i[l];if(i.length>e.length){break e;}if(c instanceof r){continue;}u.lastIndex=0;var h=u.exec(c);if(h){f&&(f=h[1].length);var p=h.index-1+f,h=h[0].slice(f),d=h.length,v=p+d,m=c.slice(0,p+1),g=c.slice(v+1),y=[l,1];m&&y.push(m);var b=new r(o,a?t.tkize(h,a):h);y.push(b);g&&y.push(g);Array.prototype.splice.apply(i,y);}}}return i;},hooks:{all:{},add:function(e,n){var r=t.hooks.all;r[e]=r[e]||[];r[e].push(n);},run:function(e,n){var r=t.hooks.all[e];if(!r||!r.length){return;}for(var i=0,s;s=r[i++];){s(n);}}}},n=t.tk=function(e,t){this.type=e;this.content=t;};n.stringify=function(e){if(typeof e==='string'){return e;}if(Object.prototype.toString.call(e)=='[object Array]'){return e.map(n.stringify).join('');}var r={type:e.type,content:n.stringify(e.content),tag:'span',classes:['tk',e.type],attributes:{}};t.hooks.run('wrap',r);var i='';for(var s in r.attributes){i+=s+'="'+(r.attributes[s]||'')+'"';}return'<'+r.tag+' class="'+r.classes.join(' ')+'" '+i+'>'+r.content+'</'+r.tag+'>';};})();
  61.  
  62. Prism.languages.css = {
  63. com: /\/\*[\w\W]*?\*\//g, // whose guilt?
  64. atr: /@[\w-]+?(\s+[^;{]+)?(?=\s*{|\s*;)/gi,
  65. url: /url\((["']?).*?\1\)/gi,
  66. sel: /[^{}\s][^{}]*(?=\s*\{)/g,
  67. pro: /(\b|\B)[a-z-]+(?=\s*:)/ig,
  68. str: /("|')(\\?.)*?\1/g,
  69. imp: /!important\b/gi,
  70. pun: /[{};:]/g
  71. };
  72.  
  73. /*
  74. .com comment, .atr at-rule, .sel selector, .imp !important,
  75. .pun punctuation, .url url, .str string
  76. */
  77. var prismCSS;
  78. if (scheme === Dark) {
  79. prismCSS = `
  80. .tk.com {
  81. color:#69A;
  82. }
  83. .tk.pun {
  84. color:#8FC;
  85. }
  86. .tk.pro {
  87. color:#5cf;
  88. }
  89. .tk.sel {
  90. color:#7F5;
  91. }
  92. .tk.str {
  93. color:#F63;
  94. }
  95. .tk.atr {
  96. color:#F8C;
  97. }
  98. .tk.imp {
  99. color:#e90;
  100. }
  101. .tk.url{
  102. color:#B87;
  103. }
  104. #css_text_area_div.hiBeautted {
  105. background-color:#293134 !important;
  106. color: #e0e2e4 !important;}
  107. `;
  108. } else {
  109. prismCSS = `
  110. .tk.com {
  111. color:slategray;
  112. }
  113. .tk.pun {
  114. color:#488;
  115. }
  116. .tk.pro {
  117. color:#05A;
  118. }
  119. .tk.sel {
  120. color:#470;
  121. }
  122. .tk.str {
  123. color:#E30;
  124. }
  125. .tk.atr {
  126. color:#905;
  127. }
  128. .tk.imp {
  129. color:#e90;
  130. /*font-weight:bold;*/
  131. }
  132. .tk.url{
  133. color:#a75;
  134. }
  135. `;}
  136.  
  137. prismCSS += `
  138. #css_text_area_div {
  139. padding: 1.2em .8em !important;
  140. }
  141. #css_text_area_div.hiBeautted {
  142. white-space: pre-wrap !important;
  143. word-wrap: break-word!important;
  144. }
  145. #hiBeauty:before{
  146. content:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAACXBIWXMAAC4jAAAuIwF4pT92AAACjklEQVR42pXTz0/TYBgH8Nf/gaMJhoAXZzx7NhJviiZ6wmiWXRbRxBjjSYEQN8PPjbGxsWwgOjQYCVHWQoQM2IYDYbCta1e6tfvRTRMVjffnsW9HFkGzuMPnfd68ab79vk1KfD7fYrlcxv8lCAJWKhWUZVnfh8PhXwaD4TLxer2LqqoCRR88vv/zjFIUBXmeh0P6fmRkhCETExOsWiyAkt6jMC+k6gb962Xj4+MM8Xg8bCnowwNrh+6bpQOLyS1s5Loul4shbrebLc874EdPO4V0FncjDTVyOp0M0Wqx6pshOHh0nsLv2izurDceRGup2yH4/KJH042VQB+ostRQ0NjYWDVISb7H3LK5aqULC1kO0+5V5B0hnbS0W/cb6UF0KcSeQeXdOQrplOPLIN5dwOw9BjQouSN1GzkcjmqQEu3D0tvWGnlnCfnbcyga56vsa3UbjY6OMoSmybFhkAInKZRmTkGe24BE5ywmb7wGDWac63Ub6UF0kQoShDNrFEbFMJTUEixnshjkJdBgUslDWlAgFN2nMLol/R1kt9uZQHoazbvGmlUxhmciGTTEsrr7cRGHn+fwVndRd/NxDmWlVLuazWYLEvqf+FNe6Ny6TiGdH4QItK0ksC3EwekQh12bPDz15uDaQ4XCqw+ykJVLtUYDAwMsGRwcZNx7TrgSuUQhnUv8OjQHN7F5cRs0aI4m4IlTgvY7OQovmvePBFkslgXS39/PxpUdnOYmdQFuGvOlPHr2BLTFeV1oX8bIpwL65xTdq6CiBeghKEkSmkymIWK1WllRFIHjOEilUphMpuAQHptHzhOJJIUbGx9/NjU1nSVGo2nW73/5dWpqpiGTk4Evvb3WtZaW1guEkBO/ARQgDsZ2Sv7VAAAAAElFTkSuQmCC);
  147. opacity:.75;
  148. display: block;
  149. position: absolute;
  150. top: 0px;
  151. left: 33%;
  152. z-index: 1;
  153. }
  154. #hiBeauty.hiBeautted:before{
  155. content:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAACXBIWXMAAC4jAAAuIwF4pT92AAACf0lEQVR42qWSW09aURCF9x9srJd6ARGqtf0nVUHkJreDtCrIRX/AjlFjQjUxISpXNdZ7DeC7rX1a0xm6TU6IjQ99+DIz+8xayVl7q06nox8eHoiBqb29fX5xRzxUu93WDJqNBj5NTeLjew8+TLgwOe6ExzEG9+gIXMNDcA4NwDHwFo16DbLfg1atVkszxMgBGey9fYbZ7e7beq3u7+81QxcXF9goFmmjWKD1Qh7r+TwVczkq5NaosJZFPpulfDaDXGaVcqsrtLayzH9RJ9GKh7q7u9MMzs7OsJxO4+vSEr6kUoyFtJXEUjKBVCKOVDwGKxZFMrrIRJBYjOD46AiiFQ91c3Ojb29viYGpvb19fnFHPNTV1ZW+vr7G4eEhpj3ubsgTHLBzaBDTbhfk22uIh+Js9OXlJTEwtbe3zy/uiIc6Pz/XDNVqNcmIOCOSjNKWRZwRcUYkGVnxGCX/ZkSNep1YA9EZtOKQNUOV42MYEzawOOQkG7BJPE5sAjahBAfNIVO1WiG5HNEZtGo2m/rk5AT/g3ioer2uGfpWKmHKPUGTrnHyOB1wj42Sa2SYnO8GyTHYj9H+PhrpeyOvXGIg1kB0Bq0qlYpmiEG1WiWDvbfP3fq8b6qgFV+7PuKHtb+/j0gwiHAwgFBgAaEFP4L+eQTmfQj4vFjweuH3zsE/N4v52Rn4Zj5jZ3sLohUPVS6XNUN7e3tImYAtvqVk95ZilOiGHAUHTXF+zbFImGLhEEXDIezsbJNoxUMdHBxohhiY2tvb53/taLW5uekrlUqPzE/mF/PE/H6FJ7MrmkfxUNlsdpcpZzKZU67fmR/ct7i2mY7A83OVs5bZkd1To939A6wgCBc2cNwFAAAAAElFTkSuQmCC);
  156. }
  157. #css_text_area_div {
  158. position: relative !important;
  159. }
  160. #hiBeauty {
  161. padding: 0;
  162. margin: 0;
  163. width: auto;
  164. cursor: pointer;
  165. border: none
  166. }
  167. `;
  168.  
  169. function hiStat(x) {
  170. vc.className = bt.className = x;
  171. bt.setAttribute('title', x ? 'raw text' : 'highlight');
  172. if (locStor) locStor.setItem('HiliteUsoCod', !!x);
  173. }
  174.  
  175. function isBeauty() {
  176. sc = document.getElementById('stylish-code');
  177. if (!sc || !(sc.textContent)) return -1;
  178. if (sc.innerHTML.substr(0, hiId.length) === hiId) {
  179. if (bt.className) {
  180. sc.innerHTML = '';
  181. sc.textContent = tc;
  182. hiStat('');
  183. } else hiStat('hiBeautted');
  184. return 1;
  185. }
  186. hiStat('');
  187. return 0;
  188. }
  189.  
  190. function hiBeauty(e) {
  191. sc = document.getElementById('stylish-code');
  192. if (e) {
  193. e.preventDefault(), e.stopPropagation();
  194. if (e.shiftKey || e.altKey) return;
  195. if (e.ctrlKey) { // 2016-01-04
  196. GM.setClipboard(sc.textContent);
  197. window.status = 'CSS copied to clipboard';
  198. return;
  199. }
  200. }
  201. if (isBeauty() !== 0) return;
  202. if (!(highlighted && tc && tc === sc.textContent)) {
  203. tc = sc.textContent;
  204. // console.log(tc);
  205. highlighted = cssbeautify(tc, {
  206. // autosemicolon: true ,
  207. // openbrace: "newline",
  208. indent: '\u0020\u0020' // 14-02-26 o_0
  209. // indent: " " // 14-02-26 o_0
  210. // indent: "\u00a0\u00a0"
  211. });
  212. // console.log(highlighted);
  213.  
  214. highlighted = highlighted.replace(/\n(\s*)@\s+/g, '\n$1@'); // 2014-03-12 dirty patch
  215. highlighted = highlighted.replace(/,\n\n+/g, ',\n'); // 2014-03-12 4freecyber
  216. highlighted = highlighted.replace(/\n\n+/g, '\n\n'); // 2014-02-25
  217. highlighted = highlighted.replace(/&/g, '&amp;').replace(/</g, '&lt;');
  218. highlighted = hiId +
  219. Prism.highlight(highlighted, Prism.languages.css) /* */ ;
  220. }
  221. // console.log(highlighted);
  222. sc.innerHTML = highlighted;
  223. hiStat('hiBeautted');
  224. }
  225.  
  226. /* * /
  227. function insAfter(n,e){
  228. if(e.nextElementSibling)
  229. return e.parentNode.insertBefore(n,e.nextElementSibling);
  230. return e.parentNode.appendChild(n);
  231. } /* */
  232.  
  233. function insBefore(n, e) {
  234. return e.parentNode.insertBefore(n, e);
  235. }
  236.  
  237. function cE(t, aA, eL, ht) {
  238. var n = document.createElement(t);
  239. for (var at in aA)
  240. if (aA.hasOwnProperty(at)) n.setAttribute(at, aA[at]);
  241. if (eL) n.addEventListener(eL[0], eL[1], eL[2] ? true : false);
  242. if (ht) n.innerHTML = ht;
  243. return n;
  244. }
  245.  
  246. function sbclik() {
  247.  
  248. if (document.querySelector('#stylish-code').outerHTML.indexOf('<textarea') !== -1) {
  249. document.querySelector('#stylish-code').outerHTML = document.querySelector('#stylish-code').outerHTML.replace('<textarea', '<code');
  250. document.querySelector('#stylish-code').style.whiteSpace = 'pre-wrap';
  251. document.querySelector('#stylish-code').style.display = 'block';
  252. document.querySelector('#stylish-code').style.width = '715px';
  253. document.querySelector('#stylish-code').style.overflowX = 'hidden';
  254. document.querySelector('#stylish-code').style.border = '1px';
  255. document.querySelector('#stylish-code').style.borderStyle = 'solid';
  256. document.querySelector('#stylish-code').style.borderColor = 'black';
  257. document.querySelector('#stylish-code').style.marginLeft = '-12px';
  258. }
  259.  
  260. if (!bt) {
  261. vc = document.getElementById('stylish-code');
  262. if (!vc) return;
  263. var pz = cE('style');
  264. pz.textContent = prismCSS;
  265. document.head.appendChild(pz);
  266. bt = cE('div', { id: 'hiBeauty', title: 'Highlight' }, ['click', hiBeauty], '');
  267. insBefore(bt, vc);
  268. }
  269. bt.className = '';
  270. //if(HiliteOn) alert('HiOn'),HiliteOn=0;
  271. }
  272.  
  273. sb.addEventListener('mousedown', sbclik, false);
  274.  
  275. var locStor, HiliteUsoCod = false;
  276. try { // localStorage throws 'security error' when cookies are disabled
  277. locStor = localStorage;
  278. HiliteUsoCod = locStor.getItem('HiliteUsoCod') === 'true';
  279. // see SQlite manager -> %FFpath%\webappstore.sqlite -> find -> key -> contains HiliteUso
  280. } catch (e) { locStor = false; }
  281.  
  282.  
  283.  
  284. document.arrive('#stylish-code', function() {
  285. var toggle = document.querySelector('#hiBeauty');
  286. if (toggle.title === 'Highlight' && localStorage.getItem('HiliteUsoCod') === 'true') {
  287. toggle.click();
  288. }
  289. });
  290.  
  291.  
  292. document.querySelector('.css_button').addEventListener('click', function() {
  293. if (document.querySelector('.css_button').firstChild.className === 'css_open') {
  294. document.querySelector('#stylish-code').style.display = 'none';
  295. document.querySelector('#hiBeauty').style.display = 'none';
  296. } else {
  297. document.querySelector('#stylish-code').style.display = 'block';
  298. document.querySelector('#hiBeauty').style.display = 'initial';
  299. }
  300. });
  301.  
  302.  
  303.  
  304.  
  305. })();
  306.  
  307.  
  308.  
  309. });