Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

fix: classify Discord avatars and dialogs structurally

Emre Sokullu4569b863abe7
diff --git a/src/chrome/src/content/chat-observation.js b/src/chrome/src/content/chat-observation.js
index e584bf22e..e994659a0 100644
--- a/src/chrome/src/content/chat-observation.js
+++ b/src/chrome/src/content/chat-observation.js
@@ -267,12 +267,16 @@
} catch { return ''; }
};
- const discordDefaultAvatar = (node) => {
+ const discordDefaultAvatarIndex = (node) => {
try {
const url = new URL(attribute(node, 'src'), window.location.href);
- return (url.hostname === 'cdn.discordapp.com' && /^\/embed\/avatars\/\d+\.png$/.test(url.pathname))
- || (url.origin === window.location.origin && /^\/assets\/embed\/avatars\/\d+\.png$/.test(url.pathname));
- } catch { return false; }
+ const match = url.hostname === 'cdn.discordapp.com'
+ ? /^\/embed\/avatars\/(\d+)\.png$/.exec(url.pathname)
+ : url.origin === window.location.origin
+ ? /^\/assets\/embed\/avatars\/(\d+)\.png$/.exec(url.pathname)
+ : null;
+ return match?.[1] || '';
+ } catch { return ''; }
};
const discordMessageText = (content) => {
@@ -334,7 +338,7 @@
const account = query(document, '[class*="accountPopoutButtonWrapper"]')[0]?.parentElement;
const ownAvatar = query(account, 'img[src*="/avatars/"]')[0];
const ownAvatarId = discordAvatarUserId(ownAvatar);
- const ownUsesDefaultAvatar = discordDefaultAvatar(ownAvatar);
+ const ownDefaultAvatarIndex = discordDefaultAvatarIndex(ownAvatar);
const rows = query(channel.transcript, '[role="article"][data-list-item-id]');
const messages = [];
let previousAuthor = '';
@@ -359,9 +363,13 @@
|| (!username ? previousAuthor : '');
const avatar = username?.closest('h3')?.parentElement?.querySelector('img[src]');
const avatarId = discordAvatarUserId(avatar);
+ const defaultAvatarIndex = discordDefaultAvatarIndex(avatar);
let direction = 'unknown';
if (ownAvatarId && avatarId) direction = ownAvatarId === avatarId ? 'outgoing' : 'incoming';
- else if (ownUsesDefaultAvatar && avatarId) direction = 'incoming';
+ else if (ownAvatarId && defaultAvatarIndex) direction = 'incoming';
+ else if (ownDefaultAvatarIndex && avatarId) direction = 'incoming';
+ else if (ownDefaultAvatarIndex && defaultAvatarIndex
+ && ownDefaultAvatarIndex !== defaultAvatarIndex) direction = 'incoming';
else if (!username) direction = previousDirection;
previousAuthor = author;
previousDirection = direction;
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index 7d7862acd..869f10f24 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -5898,19 +5898,25 @@
// Discord keeps the channel composer mounted behind its settings.
// Only the owning management dialog is a non-message surface.
if (dialog.querySelector('[data-message-id],[data-list-id="chat-messages"],[role="textbox"][aria-label^="Message " i],[role="log"] article')) return false;
- const dialogName = compact(dialog.getAttribute('aria-label'));
- const creation = /^(?:Create Channel|Create Category)$/i.test(dialogName)
- && Array.from(dialog.querySelectorAll('h1,h2,[role="heading"]'))
- .some(el => visible(el) && compact(el.textContent) === dialogName)
- && Array.from(dialog.querySelectorAll('input[type="text"],input:not([type])'))
- .some(el => visible(el));
+ const dialogId = dialog.getAttribute('id');
+ const creationHeading = dialogId && Array.from(dialog.querySelectorAll('h1'))
+ .some(el => visible(el) && el.id === `heading-${dialogId}`);
+ const creationInputs = Array.from(dialog.querySelectorAll('input[type="text"],input:not([type])'))
+ .filter(visible);
+ const creationRadios = Array.from(dialog.querySelectorAll('input[type="radio"],[role="radio"]'))
+ .filter(visible);
+ const creation = dialog.getAttribute('data-dialog') === 'modal'
+ && creationHeading && creationInputs.length === 1
+ && Array.from(dialog.querySelectorAll('input[type="checkbox"],[role="switch"]')).some(visible)
+ && Array.from(dialog.querySelectorAll('button[type="submit"],input[type="submit"]')).some(visible)
+ && (creationRadios.length === 0 || creationRadios.length >= 2);
const settingsNav = dialog.querySelector('nav [role="tablist"],[role="navigation"] [role="tablist"]');
const serverSettings = !!settingsNav
&& !!settingsNav.querySelector('[role="tab"][aria-label="Manage Roles"]')
&& Array.from(settingsNav.querySelectorAll('[role="tab"]'))
.some(el => compact(el.textContent) === 'Enable Community' || compact(el.textContent) === 'Safety Setup');
const channelSettings = dialog.getAttribute('data-layer') === 'CHANNEL_SETTINGS'
- && dialogName === 'Channel Settings' && !!settingsNav
+ && compact(dialog.getAttribute('aria-label')) === 'Channel Settings' && !!settingsNav
&& ['Overview', 'Permissions'].every(name => Array.from(settingsNav.querySelectorAll('[role="tab"]'))
.some(el => compact(el.getAttribute('aria-label') || el.textContent) === name));
if (!channelSettings && dialog.querySelector('[data-slate-editor]')) return false;
diff --git a/src/firefox/src/content/chat-observation.js b/src/firefox/src/content/chat-observation.js
index e584bf22e..e994659a0 100644
--- a/src/firefox/src/content/chat-observation.js
+++ b/src/firefox/src/content/chat-observation.js
@@ -267,12 +267,16 @@
} catch { return ''; }
};
- const discordDefaultAvatar = (node) => {
+ const discordDefaultAvatarIndex = (node) => {
try {
const url = new URL(attribute(node, 'src'), window.location.href);
- return (url.hostname === 'cdn.discordapp.com' && /^\/embed\/avatars\/\d+\.png$/.test(url.pathname))
- || (url.origin === window.location.origin && /^\/assets\/embed\/avatars\/\d+\.png$/.test(url.pathname));
- } catch { return false; }
+ const match = url.hostname === 'cdn.discordapp.com'
+ ? /^\/embed\/avatars\/(\d+)\.png$/.exec(url.pathname)
+ : url.origin === window.location.origin
+ ? /^\/assets\/embed\/avatars\/(\d+)\.png$/.exec(url.pathname)
+ : null;
+ return match?.[1] || '';
+ } catch { return ''; }
};
const discordMessageText = (content) => {
@@ -334,7 +338,7 @@
const account = query(document, '[class*="accountPopoutButtonWrapper"]')[0]?.parentElement;
const ownAvatar = query(account, 'img[src*="/avatars/"]')[0];
const ownAvatarId = discordAvatarUserId(ownAvatar);
- const ownUsesDefaultAvatar = discordDefaultAvatar(ownAvatar);
+ const ownDefaultAvatarIndex = discordDefaultAvatarIndex(ownAvatar);
const rows = query(channel.transcript, '[role="article"][data-list-item-id]');
const messages = [];
let previousAuthor = '';
@@ -359,9 +363,13 @@
|| (!username ? previousAuthor : '');
const avatar = username?.closest('h3')?.parentElement?.querySelector('img[src]');
const avatarId = discordAvatarUserId(avatar);
+ const defaultAvatarIndex = discordDefaultAvatarIndex(avatar);
let direction = 'unknown';
if (ownAvatarId && avatarId) direction = ownAvatarId === avatarId ? 'outgoing' : 'incoming';
- else if (ownUsesDefaultAvatar && avatarId) direction = 'incoming';
+ else if (ownAvatarId && defaultAvatarIndex) direction = 'incoming';
+ else if (ownDefaultAvatarIndex && avatarId) direction = 'incoming';
+ else if (ownDefaultAvatarIndex && defaultAvatarIndex
+ && ownDefaultAvatarIndex !== defaultAvatarIndex) direction = 'incoming';
else if (!username) direction = previousDirection;
previousAuthor = author;
previousDirection = direction;
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index 960b331ee..11787ccdf 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -4976,19 +4976,25 @@
// Discord keeps the channel composer mounted behind its settings.
// Only the owning management dialog is a non-message surface.
if (dialog.querySelector('[data-message-id],[data-list-id="chat-messages"],[role="textbox"][aria-label^="Message " i],[role="log"] article')) return false;
- const dialogName = compact(dialog.getAttribute('aria-label'));
- const creation = /^(?:Create Channel|Create Category)$/i.test(dialogName)
- && Array.from(dialog.querySelectorAll('h1,h2,[role="heading"]'))
- .some(el => visible(el) && compact(el.textContent) === dialogName)
- && Array.from(dialog.querySelectorAll('input[type="text"],input:not([type])'))
- .some(el => visible(el));
+ const dialogId = dialog.getAttribute('id');
+ const creationHeading = dialogId && Array.from(dialog.querySelectorAll('h1'))
+ .some(el => visible(el) && el.id === `heading-${dialogId}`);
+ const creationInputs = Array.from(dialog.querySelectorAll('input[type="text"],input:not([type])'))
+ .filter(visible);
+ const creationRadios = Array.from(dialog.querySelectorAll('input[type="radio"],[role="radio"]'))
+ .filter(visible);
+ const creation = dialog.getAttribute('data-dialog') === 'modal'
+ && creationHeading && creationInputs.length === 1
+ && Array.from(dialog.querySelectorAll('input[type="checkbox"],[role="switch"]')).some(visible)
+ && Array.from(dialog.querySelectorAll('button[type="submit"],input[type="submit"]')).some(visible)
+ && (creationRadios.length === 0 || creationRadios.length >= 2);
const settingsNav = dialog.querySelector('nav [role="tablist"],[role="navigation"] [role="tablist"]');
const serverSettings = !!settingsNav
&& !!settingsNav.querySelector('[role="tab"][aria-label="Manage Roles"]')
&& Array.from(settingsNav.querySelectorAll('[role="tab"]'))
.some(el => compact(el.textContent) === 'Enable Community' || compact(el.textContent) === 'Safety Setup');
const channelSettings = dialog.getAttribute('data-layer') === 'CHANNEL_SETTINGS'
- && dialogName === 'Channel Settings' && !!settingsNav
+ && compact(dialog.getAttribute('aria-label')) === 'Channel Settings' && !!settingsNav
&& ['Overview', 'Permissions'].every(name => Array.from(settingsNav.querySelectorAll('[role="tab"]'))
.some(el => compact(el.getAttribute('aria-label') || el.textContent) === name));
if (!channelSettings && dialog.querySelector('[data-slate-editor]')) return false;
diff --git a/test/discord-guard.mjs b/test/discord-guard.mjs
index d1f434d24..00d39deb8 100644
--- a/test/discord-guard.mjs
+++ b/test/discord-guard.mjs
@@ -114,10 +114,16 @@ for (const [kind, engine, AgentClass, policy] of [
} finally {await page.close();}
});
await t.test('creation dialogs allow field submission and Enter while the channel composer is still mounted',async()=>{
- for(const name of ['Create Channel','Create Category']) {
- const {page,guard}=await setup(`<div role="dialog" aria-modal="true" aria-label="${name}"><div role="log"></div><div role="log"></div><h1>${name}</h1>
-<form><input id="name" type="text" aria-label="Name" value="test"><button id="save" type="submit">${name}</button><button id="cancel" type="button">Cancel</button></form></div>`);
+ for(const [id,label,radios] of [
+ ['channel-create','Kanal erstellen','<div role="radiogroup"><input type="radio" checked><input type="radio"><input type="radio"></div>'],
+ ['category-create','Kategori oluÅŸtur',''],
+ ]) {
+ const {page,guard,probe}=await setup(`<div id="${id}" data-dialog="modal" role="dialog" aria-modal="true" aria-label="${label}"><div role="log"></div><div role="log"></div>
+<header><h1 id="heading-${id}">${label}</h1><button type="button" aria-label="Close">×</button></header>${radios}
+<input id="name" type="text" placeholder="${label}" value="test"><input id="private" type="checkbox" role="switch">
+<button id="save" type="submit">${label}</button><button id="cancel" type="button">Esc</button></div>`);
try {
+ assert.equal((await probe('click',{selector:'#save'})).nonMessagingTarget,true,'localized creation title is recognized structurally');
assert.equal(await guard('click',{selector:'#save'}),null);
assert.equal(await guard('click',{selector:'#cancel'}),null);
assert.equal(await guard('set_field',{selector:'#name',value:'test',submit:true}),null);
@@ -215,6 +221,14 @@ for (const [kind, engine, AgentClass, policy] of [
await page.locator('#message .contents img').evaluate(el=>el.setAttribute('src','/assets/embed/avatars/3.png'));
const defaultAvatars=await observe();
assert.equal(defaultAvatars.messages[0].direction,'unknown','shared Discord default avatars do not identify message authors');
+ await page.locator('#message .contents img').evaluate(el=>el.setAttribute('src','https://cdn.discordapp.com/embed/avatars/4.png'));
+ assert.equal((await observe()).messages[0].direction,'incoming','different default avatar indices cannot be the same account');
+ await page.locator('#profile-avatar').evaluate(el=>el.setAttribute('src','https://cdn.discordapp.com/avatars/11/self.webp?size=56'));
+ assert.equal((await observe()).messages[0].direction,'incoming','a default avatar cannot belong to the custom-avatar account');
+ await page.locator('#profile-avatar').evaluate(el=>el.setAttribute('src','/assets/embed/avatars/3.png'));
+ await page.locator('#message .contents img').evaluate(el=>el.setAttribute('src','https://cdn.discordapp.com/avatars/22/other.webp?size=160'));
+ assert.equal((await observe()).messages[0].direction,'incoming','a custom avatar cannot belong to the default-avatar account');
+ await page.locator('#message .contents img').evaluate(el=>el.setAttribute('src','/assets/embed/avatars/3.png'));
let advanced=advanceChatSession(createChatSession({threadKey:result.threadKey}),defaultAvatars);
await page.locator('#wave').evaluate(el=>el.textContent='Changed hover action');
const afterHover=await observe();