php - Shorten copied text with javascript to protect blog from copying articles -


i have added code functions.php of wordpress blog:

function add_copyright_text() {     if (is_single()) { ?>  <script type='text/javascript'> function addlink() {     if ( window.getselection().containsnode( document.getelementsbyclassname('post')[0], true)) {     var body_element = document.getelementsbytagname('body')[0];     var selection;     selection = window.getselection();     var oldselection = selection;     var pagelink = "<br /><br /> read full forex article here: <?php the_title(); ?> <a href='<?php echo get_permalink(get_the_id()); ?>'><?php echo get_permalink(get_the_id()); ?></a>"; //change if     var copy_text = selection + pagelink;     var new_div = document.createelement('div');     new_div.style.left='-99999px';     new_div.style.position='absolute';       body_element.appendchild(new_div );     new_div.innerhtml = copy_text ;     selection.selectallchildren(new_div );     window.settimeout(function() {         body_element.removechild(new_div );     },0); } }   document.oncopy = addlink; </script>  <?php } }  add_action( 'wp_head', 'add_copyright_text'); 

it works fine, want shorten copied code.

i replace line

var oldselection = selection; 

to

var oldselection = selection.tostring().substr(0,500); 

but still copies full length articles. can please me modify code in order shorten text well?

br

you're shortening old_selection, not used way think is.

you need shorten selection, after it's been copied old_selection, so:

selection = window.getselection(); var oldselection = selection; selection = selection.tostring().substr(0,500); 

later, selection used populate new div, why need shorten that variable, not old_selection.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -