JSFiddle - React, Tailwind, and code Playground

by leonardo_ciaccio

HTML

<div>
    <p>
        Questo è un testo di prova <a href="#go">Vai</a>
    </p>
    <p>
        non lo so questo è un altro testo in un paragrafo di testo
    </p>
</div>
<div class="response"></div>

JavaScript

/*

    Coded by Leonardo Ciaccio
    
    App : Conta la densita delle parole nella pagina, restituisce solo i candidati
    

*/
( function(){
    
    var words      = {},
        min        = 3,
        minresult  = 2,
        maxdisplay = 5;

    function $( selector ){
    
        return document.querySelector( selector );
        
    };
    
    $.grep = function( items, callback ){
        
        var filtered = [],
            len      = items.length,
            i = 0;
        
        for( i; i < len; i++ ){
                        
            if( callback( items[ i ] ) )filtered.push( items[ i ] );
        
        }

        return filtered;
    
    };

    function _countWords(){
        
        // Prelevo tutte le parole
        var finder = new RegExp( "[a-zA-Z0-9]{" + min + ",}\\S", "gi" ),
            all    = document.body.innerText.match( finder );
        
        if( !all )return false;
               
        // Contiamo le parole
        for( var i = 0; i < all.length; i++ ){
            
            var ident = all[ i ].toLowerCase();
            words[ ident ] = ( !words[ ident ] ) ? 1 : words[ ident ] + 1 ;
            
        }
        
        // Le ordiniamo in ordine decrescente
        var sortable = [];
        for( var word in words ){
            
            sortable.push( [ word, words[ word ] ] );
        }
        
        sortable.sort( function( a, b ){ return b[ 1 ] - a[ 1 ] } );
        
        var sortable = $.grep( sortable, function( word ){
        
            return ( word[ 1 ] >= minresult );
        
        } );
        var candidate = "Words density :\r\n";
        for( i = 0; i < maxdisplay && i < sortable.length; i++ ){
            
            candidate+= sortable[ i ].join( " = " ) + "\r\n";
            
        }
        
        alert( candidate );
        
    };
    
    _countWords();

} )();

/*

    Minificato per bookmarklets
    
(function(){var...