Skip to content

Instantly share code, notes, and snippets.

@HarryMcCarney
Created January 14, 2015 11:18
Show Gist options
  • Save HarryMcCarney/0102172d7e474e2b5d20 to your computer and use it in GitHub Desktop.
Save HarryMcCarney/0102172d7e474e2b5d20 to your computer and use it in GitHub Desktop.
Inline Widget Placement
// ==UserScript==
// @name PlacementCode
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
function autoPlaceWidget(widget){
var $root = $( '.entry-content,.entry-inner,.entry,#article,.sociable,.post_text,.post-content,.td-post-text-content,#match-report-left,.post-entry,#article_content,.pf-content' );
if(!$root.length){return widget;}
var series = [], minBorder= 8, $nodes = $root.find('p,script,iframe,ins,center');
$nodes.each(function(idx, elem, arr){series.push(elem.innerText.split(' ').length);});
var startIndex = 0, len = 0, maxIdx = 0, maxLen = 0;
$.each(series, function(idx, elem){
if(elem>=minBorder){
if(len===0)startIndex = idx;
len += 1;
} else {
if(maxLen<3 && len>=maxLen){maxIdx = startIndex;maxLen = len;}
len = 0;startIndex = 0;
}
});
if(maxIdx===0&&maxLen<3){maxIdx = startIndex;maxLen = len;}
var $target = $nodes.eq( maxIdx+Math.floor(maxLen/2) );
$target.parents().each(function(idx, elem){
if($root[0].isSameNode(elem)) return false;
else $target = $(elem);
});
if($target.next().find('img,script,iframe,ins').length>0) $target.before(widget);
else $target.after(widget);
return widget;
}
$(document).ready(function() {
$("#thefc").remove();
autoPlaceWidget('<h1 style="background-color:violet;padding: 20px; text-align: center;clear:both;">THEFC</h1>');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment