Last active
August 31, 2016 08:30
-
-
Save kaedea/b840aedc8db5af4e9e7e1f2cb3a4de7e to your computer and use it in GitHub Desktop.
JavaScript for monitoring Android WebView performance.
This file contains hidden or 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
window.addEventListener('DOMContentLoaded', | |
function() { | |
prompt('domc:' + window.app.getCurrentTime()); | |
}) | |
window.addEventListener('load', | |
function() { | |
prompt('load:' + window.app.getCurrentTime()); | |
}) | |
window.addEventListener('DOMContentLoaded', | |
function() { | |
first_screen(); | |
}) | |
function first_screen() { | |
var imgs = document.getElementsByTagName("img"), | |
fs = +window.app.getCurrentTime(); | |
var fsItems = [], | |
that = this; | |
function getOffsetTop(elem) { | |
var top = 0; | |
top = window.pageYOffset ? window.pageYOffset: document.documentElement.scrollTop; | |
try { | |
top += elem.getBoundingClientRect().top; | |
} catch(e) { | |
} finally { | |
return top; | |
} | |
} | |
var loadEvent = function() { | |
//gif避免 | |
if (this.removeEventListener) { | |
this.removeEventListener("load", loadEvent, false); | |
} | |
fsItems.push({ | |
img: this, | |
time: window.app.getCurrentTime() | |
}); | |
} | |
for (var i = 0; i < imgs.length; i++) { (function() { | |
var img = imgs[i]; | |
if (img.addEventListener) { | |
! img.complete && img.addEventListener("load", loadEvent, false); | |
} else if (img.attachEvent) { | |
img.attachEvent("onreadystatechange", | |
function() { | |
if (img.readyState == "complete") { | |
loadEvent.call(img, loadEvent); | |
} | |
}); | |
} | |
})(); | |
} | |
function firstscreen_time() { | |
var sh = document.documentElement.clientHeight; | |
for (var i = 0; i < fsItems.length; i++) { | |
var item = fsItems[i], | |
img = item['img'], | |
time = item['time'], | |
top = getOffsetTop(img); | |
if (top > 0 && top < sh) { | |
fs = time > fs ? time: fs; | |
} | |
} | |
return fs; | |
} | |
window.addEventListener('load', | |
function() { | |
prompt('firstscreen:' + firstscreen_time()); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment