Page is a not externally linkable
Solution1 - 2:09 pm on Jan 13, 2013 (gmt 0)
I wrote this code (adapting from various sources on the web) to make what I described in my previous post (more or less). It should work fine in Internet Explorer 9 and higher, and latest versions of Firefox, Chrome, Opera, Safari for Windows.
The article is expected to be within ARTICLE tags.
The description meta tag should have the word "description" in lower case, or else the alternative message will show.
<style>
#copymsg {
display: none;
}
</style>
<body oncopy="mentionCopyright();">
<div id="copymsg">© Example.com 2013</div>
<script type="text/javascript">
function mentionCopyright()
{
document.getElementById('copymsg').style.display='block';
// Prepare page link and copyright message, to append to copied content
var pageurl = document.location.href.match(/https?:\/\/(.+)/)[1];
var pagelink = "<br>Read more at: <a href='"+document.location.href+"'>"+pageurl+"</a><br>Copyright © Example.com 2013";
if (window.getSelection)
{
// Get selected part of the article
var selection = window.getSelection();
var selectionContents = selection.getRangeAt(0).cloneContents();
var selectionSize = selection.toString().length;
// Check if contents of more than one HTML element is copied
var container = selection.getRangeAt(0).commonAncestorContainer;
if (container.nodeName == "ARTICLE")
{
// Helper DIV
var newdiv = document.createElement('div');
newdiv.style.position='absolute';
newdiv.style.left='-99999px';
var body_element = document.getElementsByTagName('body')[0];
body_element.appendChild(newdiv);
var newpar=document.createElement("p");
newpar.innerHTML = pagelink;
// Check whether more than just a quote is copied
if (selectionSize < 900)
newdiv.appendChild(selectionContents);
else {
// Replace copied part with contents of description meta tag
var desc;
var descs = document.getElementsByName('description')[0];
if (typeof descs !== 'undefined')
desc = descs.getAttribute('content');
else
desc = "Please copy no more than a quote from this article ('fair use' as to international copyright laws).";
var newcontent=document.createElement("p");
newcontent.innerHTML = desc;
newdiv.appendChild(newcontent);
}
newdiv.appendChild(newpar);
selection.selectAllChildren(newdiv);
window.setTimeout(function() {
body_element.removeChild(newdiv);
},0);
}
}
}
</script>