aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt
blob: 7be8cd3cc2ecb19ed81e0458c6b8159cd5fe3cae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.pitchedapps.frost.injectors

import android.webkit.WebView
import com.pitchedapps.frost.facebook.FB_URL_BASE

/**
 * Created by Allan Wang on 2017-05-31.
 *
 * Collection of short js functions that are embedded directly
 */
enum class JsActions(body: String) : InjectorContract {
    /**
     * Redirects to login activity if create account is found
     * see [com.pitchedapps.frost.web.FrostJSI.loadLogin]
     */
    LOGIN_CHECK("document.getElementById('signup-button')&&Frost.loadLogin();"),
    BASE_HREF("""document.write("<base href='$FB_URL_BASE'/>");"""),
    FETCH_BODY("""setTimeout(function(){var e=document.querySelector("main");e||(e=document.querySelector("body")),Frost.handleHtml(e.outerHTML)},1e2);"""),
    CREATE_POST(clickBySelector("button[name=view_overview]")),
//    CREATE_MSG(clickBySelector("a[rel=dialog]")),
    /**
     * Used as a pseudoinjector for maybe functions
     */
    EMPTY("");

    val function = "!function(){$body}();"

    override fun inject(webView: WebView, callback: (() -> Unit)?) =
            JsInjector(function).inject(webView, callback)

}

@Suppress("NOTHING_TO_INLINE")
private inline fun clickBySelector(selector: String): String =
        """document.querySelector("$selector").click()"""