// source --> https://handwerkszeug.net/wp-content/plugins/blogreach/assets/public/script.js?ver=6.9.4 
// The css mark positioning.
function jsoutput() {
    window.addEventListener( 'load', function() {
        var mark = '<span class="blogreachmark">' + settings.post_meta['blogreach_custom_mark'] + '</span>';
        var legend = '<span class="blogreachlegend">' + settings.post_meta['blogreach_custom_legend'] + '</span>';
        if ( settings.options['blogreach_positions_after_css'] ) {
            if ( settings.options['blogreach_positions_after_css_selector'] ) {
                
                // Add the mark to the selected css.
                if ( settings.post_meta['blogreach_custom_mark'] ) {
                    var selectors = document.querySelectorAll( settings.options['blogreach_positions_after_css_selector'] );
                    for ( var i = 0; i < selectors.length; i++ ) {
                        selectors[i].innerHTML += mark;
                    }
                }

                // Add the legend direct after the mark.
                if ( settings.options['blogreach_positions_after_mark'] ) {
                    var selectors = document.querySelectorAll( settings.options['blogreach_positions_after_css_selector'] );
                    for ( var i = 0; i < selectors.length; i++ ) {
                        selectors[i].innerHTML += legend;
                    }
                }
            }
        }
    }, false );
}

// Set the links to nofollow.
var nofollow_domains = [];
function setlinkrel() {
    window.addEventListener( 'load', function() {
        var links = document.getElementsByTagName( 'a' );
        var domains = settings.options['blogreach_nofollow_domains'];
        var noreferrer = settings.options['blogreach_noreferrer'];

        // Identify the current link rel.
        function identifyrel() {
            if ( links[i].hasAttribute( 'rel' ) ) {
                // Remove the rel noreferrer attribute if wanted.
                if ( noreferrer === 'blogreach_noreferrer' ) {
                    if ( links[i].attributes.rel.value.search( /noreferrer/i ) !== -1 ) {
                        links[i].setAttribute( 'rel', links[i].getAttribute( 'rel' ).replace( /noreferrer/i, '' ) );
                    }
                }

                // Only when nofollow exists in rel attributes.
                if ( links[i].attributes.rel.value.search( /nofollow/i ) !== -1 ) {
                    return links[i].getAttribute( 'rel' ).trim();
                }

                // Return the existing rel attributes and add nofollow to it.
                return links[i].getAttribute( 'rel' ).trim() + ' nofollow';
            }
            // If no rel attribute exists, return it and create it than.
            else { return 'nofollow'; }
        }
        
        // Loop through all links.
        for ( var i = 0; i < links.length; i++ ) {
            // Mark all external links with rel nofollow if the star selector is activated.
            if ( domains.includes( '*' ) ) {

                // Set the rel attribute only for externel links.
                if ( links[i].hostname.includes( window.location.hostname ) !== true ) {
                    links[i].setAttribute( 'rel', identifyrel() );
                    window.nofollow_domains.push( links[i] );
                }
            }

            // Other set the specific domain as rel nofollow.
            else if ( domains.indexOf( links[i].hostname ) !== -1 ) {
                links[i].setAttribute( 'rel', identifyrel() );
                window.nofollow_domains.push( links[i] );
            }
        }
    }, false );
}

// Add the link label to affiliate links based on there tags.
var link_labeling_links = [];
function setaffiliatelinks() {
    window.addEventListener( 'load', function() {
        var links = document.getElementsByTagName( 'a' );
        var aff_tags = settings.options['blogreach_link_labeling'].split (', ');
        for ( var i = 0; i < links.length; i++ ) {
            for ( var x = 0; x < aff_tags.length; x++ ) {
                if ( links[i].href.indexOf( aff_tags[x] ) != -1 ) {
                    links[i].innerHTML += '<span class="blogreachlabel">' + settings.options['blogreach_link_label'] + '</span>';
                    window.link_labeling_links.push( links[i] );
                }
            }
        }
    }, false );
}

// Support mode function.
function supportmode() {
    window.addEventListener( 'load', function() {
        console.log( 'Blogreach options:' );
        console.log( settings.options );
        console.log( 'Nofollow domains:' );
        console.log( window.nofollow_domains ); 
        console.log( 'Link labeling links:' );
        console.log( window.link_labeling_links );
        console.log( 'Blogreach post meta:' );
        console.log( settings.post_meta );
        console.log( 'Blogreach labeling metas:' );
        console.log( settings.posts_labeled );
    }, false );
};