jQuery CssDynamic

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

Versione datata 16/01/2017. Vedi la nuova versione l'ultima versione.

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greasyfork.org/scripts/26587/169674/jQuery%20CssDynamic.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @author         Matteo Burbui @maxeo90
// @name           jQuery CssDynamic
// @description    A dinamic system to customize style css. It append the rules in the end of body
// @license        https://creativecommons.org/licenses/by-sa/4.0/
// @version        1.1
// @grant          none
// ==/UserScript==
(function( $ ){
   $.fn.cssDynamic = function(prop) {
     var selector=$(this).selector;
     var data,fval,action,idStyle,newName;
     
     if(typeof(prop)=='object'){
      fval=1,data='';
      for (var k in prop){
        if (prop.hasOwnProperty(k)) {
          data+=k+':'+prop[k]+';'
        }
      }
     }else if(arguments[1]!=undefined){
       fval=2;
       data=arguments[0]+':'+arguments[1]
     }else if(prop=='remove' || (prop=='rename' && newName=arguments[2]!=undefined)){
       fval=0;
     } else{return 'error'}
     action=(arguments[fval]==undefined)?'add':arguments[fval];
     idStyle=(arguments[fval+1]==undefined)?'cssDynamic':arguments[fval+1];
     
     switch(action) {
      case 'new':
        actNew()
        break;
      case 'add':
        actAdd()
        break;
      case 'remove':
        actRemove()
        break;
      case 'renew':
        actRemove()
        actNew()
        break;
      case 'rename':
        actRemove()
        actNew()
        break;
     }
     function chData(){
       data=selector+'{'+data+'}'
     }
     function actNew(){
       chData();
       $('body').append('<style id="'+idStyle+'">'+data+'</style>')
     }
     function actAdd(){
       if(!$('#'+idStyle).length){
         actNew()
       }else{
         chData();
         $('style#'+idStyle).append(data)
       }
     }
     function actRemove(){
       $('style#'+idStyle).remove();
     }
     
     function actRename(){
       $('style#'+idStyle).attr('id',newName);
     }
      return this;
   }; 
})( jQuery );