diff options
author | Allan Wang <me@allanwang.ca> | 2017-12-31 02:44:46 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-31 02:44:46 -0500 |
commit | 725d6a99a07f91f940a07e6b49dd6224a6aa32d1 (patch) | |
tree | 4e0b1b3b9ffe9b5aef3c8d0f154ea9ab1058fd5e /app/src/main/kotlin/com/pitchedapps/frost/web/HeadlessHtmlExtractor.kt | |
parent | 3076d9a97c203497aec1415d8ac6037d10eebb46 (diff) | |
download | frost-725d6a99a07f91f940a07e6b49dd6224a6aa32d1.tar.gz frost-725d6a99a07f91f940a07e6b49dd6224a6aa32d1.tar.bz2 frost-725d6a99a07f91f940a07e6b49dd6224a6aa32d1.zip |
Enhancement/proguard (#589)
* Add error log
* Rewrite logger
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/web/HeadlessHtmlExtractor.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/web/HeadlessHtmlExtractor.kt | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/HeadlessHtmlExtractor.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/HeadlessHtmlExtractor.kt deleted file mode 100644 index 2c7735a4..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/HeadlessHtmlExtractor.kt +++ /dev/null @@ -1,91 +0,0 @@ -package com.pitchedapps.frost.web - -import android.annotation.SuppressLint -import android.content.Context -import android.webkit.JavascriptInterface -import android.webkit.WebView -import ca.allanwang.kau.utils.gone -import com.pitchedapps.frost.R -import com.pitchedapps.frost.facebook.USER_AGENT_BASIC -import com.pitchedapps.frost.injectors.InjectorContract -import com.pitchedapps.frost.utils.L -import io.reactivex.Single -import io.reactivex.SingleEmitter -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.schedulers.Schedulers -import org.jetbrains.anko.runOnUiThread -import java.util.concurrent.TimeUnit - -/** - * Created by Allan Wang on 2017-08-12. - * - * Launches a headless html request and returns a result pair - * When successful, the pair will contain the html content and -1 - * When unsuccessful, the pair will contain an empty string and a StringRes for the given error - * - * All errors are rerouted to success calls, so no exceptions should occur. - * The headless extractor will also destroy itself on cancellation or when the request is finished - */ -fun Context.launchHeadlessHtmlExtractor(url: String, injector: InjectorContract, action: (Single<Pair<String, Int>>) -> Unit) { - val single = Single.create<Pair<String, Int>> { e: SingleEmitter<Pair<String, Int>> -> - val extractor = HeadlessHtmlExtractor(this, url, injector, e) - extractor.resumeTimers() - e.setCancellable { - runOnUiThread { - extractor.pauseTimers() - extractor.destroy() - } - e.onSuccess("" to R.string.html_extraction_cancelled) - } - }.subscribeOn(AndroidSchedulers.mainThread()) - .timeout(20, TimeUnit.SECONDS, Schedulers.io(), { it.onSuccess("" to R.string.html_extraction_timeout) }) - .onErrorReturn { "" to R.string.html_extraction_error } - action(single) -} - -/** - * Given a link and some javascript, will load the link and load the JS on completion - * The JS is expected to call [HeadlessHtmlExtractor.HtmlJSI.handleHtml], which will be sent - * to the [emitter] - */ -@SuppressLint("ViewConstructor") -private class HeadlessHtmlExtractor( - context: Context, url: String, val injector: InjectorContract, val emitter: SingleEmitter<Pair<String, Int>> -) : WebView(context) { - - val startTime = System.currentTimeMillis() - - init { - L.v("Created HeadlessHtmlExtractor for $url") - gone() - setupWebview(url) - } - - @SuppressLint("SetJavaScriptEnabled") - private fun setupWebview(url: String) { - settings.javaScriptEnabled = true - settings.userAgentString = USER_AGENT_BASIC - webViewClient = HeadlessWebViewClient(url, injector) // basic client that loads our JS once the page has loaded - webChromeClient = HeadlessChromeClient() - addJavascriptInterface(HtmlJSI(), "Frost") - loadUrl(url) - } - - inner class HtmlJSI { - @JavascriptInterface - fun handleHtml(html: String?) { - val time = System.currentTimeMillis() - startTime - emitter.onSuccess((html ?: "") to -1) - post { - L.d("HeadlessHtmlExtractor fetched $url in $time ms") - destroy() - } - } - } - - override fun destroy() { - pauseTimers() - super.destroy() - L.d("HeadlessHtmlExtractor destroyed") - } -}
\ No newline at end of file |