function wordTrim(value, length, overflowSuffix) { 
    if (value.length <= length) return value;
    var strAry = value.split(' ');
    var retLen = strAry[0].length;
    for (var i = 1; i < strAry.length; i++) {
        if(retLen == length || retLen + strAry[i].length + 1 > length) break;
        retLen+= strAry[i].length + 1
    }
    return strAry.slice(0,i).join(' ') + (overflowSuffix || '');
}


function addLink(event) {
	event.preventDefault();

	var pageUrl= window.location.href;
            if (pageUrl.indexOf("?")>-1){
		pageUrl = pageUrl .substr(0,pageUrl .indexOf("?"));
	}


	var pagelink = '... Read more at: ' + pageUrl ; 
        var copytext =  window.getSelection()+"  ";

	if(copytext != 'undefined'){
              text = wordTrim(copytext,200,pagelink );
              event.clipboardData.setData('text/plain',text);
           }
}

document.addEventListener('copy', addLink);


