Skip to content

Instantly share code, notes, and snippets.

@plfstr
Created August 19, 2013 22:39
Show Gist options
  • Save plfstr/6275052 to your computer and use it in GitHub Desktop.
Save plfstr/6275052 to your computer and use it in GitHub Desktop.
Much Content? - Script which displays a percentage of page content vs comments.
function getOffset()
{
var commentsList = ['comments', 'discussion', 'reply', 'notes', 'commentarea', 'disqus_thread', 'fb-comment-container', 'lf_comment_stream', 'respond', 'response', 'CommentList', 'comments-list', 'start-of-comments', 'article-comments', 'echo-stream', 'vanilla-comments', 'cboxid']; //If some comments are not being picked up, append their #ID to this array.
var i = 0,
commentsLength = commentsList.length,
getViewheight = window.innerHeight, //The height of the browser viewport
getTotalheight = document.body.scrollHeight,
percentStyle,
percentBox;
do
{
gotComment = document.getElementById(commentsList[i]);
var thisComment = commentsList[i];
i++
}
while (gotComment===null && i < commentsLength); // Dom search condition is false, keep looking. It wont find anything in some cases, dont loop forever!
if (gotComment != null){
var getCommenttop = document.getElementById(thisComment).offsetTop; //Position of top of comments
}
else {
return; // You found nothing so stop
}
if(getTotalheight > getViewheight) {
var getFraction = getCommenttop/getTotalheight;
}
else {
return; // Comments visibile in viewport
}
var getRatio = Math.round(getFraction*100);
//Create somewhere to display this info
percentStyle = document.createElement('style');
percentStyle.type = 'text/css';
percentStyle.innerHTML = '#muchcontent {position:fixed;top:55px;right:15px;display:block;z-index:189;padding:5px 10px;font:normal 16px/1.5 sans-serif !important;color:#eee;background:rgb(185,185,185);background:rgba(90,90,90,0.3);border-radius:.5em;text-shadow:2px 1px 1px #444;-webkit-transition:opacity .7s ease;-moz-transition:opacity .7s ease;-o-transition:opacity .7s ease;transition:opacity .7s ease;opacity:1}';
document.head.appendChild(percentStyle);
percentBox = document.createElement('div');
percentBox.setAttribute('id', 'muchcontent');
percentBox.innerHTML = getRatio +'\u0025';
document.body.appendChild(percentBox);
function removePercentbox()
{
document.getElementById('muchcontent').style.opacity = "0";
setTimeout(
function(){
document.body.removeChild(percentBox);
document.head.removeChild(percentStyle);
}, 700); //unload the generated elements after 700 miliseconds
return;
}
setTimeout(removePercentbox, 4000);
return;
}
window.onload = getOffset;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment