Shoora
4/6/2019 - 7:31 PM

copyenrich-filter

copyenrich-filter


/**
 * 
 * Default filter to prepend and append Text.
 * 
 */
(function( window ) {

    // create filter for prefix and postfix
    window.filter_prefix_postfix = function( selection ) {    
	
	// load filter configuration
	var copyenrich = window.copyenrich || {};
	var clipboard_prefix = copyenrich.filter_prefix || '';
	var clipboard_postfix = copyenrich.filter_postfix || '';   
	
	return clipboard_prefix + selection + clipboard_postfix;
    };

})( window );

/**
 * 
 * Minimal length required to perform text enrichment.
 * If the minimum length is not reached, function will return FALSE
 * which results in cancelation of all changes made and return the 
 * originally selected text.
 * 
 */
(function( window ) {
    
    window.filter_minlength = function( selection ) {

	// load filter configuration
	var copyenrich = window.copyenrich || {};
	var minlength = copyenrich.filter_minlength || 20;

	if ( selection.length < minlength ) {
	    // Boolean FALSE means that no changes will be done at all.
	    return false;
	};

	return selection;
    };

})( window );

/**
 * 
 * User defined filters. They can be loaded from external file.
 * 
 */
(function( window ) {
    
    window.filter_source_url = function( selection ) {    
	
	// load filter configuration
	var copyenrich = window.copyenrich || {};
	var postfix = copyenrich.filter_source_url || '';	
	
	return selection + postfix + location.href;
    };

})( window );

/**
 *
 * Tracks the copied text with Google Analytics.
 * 
 */
(function( window ) {    
        
    window._gaq = window._gaq || [];
    window.filter_analytics = function( selection ) {
	// load filter configuration
	var copyenrich = window.copyenrich || {};
	var track_name = copyenrich.filter_analytics_name;
	var track_value = copyenrich.filter_analytics_value;
	
	window._gaq.push(['_trackEvent', track_name, track_value, selection]);
	// return selection without modification
	return selection;
    };

})( window );

/**
 * 
 * Inserts an Ad to the selection if certain words have been selected.
 * Context based Ad insetion.
 * 
 */
(function( window ) {
    
    window.filter_wordmatch_ad = function( selection ) {
	
	// load filter configuration
	var copyenrich = window.copyenrich || {};
	// if these words are inside the selection, AdText will be inserted
	var signal_words = copyenrich.filter_wordmatch_ad_signal_words || [ ];
	var ad_text = copyenrich.filter_wordmatch_ad || '';
	
	// try to match words inside selected text
	var word = '';
	var word_match = false;
	
	var split = selection.split( ' ' );
	for( var w = 0; w < split.length; w++ ) {
	    word = split[w].trim( ).replace( /[\.,\-;:\n\r]+/gi, '' );

	    for( var sw = 0; sw < signal_words.length; sw++ ) {
		if ( signal_words[sw] == word ) {
		    word_match = true;
		}
	    }
	    
	    if ( word_match === true ) {
		return selection + ad_text;
	    }
	}

	return selection;
    };

})( window );

/**
 * 
 * Enables script if at least one keyword is inside the selected text.
 * 
 */
(function( window ) {
    
    window.filter_wordmatch_enabled = function( selection ) {

	// load filter configuration
	var copyenrich = window.copyenrich || {};
	// if these words are inside the selection, script will be enabled
	var signal_words = copyenrich.filter_wordmatch_enabled_signal_words || [ ];	

	// try to match words inside selected text
	var word = '';
	var word_match = false;
	
	var split = selection.split( ' ' );
	for( var w = 0; w < split.length; w++ ) {
	    word = split[w].trim( ).replace( /[\.,\-;:\n\r]+/gi, '' );

	    for( var sw = 0; sw < signal_words.length; sw++ ) {
		if ( signal_words[sw] == word ) {
		    word_match = true;
		}
	    }	    
	}

	// if signal words not found, disable all changes made by the script
	if ( word_match !== true ) {
	    // Boolean FALSE means that no changes will be done at all.
	    return false;
	}

	return selection;
    };

})( window );
<script src="copyenrich.js"></script>
<script src="copyenrich-filter.js"></script>
<script>
    var copyenrich = {
	 filter_prefix : 'Copy from performics.de:\n\n'
	,filter_postfix : '\n\nFollow us on Twitter: @performics'
	
	,filter_minlength: 40
	
	,filter_source_url: '\n\nFound on:'
	
	,filter_analytics_name: "Copy on page"
	,filter_analytics_value: "Content"
	
	,filter_wordmatch_ad_signal_words: [ 'Bus', 'Reisebus', 'Fernreise', 'Bahnticket' ]
	,filter_wordmatch_ad: '\n\n' +
			'****************************************************\n' +
			'* Günstig und entspannt von A nach B mit TURBOBUS! *\n' +
			'* Sonderangebot hier und jetzt: http://turbobus.de *\n' +
			'****************************************************\n'
		
	,filter_wordmatch_enabled_signal_words: [ 'Bus', 'Reisebus', 'Fernreise', 'Bahnticket' ]
    }; 

    // initialize
    var copyenrichFilters = copyenrichFilters || [];

    copyenrichFilters.push([ filter_minlength ]);    
    copyenrichFilters.push([ filter_wordmatch_enabled ]);   
    copyenrichFilters.push([ filter_wordmatch_ad ]);    
    copyenrichFilters.push([ filter_analytics ]);
    copyenrichFilters.push([ filter_source_url ]);
    copyenrichFilters.push([ filter_prefix_postfix ]);
 
</script>

/**
    // Copyright (c) 2014 AKM3 GmbH
    // Copyright (c) 2016 Performics.de
    // http://www.performics.de
    // @Author Erich Kachel
    // @Version v1.4
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    // in the Software without restriction, including without limitation the rights
    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    // copies of the Software, and to permit persons to whom the Software is
    // furnished to do so, subject to the following conditions:
    //
    // The above copyright notice and this permission notice shall be included in
    // all copies or substantial portions of the Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    // THE SOFTWARE.
 */
(function( d, w ) {
    
    // Version
    w.version = '1.4';

    // preparing filter array where filter calbacks are stored
    w.copyenrichFilters = w.copyenrichFilters || [];
    
    // register "copy" event
    if ( w.addEventListener ) {
	d.body.addEventListener( "copy", intercept_clipboard, true );		
    }		

    /**
     * Creates helper element, copies selected and modified text inthere
     * and creates new selection with the new text.
     */
    function intercept_clipboard( ) {

	var clipboard_text = get_selection( );
	var modified_clipboard_text = clipboard_text;
	
	// Running filters on selection. Here all changes of the selected text are done.
	// To stop filtering and restore original selection, any filter
	// may return (boolean) FALSE.
	
	if ( w.copyenrichFilters.length > 0 ) {
	    // running filters
	    for( var f = 0; f < w.copyenrichFilters.length; f++ ) {
		try {
		    modified_clipboard_text = w.copyenrichFilters[f][0]( modified_clipboard_text );
		    console.log( 'Copyenrich executing filter: ' + f );
		    // breaking condition: returning (bool) FALSE means that no changes should be done at all
		    if ( modified_clipboard_text === false ) {			
			// Remove helper element 
			w.setTimeout( hide_clipboard_interceptor, 200 );		
			console.log( 'Copyenrich: filter triggered reset. No changes done.' );
			// terminate here, undo all changes. filter told us to do so.
			return;
		    }
		    
		} catch ( err ) {
		    console.log( '! Copyenrich filter caused an error. Current filter was: ' + w.copyenrichFilters[f][0] );
		}
	    }
	}

	// Creates helper element outside the view
	var top = ( w.pageYOffset || d.scrollTop );
	var div = d.createElement( 'span' );
	div.id = 'clipboard_interceptor';
	div.style.position = 'absolute';
	div.style.top = ( typeof top == 'undefined' ? 0 : top ) + 'px';
	div.style.left = '-300px';
	div.style.overflow = 'hidden';
	div.style.width = '100px';
	div.style.height = '100px';
	d.body.appendChild( div );
	
	// Modifies clipboard text and preselects it to be copied 
	if ( navigator.userAgent.indexOf( 'Firefox' ) > 0  ) {
	    div.innerHTML = modified_clipboard_text.replace( /\n/g, '<br>' );
	} else {
	    // Creates textarea to store clipboard text
	    var textarea = d.createElement( 'textarea' );
	    div.appendChild( textarea );
	    
	    textarea.innerHTML = modified_clipboard_text;
	}
	
	// Selects modified hidden text to be copied instead of original selection
	if ( navigator.userAgent.indexOf( 'Firefox' ) > 0  ) {
	    var sel = w.getSelection( );
	    if( sel.rangeCount > 0 ) sel.removeAllRanges( );
	    var range = d.createRange( );
	    range.selectNode( div );
	    sel.addRange( range );	    
	} else {
	    textarea.select( );
	}
	console.log( 'Copyenrich: done.' );
	// Remove helper element 
	w.setTimeout( hide_clipboard_interceptor, 200 );
    }

    /**
     * Gets selected Text
     */
    function get_selection( ) {

	var sel = w.getSelection( );
	var selected_text = sel.toString( );

	return selected_text;
    }

    /**
     * Removes helper element
     */
    function hide_clipboard_interceptor() {
	var c = d.getElementById( 'clipboard_interceptor' );
	c && c.parentNode.removeChild( c );
    }

})( document, window );