Last active
November 25, 2017 00:22
-
-
Save cypressious/b9f16a2124e61b0c059870f3a46a2c65 to your computer and use it in GitHub Desktop.
Kotlin Firefox Extension content script https://medium.com/@Cypressious/your-second-firefox-extension-in-kotlin-bafd91d87c41
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
fun main(args: Array<String>) { | |
if (window.asDynamic().hasRun == true) { | |
return | |
} | |
window.asDynamic().hasRun = true | |
browser.runtime.onMessage.addListener { message -> | |
if (message.command === "beastify") { | |
insertBeast(message.beastURL as String) | |
} else if (message.command === "reset") { | |
removeExistingBeasts() | |
} | |
} | |
} | |
fun insertBeast(beastURL: String) { | |
removeExistingBeasts() | |
val beastImage = document.createElement("img") as HTMLElement | |
beastImage.run { | |
setAttribute("src", beastURL) | |
style.height = "100vh" | |
className = "beastify-image" | |
} | |
document.body?.appendChild(beastImage) | |
} | |
fun removeExistingBeasts() { | |
val existingBeasts = document.querySelectorAll(".beastify-image") | |
for (beast in existingBeasts.asList()) { | |
beast.parentNode?.removeChild(beast) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment