Skip to content

Instantly share code, notes, and snippets.

@LeonardoCiaccio
Last active August 2, 2017 14:28
Show Gist options
  • Select an option

  • Save LeonardoCiaccio/75012abcae1f61c58944243195dabb68 to your computer and use it in GitHub Desktop.

Select an option

Save LeonardoCiaccio/75012abcae1f61c58944243195dabb68 to your computer and use it in GitHub Desktop.
Aggiunge un attributo univoco a seconda delle opzioni
/*
Version : 1.0.0
Aggiunge un attributo univoco dinamico a seconda delle opzioni, molto utile per fare
una istantanea della pagina e avere riferimenti solidi :
window._id( {
selector : "signature" // --> default "_id"
,main : "body" // --> default "html"
,allowed : [ "div", "span" ] // --> default []
,ignored : [] // --> default []
,onend : function(){ console.log( "END" ); } // --> function(){}
} );
In questo esempio tutti i "div" e i "span" contenuti nel tag "body" otterranno
in aggiunta un nuovo attributo con nome "signature" e valore univoco.
L'opzione "ignored" possiede una priorità maggiore di "allowed", se avessimo settato
questa opzione a "[ 'span' ]" tutti gli "span" non avrebbero ottenuto i nuovi attributi
anche se presenti in "allowed"
COMPRESSO :
!function(){"use strict";var e={selector:"_id",main:"html",allowed:[],ignored:[],onend:function(){}},r=function(e){return"string"==typeof e?e.toUpperCase():e};Array.isArray||(Array.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)}),window._id=function(n){var t=JSON.parse(JSON.stringify(e));n&&("string"==typeof n.selector&&(t.selector=n.selector),"string"==typeof n.main&&(t.main=n.main),Array.isArray(n.allowed)&&(t.allowed=n.allowed),Array.isArray(n.ignored)&&(t.ignored=n.ignored),"function"==typeof n.onend&&(t.onend=n.onend));for(var o=0;o<t.allowed.length;o++)t.allowed[o]=r(t.allowed[o]);for(var o=0;o<t.ignored.length;o++)t.ignored[o]=r(t.ignored[o]);var i=document.querySelector(t.main);if(!i)return t.onend();for(var a=document.querySelectorAll("*"),l=0;l<a.length;l++)a[l].removeAttribute(t.selector);a=[],a=i.getElementsByTagName("*");for(var d=0,l=0;l<a.length;l++)t.allowed.indexOf(a[l].tagName.toUpperCase())<0||t.ignored.indexOf(a[l].tagName.toUpperCase())>-1||a[l].setAttribute(t.selector,d++);t.onend()}}();
*/
( function(){
"use strict";
// --> Varibili globali
var
_options = {
//--> Il nome del selettore univoco
selector : "_id"
// --> Il selettore che contiene tutti gli elementi da segnare
,main : "html"
// --> Tags consentiti
,allowed : []
// --> Ignora questi tag, filtra
,ignored : []
// --> Evento generato alla fine della routin
,onend : function(){}
}
,_toUpper = function( x ){
return ( typeof x === "string" ) ? x.toUpperCase() : x;
}
;
// --> Controllo array nativo, se è un array
if ( !Array.isArray ){
Array.isArray = function( arg ){
return ( Object.prototype.toString.call( arg ) === "[object Array]" );
};
}
// --> L'oggetto _id
window._id = function( opts ){
// --> Clono le opzioni di base ed eventualmente lo aggiorno
var myopts = JSON.parse( JSON.stringify( _options ) );
if( opts ){
if( typeof opts.selector === "string" )myopts.selector = opts.selector;
if( typeof opts.main === "string" )myopts.main = opts.main;
if( Array.isArray( opts.allowed ) )myopts.allowed = opts.allowed;
if( Array.isArray( opts.ignored ) )myopts.ignored = opts.ignored;
if( typeof opts.onend === "function" )myopts.onend = opts.onend;
}
// --> I consentiti in maiuscolo
for( var i = 0; i < myopts.allowed.length; i++ ){
myopts.allowed[ i ] = _toUpper( myopts.allowed[ i ] );
}
// --> Il filtro in maiuscolo
for( var i = 0; i < myopts.ignored.length; i++ ){
myopts.ignored[ i ] = _toUpper( myopts.ignored[ i ] );
}
// --> Seleziono il contenitore (main)
var main = document.querySelector( myopts.main );
if( !main )return myopts.onend();
// --> Rimuovo tutte le signature
var all = document.querySelectorAll( "*" );
for( var x = 0; x < all.length; x++ ){
all[ x ].removeAttribute( myopts.selector );
}
// --> Segno ogni singolo elemento che contiene
all = [];
all = main.getElementsByTagName( "*" );
var signed = 0;
for( var x = 0; x < all.length; x++ ){
if( myopts.allowed.indexOf( all[ x ].tagName.toUpperCase() ) < 0 )continue;
if( myopts.ignored.indexOf( all[ x ].tagName.toUpperCase() ) > -1 )continue;
all[ x ].setAttribute( myopts.selector, signed++ );
}
myopts.onend();
}; // <-- window._id()
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment