-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jelly: Load manifest icon instead of favicon
We can use a higher quality icon that is defined in the manifest.json If website has manifest, we ignore the favicon and try to extract icon from it Change-Id: Ie7219807d96d351e90742dbe92f9520489e43928
- Loading branch information
1 parent
e12cd59
commit 0e1a72b
Showing
4 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2025 The LineageOS Project | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.lineageos.jelly.js | ||
|
||
import android.graphics.BitmapFactory | ||
import android.webkit.JavascriptInterface | ||
import androidx.annotation.Keep | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import org.lineageos.jelly.webview.WebViewExtActivity | ||
import java.net.HttpURLConnection | ||
import java.net.URI | ||
import java.net.URL | ||
import kotlin.reflect.cast | ||
|
||
@Keep | ||
class JsManifest( | ||
private val activity: WebViewExtActivity, | ||
) { | ||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) | ||
|
||
@JavascriptInterface | ||
fun onIconResolved(baseUrl: String, iconUrl: String) { | ||
val url = URI(baseUrl).resolve(iconUrl).toString() | ||
scope.launch { | ||
val bitmap = getIconBitmap(url) | ||
if (bitmap != null) { | ||
withContext(Dispatchers.Main) { | ||
activity.onFaviconLoaded(bitmap) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun getIconBitmap(url: String) = runCatching { | ||
val connection = HttpURLConnection::class.cast( | ||
URL(url).openConnection() | ||
) | ||
connection.connect() | ||
if (connection.responseCode != HttpURLConnection.HTTP_OK) return null | ||
connection.inputStream.buffered().use { | ||
BitmapFactory.decodeStream(it) | ||
} | ||
}.getOrNull() | ||
|
||
companion object { | ||
const val INTERFACE = "JsManifest" | ||
const val URL = "(() => document.querySelector('link[rel=\"manifest\"]')?.href ?? '')" | ||
|
||
private const val MONKEY_PATCH_ONCE_KEY = "JsManifestMonkeyPatch" | ||
const val SCRIPT = """ | ||
(async () => { | ||
if (window.$MONKEY_PATCH_ONCE_KEY) return; | ||
window.$MONKEY_PATCH_ONCE_KEY = true; | ||
const baseUrl = $URL(); | ||
if (!baseUrl) return; | ||
try { | ||
const res = await fetch(baseUrl); | ||
const manifest = await res.json(); | ||
let iconUrl = null; | ||
let minWidth = 33; | ||
const maxWidth = 333; | ||
(manifest.icons ?? []).forEach((icon) => { | ||
if (!icon.sizes) return; | ||
if (icon.purpose?.includes('monochrome')) return; | ||
const width = Number(icon.sizes.split('x')[0]); | ||
if (width >= minWidth && width <= maxWidth) { | ||
minWidth = width; | ||
iconUrl = icon.src; | ||
} | ||
}); | ||
if (iconUrl !== null) { | ||
$INTERFACE.onIconResolved(baseUrl, iconUrl); | ||
} | ||
} catch (error) { | ||
} | ||
})(); | ||
""" | ||
} | ||
} |
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
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
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