Last active
September 14, 2016 22:14
-
-
Save cvazac/87c001992507b52a22ba7ab0b4eec6ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(w) { | |
if (w != top) return | |
var anchors = document.getElementsByTagName('a') | |
w.addEventListener('load', checkVisibleAnchors) | |
w.addEventListener('scroll', checkVisibleAnchors) | |
var sum = 0, count = 0 | |
function checkVisibleAnchors() { | |
sum -= performance.now() | |
for (var i = 0; i < anchors.length; i++) { | |
var anchor = anchors[i] | |
var href = anchor.href | |
var visited = anchor.__visited | |
if (!href || visited || !isInView(anchor)) continue | |
anchor.__visited = true | |
var iframe = document.createElement('iframe') | |
iframe.style.display = 'none' | |
iframe.src = href | |
iframe.addEventListener('load', function(e) { | |
e.target.parentNode && e.target.parentNode.removeChild(e.target) | |
}) | |
document.body.appendChild(iframe) | |
} | |
sum += performance.now() | |
console.info('avg', (sum / ++count)) | |
} | |
function isInView(a) { | |
var rect = a.getBoundingClientRect() | |
return rect.top >= 0 && | |
rect.bottom <= w.innerHeight | |
return isVisible | |
} | |
})(window) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment