jQuery CssDynamic

A dinamic system to customize style css. It append the rules in the end of body

As of 16/01/2017. See the latest version.

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/26587/169674/jQuery%20CssDynamic.js

  1. // ==UserScript==
  2. // @author Matteo Burbui @maxeo90
  3. // @name jQuery CssDynamic
  4. // @description A dinamic system to customize style css. It append the rules in the end of body
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @version 1.1
  7. // @grant none
  8. // ==/UserScript==
  9. (function( $ ){
  10. $.fn.cssDynamic = function(prop) {
  11. var selector=$(this).selector;
  12. var data,fval,action,idStyle,newName;
  13. if(typeof(prop)=='object'){
  14. fval=1,data='';
  15. for (var k in prop){
  16. if (prop.hasOwnProperty(k)) {
  17. data+=k+':'+prop[k]+';'
  18. }
  19. }
  20. }else if(arguments[1]!=undefined){
  21. fval=2;
  22. data=arguments[0]+':'+arguments[1]
  23. }else if(prop=='remove' || (prop=='rename' && newName=arguments[2]!=undefined)){
  24. fval=0;
  25. } else{return 'error'}
  26. action=(arguments[fval]==undefined)?'add':arguments[fval];
  27. idStyle=(arguments[fval+1]==undefined)?'cssDynamic':arguments[fval+1];
  28. switch(action) {
  29. case 'new':
  30. actNew()
  31. break;
  32. case 'add':
  33. actAdd()
  34. break;
  35. case 'remove':
  36. actRemove()
  37. break;
  38. case 'renew':
  39. actRemove()
  40. actNew()
  41. break;
  42. case 'rename':
  43. actRemove()
  44. actNew()
  45. break;
  46. }
  47. function chData(){
  48. data=selector+'{'+data+'}'
  49. }
  50. function actNew(){
  51. chData();
  52. $('body').append('<style id="'+idStyle+'">'+data+'</style>')
  53. }
  54. function actAdd(){
  55. if(!$('#'+idStyle).length){
  56. actNew()
  57. }else{
  58. chData();
  59. $('style#'+idStyle).append(data)
  60. }
  61. }
  62. function actRemove(){
  63. $('style#'+idStyle).remove();
  64. }
  65. function actRename(){
  66. $('style#'+idStyle).attr('id',newName);
  67. }
  68. return this;
  69. };
  70. })( jQuery );