Skip to content

Commit

Permalink
Support data URL in @require
Browse files Browse the repository at this point in the history
Close #179 as completed.
  • Loading branch information
JingMatrix committed Jul 25, 2024
1 parent eb8ded6 commit 4bf9a1d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/org/matrix/chromext/script/Parser.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.matrix.chromext.script

import android.net.Uri
import android.util.Base64
import java.net.HttpURLConnection
import java.net.URL
import kotlin.text.Regex
Expand Down Expand Up @@ -80,6 +82,16 @@ fun parseScript(input: String, storage: String? = null): Script? {
}

private fun downloadLib(libUrl: String): String {
if (libUrl.startsWith("data:")) {
val chunks = libUrl.split(",").toMutableList()
val type = chunks.removeFirst()
val data = Uri.decode(chunks.joinToString(""))
if (type.endsWith("base64")) {
return Base64.decode(data, Base64.DEFAULT).toString()
} else {
return data
}
}
val url = URL(libUrl)
val connection = url.openConnection() as HttpURLConnection
return connection.inputStream.bufferedReader().use { it.readText() }
Expand Down

0 comments on commit 4bf9a1d

Please sign in to comment.