Async JavaScript load

This commit is contained in:
Eduard Urbach 2018-04-24 13:29:46 +02:00
parent c7f482c525
commit e63be2c43a
4 changed files with 26 additions and 10 deletions

View File

@ -54,7 +54,7 @@ component Layout(app *aero.Application, ctx *aero.Context, user *arn.User, openG
ExtensionNavigation(user) ExtensionNavigation(user)
if user != nil if user != nil
#user(data-id=user.ID, data-pro=user.IsPro(), data-theme=user.Settings().Theme) #user(data-id=user.ID, data-pro=user.IsPro(), data-theme=user.Settings().Theme)
script(src="/scripts") script(src="/scripts", async="async")
script(type="application/ld+json")!= organization script(type="application/ld+json")!= organization
component Content(content string) component Content(content string)

View File

@ -3,7 +3,12 @@ import AnimeNotifier from "../AnimeNotifier"
// Chrome extension installation // Chrome extension installation
export function installExtension(arn: AnimeNotifier, button: HTMLElement) { export function installExtension(arn: AnimeNotifier, button: HTMLElement) {
let browser: any = window["chrome"] let browser: any = window["chrome"]
if(browser && browser.webstore) {
browser.webstore.install() browser.webstore.install()
} else {
window.open("https://chrome.google.com/webstore/detail/anime-notifier/hajchfikckiofgilinkpifobdbiajfch", "_blank")
}
} }
// Desktop app installation // Desktop app installation

View File

@ -83,9 +83,13 @@ export default class AnimeNotifier {
// Event listeners // Event listeners
document.addEventListener("readystatechange", this.onReadyStateChange.bind(this)) document.addEventListener("readystatechange", this.onReadyStateChange.bind(this))
document.addEventListener("DOMContentLoaded", this.onContentLoaded.bind(this)) document.addEventListener("DOMContentLoaded", this.onContentLoaded.bind(this))
document.addEventListener("keydown", this.onKeyDown.bind(this), false)
window.addEventListener("popstate", this.onPopState.bind(this)) // If we finished loading the DOM (either "interactive" or "complete" state),
window.addEventListener("error", this.onError.bind(this)) // immediately trigger the event listener functions.
if(document.readyState !== "loading") {
this.app.emit("DOMContentLoaded")
this.run()
}
// Idle // Idle
requestIdleCallback(this.onIdle.bind(this)) requestIdleCallback(this.onIdle.bind(this))
@ -176,6 +180,11 @@ export default class AnimeNotifier {
} }
async onIdle() { async onIdle() {
// Register event listeners
document.addEventListener("keydown", this.onKeyDown.bind(this), false)
window.addEventListener("popstate", this.onPopState.bind(this))
window.addEventListener("error", this.onError.bind(this))
// Service worker // Service worker
this.serviceWorkerManager = new ServiceWorkerManager(this, "/service-worker") this.serviceWorkerManager = new ServiceWorkerManager(this, "/service-worker")
this.serviceWorkerManager.register() this.serviceWorkerManager.register()

View File

@ -26,12 +26,14 @@ export default class Application {
} }
init() { init() {
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", this.onContentLoaded.bind(this))
}
onContentLoaded() {
let links = document.getElementsByTagName("a") let links = document.getElementsByTagName("a")
this.markActiveLinks(links) this.markActiveLinks(links)
this.ajaxify(links) this.ajaxify(links)
})
} }
get(url: string): Promise<string> { get(url: string): Promise<string> {