Skip to content

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

fix: recognize Discord direct-message conversations

Emre Sokullu6449cb678fc4
diff --git a/src/chrome/src/content/chat-observation.js b/src/chrome/src/content/chat-observation.js
index ad8e6acc8..58c135523 100644
--- a/src/chrome/src/content/chat-observation.js
+++ b/src/chrome/src/content/chat-observation.js
@@ -138,24 +138,45 @@
const discordChannel = (composer) => {
if (!composer || !/^(?:www\.)?discord\.com$/.test(window.location?.hostname || '')) return null;
const route = /^\/channels\/(\d+)\/(\d+)\/?$/.exec(window.location?.pathname || '');
- if (!route || !composer.matches?.('[data-slate-editor="true"][contenteditable="true"][role="textbox"]')) return null;
+ const directMessageRoute = /^\/channels\/@me\/(\d+)\/?$/.exec(window.location?.pathname || '');
+ if ((!route && !directMessageRoute)
+ || !composer.matches?.('[data-slate-editor="true"][contenteditable="true"][role="textbox"]')) return null;
const root = composer.closest('main,[role="main"]');
if (!root || !visible(root)) return null;
const transcript = query(root, '[data-list-id="chat-messages"]').find(node =>
visible(node) && !contains(node, composer));
+ const channelId = route?.[2] || directMessageRoute[1];
+ const routePath = route
+ ? `/channels/${route[1]}/${channelId}`
+ : `/channels/@me/${channelId}`;
const channelLink = query(document, 'nav a[href],[role="navigation"] a[href]').find(node => {
try {
const url = new URL(attribute(node, 'href'), window.location.href);
return visible(node) && attribute(node, 'aria-current') === 'page' && url.origin === window.location.origin
- && url.pathname.replace(/\/$/, '') === `/channels/${route[1]}/${route[2]}`;
+ && url.pathname.replace(/\/$/, '') === routePath;
} catch { return false; }
});
+ const selectedDmRouteMismatch = !!directMessageRoute
+ && query(document, 'nav a[href][data-list-item-id^="private-channels-"],[role="navigation"] a[href][data-list-item-id^="private-channels-"]')
+ .some(node => {
+ try {
+ const url = new URL(attribute(node, 'href'), window.location.href);
+ return visible(node) && attribute(node, 'aria-current') === 'page'
+ && url.origin === window.location.origin
+ && /^\/channels\/@me\/\d+\/?$/.test(url.pathname)
+ && url.pathname.replace(/\/$/, '') !== routePath;
+ } catch { return false; }
+ });
const name = /^Message #(.+)$/.exec(attribute(composer, 'aria-label'))?.[1]
|| /^Messages in (.+)$/.exec(attribute(transcript, 'aria-label'))?.[1]
|| /^(.+) \(channel\)$/.exec(attribute(root, 'aria-label'))?.[1]
- || `channel-${route[2]}`;
- return transcript && channelLink
- ? { root, transcript, name, channelId: route[2], id: `discord:${route[1]}:${route[2]}` }
+ || (directMessageRoute ? `direct-message-${channelId}` : `channel-${channelId}`);
+ return transcript && (channelLink || (directMessageRoute && !selectedDmRouteMismatch))
+ ? {
+ root, transcript, name, channelId,
+ isDirectMessage: !!directMessageRoute,
+ id: directMessageRoute ? `discord:dm:${channelId}` : `discord:${route[1]}:${channelId}`,
+ }
: null;
};
@@ -202,7 +223,7 @@
const identityFor = (composer, root, probe) => {
const channel = discordChannel(composer);
- if (channel?.root === root) return `#${channel.name}`;
+ if (channel?.root === root) return channel.isDirectMessage ? channel.name : `#${channel.name}`;
const explicit = firstAttribute(parentChain(composer), IDENTITY_ATTRIBUTES)
|| firstAttribute([root], IDENTITY_ATTRIBUTES)
|| firstAttribute(queryMany(root, IDENTITY_ATTRIBUTES.map(name => `[${name}]`)), IDENTITY_ATTRIBUTES);
diff --git a/src/firefox/src/content/chat-observation.js b/src/firefox/src/content/chat-observation.js
index ad8e6acc8..58c135523 100644
--- a/src/firefox/src/content/chat-observation.js
+++ b/src/firefox/src/content/chat-observation.js
@@ -138,24 +138,45 @@
const discordChannel = (composer) => {
if (!composer || !/^(?:www\.)?discord\.com$/.test(window.location?.hostname || '')) return null;
const route = /^\/channels\/(\d+)\/(\d+)\/?$/.exec(window.location?.pathname || '');
- if (!route || !composer.matches?.('[data-slate-editor="true"][contenteditable="true"][role="textbox"]')) return null;
+ const directMessageRoute = /^\/channels\/@me\/(\d+)\/?$/.exec(window.location?.pathname || '');
+ if ((!route && !directMessageRoute)
+ || !composer.matches?.('[data-slate-editor="true"][contenteditable="true"][role="textbox"]')) return null;
const root = composer.closest('main,[role="main"]');
if (!root || !visible(root)) return null;
const transcript = query(root, '[data-list-id="chat-messages"]').find(node =>
visible(node) && !contains(node, composer));
+ const channelId = route?.[2] || directMessageRoute[1];
+ const routePath = route
+ ? `/channels/${route[1]}/${channelId}`
+ : `/channels/@me/${channelId}`;
const channelLink = query(document, 'nav a[href],[role="navigation"] a[href]').find(node => {
try {
const url = new URL(attribute(node, 'href'), window.location.href);
return visible(node) && attribute(node, 'aria-current') === 'page' && url.origin === window.location.origin
- && url.pathname.replace(/\/$/, '') === `/channels/${route[1]}/${route[2]}`;
+ && url.pathname.replace(/\/$/, '') === routePath;
} catch { return false; }
});
+ const selectedDmRouteMismatch = !!directMessageRoute
+ && query(document, 'nav a[href][data-list-item-id^="private-channels-"],[role="navigation"] a[href][data-list-item-id^="private-channels-"]')
+ .some(node => {
+ try {
+ const url = new URL(attribute(node, 'href'), window.location.href);
+ return visible(node) && attribute(node, 'aria-current') === 'page'
+ && url.origin === window.location.origin
+ && /^\/channels\/@me\/\d+\/?$/.test(url.pathname)
+ && url.pathname.replace(/\/$/, '') !== routePath;
+ } catch { return false; }
+ });
const name = /^Message #(.+)$/.exec(attribute(composer, 'aria-label'))?.[1]
|| /^Messages in (.+)$/.exec(attribute(transcript, 'aria-label'))?.[1]
|| /^(.+) \(channel\)$/.exec(attribute(root, 'aria-label'))?.[1]
- || `channel-${route[2]}`;
- return transcript && channelLink
- ? { root, transcript, name, channelId: route[2], id: `discord:${route[1]}:${route[2]}` }
+ || (directMessageRoute ? `direct-message-${channelId}` : `channel-${channelId}`);
+ return transcript && (channelLink || (directMessageRoute && !selectedDmRouteMismatch))
+ ? {
+ root, transcript, name, channelId,
+ isDirectMessage: !!directMessageRoute,
+ id: directMessageRoute ? `discord:dm:${channelId}` : `discord:${route[1]}:${channelId}`,
+ }
: null;
};
@@ -202,7 +223,7 @@
const identityFor = (composer, root, probe) => {
const channel = discordChannel(composer);
- if (channel?.root === root) return `#${channel.name}`;
+ if (channel?.root === root) return channel.isDirectMessage ? channel.name : `#${channel.name}`;
const explicit = firstAttribute(parentChain(composer), IDENTITY_ATTRIBUTES)
|| firstAttribute([root], IDENTITY_ATTRIBUTES)
|| firstAttribute(queryMany(root, IDENTITY_ATTRIBUTES.map(name => `[${name}]`)), IDENTITY_ATTRIBUTES);
diff --git a/test/discord-guard.mjs b/test/discord-guard.mjs
index 42bf680a5..c5febcb02 100644
--- a/test/discord-guard.mjs
+++ b/test/discord-guard.mjs
@@ -219,6 +219,33 @@ for (const [kind, engine, AgentClass, policy] of [
assert.notEqual((await probe('click',{selector:'#forward'})).nonMessagingTarget,true);
} finally {await page.close();}
});
+ await t.test('DM routes use their stable route identity and Discord transcript',async()=>{
+ const {page}=await setup();
+ try {
+ await page.locator('#channel').evaluate(el=>{
+ el.setAttribute('href','/channels/@me/456');
+ el.setAttribute('aria-label','Ada direct message');
+ });
+ await page.locator('#composer').evaluate(el=>el.setAttribute('aria-label','Message Ada'));
+ await page.locator('main').evaluate(el=>el.setAttribute('aria-label','Direct message'));
+ await page.locator('[data-list-id=chat-messages]').evaluate(el=>el.setAttribute('aria-label','Conversation'));
+ await page.evaluate(()=>history.replaceState(null,'','/channels/@me/456'));
+ await page.locator('#composer').focus();
+ const observe=()=>page.evaluate(()=>window.__wb_observe_chat_dom({}));
+ const result=await observe();
+ assert.equal(result.success,true,JSON.stringify(result));
+ assert.equal(result.conversationId,'discord:dm:456');
+ assert.equal(result.threadKey,'dom:discord:dm:456');
+ assert.equal(result.conversationIdentity,'direct-message-456');
+ assert.equal(result.messages[0].id,'discord:456:1001');
+
+ await page.locator('#channel').evaluate(el=>el.remove());
+ assert.equal((await observe()).success,true,'a strict DM route plus the composer and transcript retains stable identity without an active link');
+ await page.locator('nav').evaluate(el=>el.insertAdjacentHTML('beforeend',
+ '<a aria-current="page" data-list-item-id="private-channels-uid_11___789" href="/channels/@me/789">Stale DM</a>'));
+ assert.equal((await observe()).success,false,'a conflicting selected DM route fails closed');
+ } finally {await page.close();}
+ });
await t.test('channel observation recognizes the transcript, empty channels and stable server/channel identity',async()=>{
const {page}=await setup();
try {