Skip to content

Commit

Permalink
original
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhinocros committed Feb 9, 2020
1 parent 513c8ba commit 5199751
Show file tree
Hide file tree
Showing 50 changed files with 4,548 additions and 0 deletions.
216 changes: 216 additions & 0 deletions adsense/adsenseloader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
A JavaScript plugin for lazy-loading responsive Google Adsense ads.
-
By Osvaldas Valutis, www.osvaldas.info
Available for use under the MIT License
*/

;( function( root, factory )
{
var pluginName = 'adsenseLoader';
if( typeof define === 'function' && define.amd ) define([], factory( pluginName ));
else if( typeof exports === 'object' ) module.exports = factory( pluginName );
else root[ pluginName ] = factory( pluginName );

}( this, function( pluginName )
{
'use strict';

var scriptUrl = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js',
scriptLoaded = false,
throttleTO = 250,

defaultOpts =
{
laziness: 1,
onLoad: false,
},

extendObj = function( defaults, options )
{
var prop, extended = {};
for( prop in defaults )
if( Object.prototype.hasOwnProperty.call( defaults, prop ))
extended[ prop ] = defaults[ prop ];

for( prop in options )
if( Object.prototype.hasOwnProperty.call( options, prop ))
extended[ prop ] = options[ prop ];

return extended;
},

addClass = function( el, className )
{
if( el.classList ) el.classList.add( className );
else el.className += ' ' + className;
},

getOffset = function( el )
{
var rect = el.getBoundingClientRect();
return { top: rect.top + document.body.scrollTop, left: rect.left + document.body.scrollLeft };
},

loadScript = function( url, callback )
{
var s = document.createElement( 'script' );
s.src = url;
s.async = true;
s.addEventListener( 'load', function()
{
s.parentNode.removeChild( s );
if( typeof callback === 'function' )
callback();
});
document.body.appendChild( s );
},

throttle = function(a,b){var c,d;return function(){var e=this,f=arguments,g=+new Date;c&&g<c+a?(clearTimeout(d),d=setTimeout(function(){c=g,b.apply(e,f)},a)):(c=g,b.apply(e,f))}},


adsToLoad = [],
adsLoaded = [],
adsPending = [],

loadAd = function( ad )
{
( adsbygoogle = window.adsbygoogle || []).push({});

var onLoadFn = ad._adsenseLoaderData.options.onLoad;
if( typeof onLoadFn === 'function' )
{
ad.querySelector( 'iframe' ).addEventListener( 'load', function()
{
onLoadFn( ad );
});
}
},
initAds = function()
{
if( !adsToLoad.length ) return true;

var winScroll = window.pageYOffset,
winHeight = window.innerHeight;

adsToLoad.forEach( function( ad )
{
var offset = getOffset( ad ).top,
laziness = ad._adsenseLoaderData.options.laziness + 1;

// if the element is too far below || too far above
if( offset - winScroll > winHeight * laziness || winScroll - offset - ad.offsetHeight - ( winHeight * laziness ) > 0 )
return true;

adsToLoad = removeAdFromList( adsToLoad, ad );
ad._adsenseLoaderData.width = getAdWidth( ad );
addClass( ad.children[ 0 ], 'adsbygoogle' );
adsLoaded.push( ad );

if( typeof adsbygoogle !== 'undefined' ) loadAd( ad );
else adsPending.push( ad );

if( !scriptLoaded )
{
scriptLoaded = true;
loadScript( scriptUrl, function()
{
adsPending.forEach( loadAd );
});
}
});
},
resizeAds = function()
{
if( !adsLoaded.length ) return true;

var anyNew = false;
adsLoaded.forEach( function( ad )
{
if( ad._adsenseLoaderData.width != getAdWidth( ad ))
{
anyNew = true;
adsLoaded = removeAdFromList( adsLoaded, ad );
ad.innerHTML = ad._adsenseLoaderData.originalHTML;
adsToLoad.push( ad );
}
});
if( anyNew ) initAds();
},
getAdWidth = function( ad )
{
return parseInt( window.getComputedStyle( ad, ':before' ).getPropertyValue( 'content' ).slice( 1, -1 ) || 9999 );
},
removeAdFromList = function( list, element )
{
return list.filter( function( entry )
{
return entry !== element;
});
},
normalizeAdElement = function( ad, options )
{
ad._adsenseLoaderData =
{
originalHTML: ad.innerHTML,
options: options
};
ad.adsenseLoader = function( method )
{
if( method == 'destroy' )
{
adsToLoad = removeAdFromList( adsToLoad, ad );
adsLoaded = removeAdFromList( adsLoaded, ad );
adsPending = removeAdFromList( adsLoaded, ad );
ad.innerHTML = ad._adsenseLoaderData.originalHTML;
}
};
return ad;
};


window.addEventListener( 'scroll', throttle( throttleTO, initAds ));
window.addEventListener( 'resize', throttle( throttleTO, initAds ));
window.addEventListener( 'resize', throttle( throttleTO, resizeAds ));


function Plugin( elements, options )
{
if( typeof elements === 'string' ) elements = document.querySelectorAll( elements );
else if( typeof elements.length === 'undefined' ) elements = [ elements ];

options = extendObj( defaultOpts, options );

[].forEach.call( elements, function( entry )
{
entry = normalizeAdElement( entry, options );
adsToLoad.push( entry );
});

this.elements = elements;

initAds();
}

Plugin.prototype =
{
destroy: function()
{
this.elements.forEach( function( entry )
{
entry.adsenseLoader( 'destroy' );
});
}
};

window.adsenseLoaderConfig = function( options )
{
if( typeof options.scriptUrl !== 'undefined' )
scriptUrl = options.scriptUrl;

if( typeof options.throttle !== 'undefined' )
throttleTO = options.throttle;
};

return Plugin;
}));
159 changes: 159 additions & 0 deletions adsense/jquery.adsenseloader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
A JavaScript plugin for lazy-loading responsive Google Adsense ads.
-
By Osvaldas Valutis, www.osvaldas.info
Available for use under the MIT License
*/

;( function( $, window, document, undefined )
{
'use strict';

var $win = $( window ),
throttle = function(a,b){var c,d;return function(){var e=this,f=arguments,g=+new Date;c&&g<c+a?(clearTimeout(d),d=setTimeout(function(){c=g,b.apply(e,f)},a)):(c=g,b.apply(e,f))}},

scriptUrl = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js',
scriptLoaded = false,
throttleTO = 250,

$adsLoaded = $([]),
$adsToLoad = $([]),
$adsPending = $([]),

loadAd = function( $ad )
{
( adsbygoogle = window.adsbygoogle || []).push({});

var onLoadFn = $ad.data( 'alOptions' ).onLoad;
if( typeof onLoadFn === 'function' )
{
$ad.find( 'iframe' ).one( 'load', function()
{
onLoadFn( $ad );
});
}
},
getAdWidth = function( ad )
{
return parseInt( window.getComputedStyle( ad, ':before' ).getPropertyValue( 'content' ).slice( 1, -1 ) || 9999 );
},
initAds = function()
{
if( !$adsToLoad.length ) return true;

var winScroll = $win.scrollTop(),
winHeight = $win.height();

$adsToLoad.each( function()
{
var $this = $( this ),
thisOpts = $this.data( 'alOptions' ),
laziness = thisOpts.laziness + 1,
offset = $this.offset().top;

// if the element is too far below || too far above
if( offset - winScroll > winHeight * laziness || winScroll - offset - $this.outerHeight() - ( winHeight * laziness ) > 0 )
return true;

$adsToLoad = $adsToLoad.not( $this );
$adsLoaded = $adsLoaded.add( $this );

$this
.data( 'alOriginalHTML', $this.html())
.data( 'alWidth', getAdWidth( this ))
.children( ':first' )
.addClass( 'adsbygoogle' );

if( typeof adsbygoogle !== 'undefined' ) loadAd( $this );
else $adsPending = $adsPending.add( $this );

if( !scriptLoaded )
{
scriptLoaded = true;
$.ajax(
{
url: scriptUrl,
async: true,
cache: true,
dataType: 'script',
success: function()
{
$adsPending.each( function()
{
loadAd( $( this ));
});
$adsPending = $([]);
}
});
}
});
},
resizeAds = function()
{
if( !$adsLoaded.length ) return true;

var anyNew = false;
$adsLoaded.each( function()
{
var $this = $( this );
if( $this.data( 'alWidth' ) != getAdWidth( this ))
{
$adsLoaded = $adsLoaded.not( $this );
$this.html( $this.data( 'alOriginalHTML' ));
$adsToLoad = $adsToLoad.add( $this );
anyNew = true;
}
});
if( anyNew ) initAds();
};

$win
.on( 'scroll resize', throttle( throttleTO, initAds ))
.on( 'resize', throttle( throttleTO, resizeAds ));

$.fn.adsenseLoader = function( options )
{
if( typeof options !== 'string' )
{
options = $.extend({},
{
laziness: 1,
onLoad: false
},
options );
}

this.each( function()
{
var $this = $( this );

if( options === 'destroy' )
{
$this.html( $this.data( 'alOriginalHTML' ));
$adsToLoad = $adsToLoad.not( $this );
$adsLoaded = $adsLoaded.not( $this );
$adsPending = $adsPending.not( $this );
}
else
{
$this.data( 'alOptions', options );
$adsToLoad = $adsToLoad.add( $this );
}
});

if( options !== 'destroy' )
initAds();

return this;
};

$.adsenseLoaderConfig = function( options )
{
if( typeof options.scriptUrl !== 'undefined' )
scriptUrl = options.scriptUrl;

if( typeof options.throttle !== 'undefined' )
throttleTO = options.throttle;
};

})( jQuery, window, document );
6 changes: 6 additions & 0 deletions bootstrap/bootstrap.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bootstrap/bootstrap.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 5199751

Please sign in to comment.