Discord: only send icons Discord can show (no ICO/SVG), fall back to og:image

discord.com only has /assets/favicon.ico, which Discord draws as "?".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-12-03 10:35:45 -06:00
commit 297034eb0d
3 changed files with 29 additions and 23 deletions

View file

@ -1672,7 +1672,8 @@ impl App {
"ready" => {},
"meta" => {
let color = crate::theme::parse_css_color(&s(&data, "theme_color"));
let icon = data.get("icons").and_then(|v| v.as_array()).and_then(|a| a.first()).map(|i| s(i, "href")).unwrap_or_default();
let icons: Vec<String> = data.get("icons").and_then(|v| v.as_array()).map(|a| a.iter().map(|i| s(i, "href")).collect()).unwrap_or_default();
let icon = icons.first().cloned().unwrap_or_default();
let changed;
{
let tab = &mut self.windows.get_mut(&w).expect("found").tabs[i];
@ -1680,6 +1681,7 @@ impl App {
tab.theme_color = color;
tab.site_name = s(&data, "site_name");
tab.page_image = s(&data, "image");
tab.rpc_icon = icons.iter().find(|i| crate::discord::usable_image(i)).cloned().unwrap_or_default();
tab.manifest = s(&data, "manifest");
if !icon.is_empty() {
tab.favicon_url = icon;
@ -2242,14 +2244,14 @@ impl App {
}
let mut state = state.clone();
state.position = state.position_at(*received);
activity = crate::discord::media_activity(&settings, &url, &tab.title, &tab.favicon_url, &state, tab.id, &mut self.presence_started);
activity = crate::discord::media_activity(&settings, &url, &tab.title, &[&tab.rpc_icon, &tab.page_image], &state, tab.id, &mut self.presence_started);
}
if activity.is_none()
&& let Some(win) = self.current_window().and_then(|w| self.windows.get(&w))
&& let Some(tab) = win.tab()
&& tab.page.is_none()
&& let Ok(url) = Url::parse(&tab.url)
&& let Some(act) = crate::discord::browse_activity(&settings, &url, &tab.title, &tab.site_name, &tab.page_image, &tab.favicon_url, tab.arrived_ms as i64)
&& let Some(act) = crate::discord::browse_activity(&settings, &url, &tab.title, &tab.site_name, &tab.page_image, &tab.rpc_icon, tab.arrived_ms as i64)
{
activity = Some((act, settings.client_id.clone()));
}

View file

@ -111,17 +111,17 @@ fn https_short(url: &str) -> bool {
url.starts_with("https://") && url.len() <= 256
}
/// Image for the activity's large icon: the first of `images` Discord can fetch (short https URL),
/// else the page origin's /favicon.ico (data:/http:/long favicon URLs are unusable; no third-party
/// service, so no visited host leaks beyond Discord). Empty for pages without a host.
fn icon_url(images: &[&str], page: &Url) -> String {
if let Some(img) = images.iter().find(|i| https_short(i)) {
return (*img).to_owned();
}
match page.host_str() {
Some(host) if matches!(page.scheme(), "http" | "https") => format!("https://{host}/favicon.ico"),
_ => String::new(),
}
/// An image Discord can show: a short https URL that isn't ICO or SVG (Discord draws those as "?").
pub fn usable_image(url: &str) -> bool {
let path = url.split(['?', '#']).next().unwrap_or("").to_ascii_lowercase();
https_short(url) && !path.ends_with(".ico") && !path.ends_with(".svg")
}
/// Image for the activity's large icon: the first of `images` Discord can show, else none, so
/// Discord shows the application's own icon (no third-party favicon service, so no visited host
/// leaks beyond Discord).
fn icon_url(images: &[&str]) -> String {
images.iter().find(|i| usable_image(i)).map(|i| (*i).to_owned()).unwrap_or_default()
}
/// Options that Discord clients may refuse; the worker turns them off when it is told so.
@ -133,7 +133,7 @@ pub struct Quirks {
/// Activity for a playing (or paused) media tab, plus the application ID to use.
/// `started` keeps the progress bar steady between polls: (tab id, title, start ms).
pub fn media_activity(settings: &Rpc, tab_url: &Url, tab_title: &str, favicon: &str, state: &MediaState, tab_id: u64, started: &mut Option<(u64, String, i64)>) -> Option<(Value, String)> {
pub fn media_activity(settings: &Rpc, tab_url: &Url, tab_title: &str, images: &[&str], state: &MediaState, tab_id: u64, started: &mut Option<(u64, String, i64)>) -> Option<(Value, String)> {
let (key, name, kind, button) = site_for(&util::host_of(tab_url), settings)?;
if state.paused && !settings.show_paused {
return None;
@ -149,8 +149,8 @@ pub fn media_activity(settings: &Rpc, tab_url: &Url, tab_title: &str, favicon: &
art = format!("https://i.ytimg.com/vi/{vid}/hqdefault.jpg");
}
}
if !https_short(&art) {
art = icon_url(&[favicon], tab_url);
if !usable_image(&art) {
art = icon_url(images);
}
let by = if creator != name { format!("by {creator}") } else { name.clone() };
let mut act = json!({
@ -212,7 +212,7 @@ pub fn browse_activity(settings: &Rpc, url: &Url, title: &str, site_name: &str,
"timestamps": {"start": since_ms},
"assets": {"large_text": clip(&host, 128)},
});
let icon = icon_url(&[image, favicon], url);
let icon = icon_url(&[image, favicon]);
if !icon.is_empty() {
act["assets"]["large_image"] = json!(icon);
}
@ -490,11 +490,12 @@ mod tests {
#[test]
fn icon_prefers_fetchable_image_then_falls_back() {
let page = Url::parse("https://example.org/a").unwrap();
assert_eq!(icon_url(&["https://x.test/a.png", "https://y.test/f.png"], &page), "https://x.test/a.png");
assert_eq!(icon_url(&["data:image/png;base64,AAAA", "https://y.test/f.png"], &page), "https://y.test/f.png");
assert_eq!(icon_url(&["https://x.test/a.png", "https://y.test/f.png"]), "https://x.test/a.png");
assert_eq!(icon_url(&["data:image/png;base64,AAAA", "https://y.test/f.png"]), "https://y.test/f.png");
let long = format!("https://x.test/{}", "a".repeat(300));
assert_eq!(icon_url(&["", "http://y.test/f.ico", &long], &page), "https://example.org/favicon.ico");
assert_eq!(icon_url(&[""], &Url::parse("about:blank").unwrap()), "");
assert_eq!(icon_url(&["", "http://y.test/f.png", &long]), "");
// Discord draws ICO and SVG as "?" (discord.com only has /assets/favicon.ico).
assert_eq!(icon_url(&["https://discord.com/assets/favicon.ico", "https://x.test/f.SVG?v=2"]), "");
assert_eq!(icon_url(&["https://x.test/f.ico?v=1", "https://x.test/og.png?w=1"]), "https://x.test/og.png?w=1");
}
}

View file

@ -247,6 +247,8 @@ pub struct Tab {
pub favicon_colour: Option<egui::Color32>,
pub site_name: String,
pub page_image: String,
/// Largest page icon Discord can show (not ICO/SVG), for Rich Presence.
pub rpc_icon: String,
pub manifest: String,
/// When the user arrived on this page (Discord "browsing" timer), ms.
pub arrived_ms: u64,
@ -308,6 +310,7 @@ impl Tab {
favicon_colour: None,
site_name: String::new(),
page_image: String::new(),
rpc_icon: String::new(),
manifest: String::new(),
arrived_ms: crate::util::now_ms(),
context: None,