Skip to content

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

Fix Discord management guard and chat observation

Emre Sokullu50be03810a26
diff --git a/package.json b/package.json
index 1efaac200..75d7c1d55 100644
--- a/package.json
+++ b/package.json
@@ -5,7 +5,7 @@
"private": true,
"type": "module",
"scripts": {
- "test": "npm run test:markdown && npm run test:systemone && npm run test:systemone:fast && node --test test/browser-dialogs.mjs test/capsolver-cloud-broker.mjs test/captcha-providers.mjs test/captcha-weighted-fallback.mjs && npm run test:firefox-bidi && npm run test:runtime-lifecycle && npm run test:provider-limits && npm run test:accessibility-tree-benchmark && npm run test:toolbar-guard && npm run test:pdf-read && npm run test:pdf-selection && npm run test:social-contract && npm run test:safesocial && npm run test:build-unpacked && npm run test:attachment-drop && npm run test:workflow-editor && node --test test/profile-sync-stats.mjs && node test/run.js && node test/selection-scope-restoration.mjs && node scripts/benchmark-offline-relevance.mjs && npm run test:security",
+ "test": "npm run test:markdown && npm run test:systemone && npm run test:systemone:fast && node --test test/browser-dialogs.mjs test/capsolver-cloud-broker.mjs test/captcha-providers.mjs test/captcha-weighted-fallback.mjs && npm run test:firefox-bidi && npm run test:runtime-lifecycle && npm run test:provider-limits && npm run test:accessibility-tree-benchmark && npm run test:toolbar-guard && npm run test:discord && npm run test:pdf-read && npm run test:pdf-selection && npm run test:social-contract && npm run test:safesocial && npm run test:build-unpacked && npm run test:attachment-drop && npm run test:workflow-editor && node --test test/profile-sync-stats.mjs && node test/run.js && node test/selection-scope-restoration.mjs && node scripts/benchmark-offline-relevance.mjs && npm run test:security",
"test:captcha:ui": "node test/captcha-settings-ui.mjs",
"test:provider-limits": "node test/provider-model-limits.mjs",
"test:markdown": "node --test test/markdown-render.mjs",
@@ -13,6 +13,7 @@
"test:attachment-drop": "node test/attachment-drop.mjs",
"test:workflow-editor": "node --test test/workflow-editor.mjs",
"test:security": "node test/security/injection-corpus.mjs",
+ "test:discord": "node --test test/discord-guard.mjs",
"test:toolbar-guard": "node test/rich-text-toolbar-guard.mjs",
"test:pdf-read": "node test/pdf-read.mjs",
"test:pdf-selection": "node test/pdf-selection.mjs",
diff --git a/src/chrome/src/agent/adapters.js b/src/chrome/src/agent/adapters.js
index e8ecd1dc8..fba21f5f1 100644
--- a/src/chrome/src/agent/adapters.js
+++ b/src/chrome/src/agent/adapters.js
@@ -17601,6 +17601,7 @@ const ADAPTERS = [
{
name: 'discord',
category: 'general',
+ messaging: { verifyActiveRecipient: true },
matches: (url) => /^https?:\/\/(www\.)?discord\.com\/(channels|app)/.test(url) || /^https?:\/\/(www\.)?discord\.com\/$/.test(url),
notes: `
- Three-pane layout: Server list (icons, left-most rail) → Channel list (per-server, second rail) → Channel chat (main pane). Selecting a server reveals its channels; selecting a channel loads its message history.
diff --git a/src/chrome/src/content/chat-observation.js b/src/chrome/src/content/chat-observation.js
index 6268aa714..8f7da8d28 100644
--- a/src/chrome/src/content/chat-observation.js
+++ b/src/chrome/src/content/chat-observation.js
@@ -135,8 +135,32 @@
return attributePresent && !normalized ? true : null;
};
+ const discordChannel = (composer) => {
+ if (!composer || !/^(?:www\.)?discord\.com$/.test(window.location?.hostname || '')) return null;
+ const route = /^\/channels\/(\d+)\/(\d+)\/?$/.exec(window.location?.pathname || '');
+ const name = /^Message #(.+)$/.exec(attribute(composer, 'aria-label'))?.[1];
+ if (!route || !name || !composer.matches?.('[data-slate-editor="true"][contenteditable="true"][role="textbox"]')) return null;
+ const root = composer.closest('main,[role="main"]');
+ if (!root || attribute(root, 'aria-label') !== `${name} (channel)` || !visible(root)) return null;
+ const transcript = query(root, '[role="list"],ol,ul').find(node =>
+ attribute(node, 'aria-label') === `Messages in ${name}` && visible(node) && !contains(node, composer));
+ 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]}`
+ && attribute(node, 'aria-label') === `${name} (text channel)`;
+ } catch { return false; }
+ });
+ return transcript && channelLink
+ ? { root, transcript, name, channelId: route[2], id: `discord:${route[1]}:${route[2]}` }
+ : null;
+ };
+
const activeRoot = (composer) => {
if (!composer) return null;
+ const channel = discordChannel(composer);
+ if (channel) return channel.root;
const chain = parentChain(composer);
const explicit = chain.find(node => THREAD_ID_ATTRIBUTES.some(name => attribute(node, name))
|| CHAT_ROOT_SELECTORS.some(selector => {
@@ -161,6 +185,8 @@
};
const markerFor = (composer, root) => {
+ const channel = discordChannel(composer);
+ if (channel?.root === root) return { value: channel.id };
const fromComposer = firstAttribute(parentChain(composer), THREAD_ID_ATTRIBUTES);
if (fromComposer) return fromComposer;
const activeMarker = queryMany(root, THREAD_ID_ATTRIBUTES.map(name => `[${name}]`))
@@ -173,6 +199,8 @@
};
const identityFor = (composer, root, probe) => {
+ const channel = discordChannel(composer);
+ if (channel?.root === root) return `#${channel.name}`;
const explicit = firstAttribute(parentChain(composer), IDENTITY_ATTRIBUTES)
|| firstAttribute([root], IDENTITY_ATTRIBUTES)
|| firstAttribute(queryMany(root, IDENTITY_ATTRIBUTES.map(name => `[${name}]`)), IDENTITY_ATTRIBUTES);
@@ -228,7 +256,78 @@
}
};
+ const discordAvatarUserId = (node) => {
+ try {
+ const url = new URL(attribute(node, 'src'), window.location.href);
+ return url.hostname === 'cdn.discordapp.com'
+ ? /^\/avatars\/(\d+)\//.exec(url.pathname)?.[1] || ''
+ : '';
+ } catch { return ''; }
+ };
+
+ const discordMessageText = (content) => {
+ const read = (node) => {
+ if (node?.nodeType === 3) return node.nodeValue || '';
+ if (node?.nodeType !== 1 || node.hidden || attribute(node, 'aria-hidden') === 'true') return '';
+ const style = getComputedStyle(node);
+ if (style.display === 'none' || style.visibility === 'hidden') return '';
+ const tag = String(node.tagName || '').toLowerCase();
+ if (tag === 'br') return '\n';
+ if (tag === 'img') return node.matches?.('.emoji,[data-type="emoji"]') ? attribute(node, 'alt') : '';
+ const text = Array.from(node.childNodes || []).map(read).join('');
+ return /^(?:div|p|li|blockquote|pre)$/.test(tag) ? `${text}\n` : text;
+ };
+ return normalizeText(read(content));
+ };
+
+ const collectDiscordMessages = (channel) => {
+ const panel = query(document, 'section[aria-label="User status and settings"]')[0];
+ const ownAvatarId = discordAvatarUserId(query(panel, 'img[src*="/avatars/"]')[0]);
+ const ownName = compact(query(panel, '[class*="panelTitle"]')[0]?.textContent, 240);
+ const rows = query(channel.transcript, '[role="article"][data-list-item-id]');
+ const messages = [];
+ let previousAuthor = '';
+ let previousDirection = 'unknown';
+ for (const row of rows) {
+ const match = /^chat-messages___chat-messages-(\d+)-(\d+)$/.exec(attribute(row, 'data-list-item-id'));
+ if (!match || match[1] !== channel.channelId || !visible(row)) continue;
+ const messageId = match[2];
+ const usernameNode = query(row, `[id="message-username-${messageId}"]`)[0];
+ const username = query(usernameNode, '[data-text]')[0] || usernameNode;
+ const system = !username && /(?:^|\s)(?:isSystemMessage|systemMessage)/.test(String(row.className || ''));
+ if (system) {
+ previousAuthor = '';
+ previousDirection = 'unknown';
+ continue;
+ }
+ const content = query(row, `[id="message-content-${messageId}"]`)[0];
+ const body = discordMessageText(content);
+ const author = compact(attribute(username, 'data-text') || username?.textContent, 240)
+ || (!username ? previousAuthor : '');
+ const avatar = username?.closest('h3')?.parentElement?.querySelector('img[src]');
+ const avatarId = discordAvatarUserId(avatar);
+ let direction = 'unknown';
+ if (ownAvatarId && avatarId) direction = ownAvatarId === avatarId ? 'outgoing' : 'incoming';
+ else if (author && ownName && author !== ownName) direction = 'incoming';
+ else if (!username) direction = previousDirection;
+ previousAuthor = author;
+ previousDirection = direction;
+ if (!body) continue;
+ const timestamp = attribute(query(row, `[id="message-timestamp-${messageId}"]`)[0], 'datetime');
+ messages.push({
+ id: `discord:${channel.channelId}:${messageId}`,
+ direction,
+ text: body,
+ ...(author ? { author } : {}),
+ ...(timestamp ? { timestamp } : {}),
+ });
+ }
+ return messages.slice(-MAX_ITEMS);
+ };
+
const collectMessages = (root, composer) => {
+ const channel = discordChannel(composer);
+ if (channel?.root === root) return collectDiscordMessages(channel);
const candidates = queryMany(root, MESSAGE_SELECTORS)
.filter(node => visible(node) && !ignoredMessageNode(node, composer))
.map((node, index) => {
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index 585a035eb..68ad2eddf 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -5873,6 +5873,75 @@
}
};
+ const verifiedDiscordManagementControl = (clicked) => {
+ if (params.adapterName !== 'discord'
+ || !/^(?:www\.)?discord\.com$/.test(location.hostname)
+ || !/^\/channels\/\d+(?:\/\d+)?\/?$/.test(location.pathname)) return false;
+ const control = _composedClosestElement(clicked,
+ 'button,a[href],input,select,textarea,[role="textbox"],[role="button"],[role="menuitem"],[role="tab"],[role="switch"],[role="radio"],[role="checkbox"]');
+ if (!control || !visible(control) || control.disabled
+ || control.getAttribute('aria-disabled') === 'true'
+ || _composedClosestElement(control,
+ 'article,[role="log"],[data-message-id],[data-list-item-id^="chat-messages"]')) return false;
+ const label = compact(control.getAttribute('aria-label') || control.innerText || control.textContent);
+ const messageCommit = /(?:^|[^\p{L}])(?:send|enviar|envoyer|invia|senden|verzenden|gönder|отправить|送信|发送|發送|보내|إرسال|ارسال)(?:[^\p{L}]|$)/iu;
+ const actionLabels = [control.getAttribute('aria-label'), control.getAttribute('title'),
+ ...(!editable(control) ? [control.innerText || control.textContent, control.value] : [])];
+ if (actionLabels.some(value => messageCommit.test(value || ''))) return false;
+ const modal = _findTopmostBlockingModal();
+ if (modal && !_isComposedAncestor(modal, control)) return false;
+ const rect = control.getBoundingClientRect();
+ if (!_isComposedAncestor(control, _shadowAwareElementFromPoint(
+ rect.left + rect.width / 2, rect.top + rect.height / 2))) return false;
+ const dialog = _composedClosestElement(control, 'dialog,[role="dialog"],[role="alertdialog"]');
+ if (dialog) {
+ // 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 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
+ && ['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;
+ return (creation || serverSettings || channelSettings) && !control.hasAttribute('form');
+ }
+ if (editable(control) || control.form || control.hasAttribute('form')
+ || _composedClosestElement(control, 'form')) return false;
+ const menu = _composedClosestElement(control, '[role="menu"]');
+ if (menu) {
+ const menuActions = {
+ 'guild-header-popout-settings': 'Server Settings',
+ 'guild-header-popout-create-channel': 'Create Channel',
+ 'guild-header-popout-create-category': 'Create Category',
+ };
+ return menu.id === 'guild-header-popout'
+ && control.getAttribute('role') === 'menuitem'
+ && menuActions[control.id] === label;
+ }
+ const nav = _composedClosestElement(control, 'nav,[role="navigation"]');
+ if (!nav || !/\(server\)$/.test(compact(nav.getAttribute('aria-label')))) return false;
+ if (control.matches('a[href]')) {
+ const destination = new URL(control.getAttribute('href'), location.href);
+ return destination.origin === location.origin && !destination.search && !destination.hash
+ && !destination.username && !destination.password
+ && /^\/channels\/\d+\/\d+\/?$/.test(destination.pathname);
+ }
+ return /^(?:Create Channel|Edit Channel|Create Category)$/i.test(label)
+ || (control.hasAttribute('aria-expanded')
+ && (/[, ]server actions$/i.test(label) || /\(category\)$/.test(label)));
+ };
+
const verifiedLinkedInPostEntry = (clicked) => {
if (params.adapterName !== 'linkedin' || !/^\/feed\/?$/.test(location.pathname)) return false;
const button = _composedClosestElement(clicked, 'button,[role="button"]');
@@ -5948,6 +6017,15 @@
let composer = null;
let messageSend = null;
+ const discordManagementTarget = tool === 'press_keys' && String(args.key) === 'Enter'
+ ? active : (targetResolved ? target : null);
+ if (!observationOnly && ['click', 'click_ax', 'set_field', 'press_keys'].includes(tool)
+ && verifiedDiscordManagementControl(discordManagementTarget)) {
+ return {
+ success: true, messageSend: false, conclusive: true,
+ nonMessagingTarget: true, reasonCode: 'non_messaging_target', identityCandidates: [],
+ };
+ }
if (observationOnly) {
composer = layoutComposer;
} else if (tool === 'press_keys') {
diff --git a/src/firefox/src/agent/adapters.js b/src/firefox/src/agent/adapters.js
index 6ef076c3b..d54096541 100644
--- a/src/firefox/src/agent/adapters.js
+++ b/src/firefox/src/agent/adapters.js
@@ -17599,6 +17599,7 @@ const ADAPTERS = [
{
name: 'discord',
category: 'general',
+ messaging: { verifyActiveRecipient: true },
matches: (url) => /^https?:\/\/(www\.)?discord\.com\/(channels|app)/.test(url) || /^https?:\/\/(www\.)?discord\.com\/$/.test(url),
notes: `
- Three-pane layout: Server list (icons, left-most rail) → Channel list (per-server, second rail) → Channel chat (main pane). Selecting a server reveals its channels; selecting a channel loads its message history.
diff --git a/src/firefox/src/content/chat-observation.js b/src/firefox/src/content/chat-observation.js
index 6268aa714..8f7da8d28 100644
--- a/src/firefox/src/content/chat-observation.js
+++ b/src/firefox/src/content/chat-observation.js
@@ -135,8 +135,32 @@
return attributePresent && !normalized ? true : null;
};
+ const discordChannel = (composer) => {
+ if (!composer || !/^(?:www\.)?discord\.com$/.test(window.location?.hostname || '')) return null;
+ const route = /^\/channels\/(\d+)\/(\d+)\/?$/.exec(window.location?.pathname || '');
+ const name = /^Message #(.+)$/.exec(attribute(composer, 'aria-label'))?.[1];
+ if (!route || !name || !composer.matches?.('[data-slate-editor="true"][contenteditable="true"][role="textbox"]')) return null;
+ const root = composer.closest('main,[role="main"]');
+ if (!root || attribute(root, 'aria-label') !== `${name} (channel)` || !visible(root)) return null;
+ const transcript = query(root, '[role="list"],ol,ul').find(node =>
+ attribute(node, 'aria-label') === `Messages in ${name}` && visible(node) && !contains(node, composer));
+ 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]}`
+ && attribute(node, 'aria-label') === `${name} (text channel)`;
+ } catch { return false; }
+ });
+ return transcript && channelLink
+ ? { root, transcript, name, channelId: route[2], id: `discord:${route[1]}:${route[2]}` }
+ : null;
+ };
+
const activeRoot = (composer) => {
if (!composer) return null;
+ const channel = discordChannel(composer);
+ if (channel) return channel.root;
const chain = parentChain(composer);
const explicit = chain.find(node => THREAD_ID_ATTRIBUTES.some(name => attribute(node, name))
|| CHAT_ROOT_SELECTORS.some(selector => {
@@ -161,6 +185,8 @@
};
const markerFor = (composer, root) => {
+ const channel = discordChannel(composer);
+ if (channel?.root === root) return { value: channel.id };
const fromComposer = firstAttribute(parentChain(composer), THREAD_ID_ATTRIBUTES);
if (fromComposer) return fromComposer;
const activeMarker = queryMany(root, THREAD_ID_ATTRIBUTES.map(name => `[${name}]`))
@@ -173,6 +199,8 @@
};
const identityFor = (composer, root, probe) => {
+ const channel = discordChannel(composer);
+ if (channel?.root === root) return `#${channel.name}`;
const explicit = firstAttribute(parentChain(composer), IDENTITY_ATTRIBUTES)
|| firstAttribute([root], IDENTITY_ATTRIBUTES)
|| firstAttribute(queryMany(root, IDENTITY_ATTRIBUTES.map(name => `[${name}]`)), IDENTITY_ATTRIBUTES);
@@ -228,7 +256,78 @@
}
};
+ const discordAvatarUserId = (node) => {
+ try {
+ const url = new URL(attribute(node, 'src'), window.location.href);
+ return url.hostname === 'cdn.discordapp.com'
+ ? /^\/avatars\/(\d+)\//.exec(url.pathname)?.[1] || ''
+ : '';
+ } catch { return ''; }
+ };
+
+ const discordMessageText = (content) => {
+ const read = (node) => {
+ if (node?.nodeType === 3) return node.nodeValue || '';
+ if (node?.nodeType !== 1 || node.hidden || attribute(node, 'aria-hidden') === 'true') return '';
+ const style = getComputedStyle(node);
+ if (style.display === 'none' || style.visibility === 'hidden') return '';
+ const tag = String(node.tagName || '').toLowerCase();
+ if (tag === 'br') return '\n';
+ if (tag === 'img') return node.matches?.('.emoji,[data-type="emoji"]') ? attribute(node, 'alt') : '';
+ const text = Array.from(node.childNodes || []).map(read).join('');
+ return /^(?:div|p|li|blockquote|pre)$/.test(tag) ? `${text}\n` : text;
+ };
+ return normalizeText(read(content));
+ };
+
+ const collectDiscordMessages = (channel) => {
+ const panel = query(document, 'section[aria-label="User status and settings"]')[0];
+ const ownAvatarId = discordAvatarUserId(query(panel, 'img[src*="/avatars/"]')[0]);
+ const ownName = compact(query(panel, '[class*="panelTitle"]')[0]?.textContent, 240);
+ const rows = query(channel.transcript, '[role="article"][data-list-item-id]');
+ const messages = [];
+ let previousAuthor = '';
+ let previousDirection = 'unknown';
+ for (const row of rows) {
+ const match = /^chat-messages___chat-messages-(\d+)-(\d+)$/.exec(attribute(row, 'data-list-item-id'));
+ if (!match || match[1] !== channel.channelId || !visible(row)) continue;
+ const messageId = match[2];
+ const usernameNode = query(row, `[id="message-username-${messageId}"]`)[0];
+ const username = query(usernameNode, '[data-text]')[0] || usernameNode;
+ const system = !username && /(?:^|\s)(?:isSystemMessage|systemMessage)/.test(String(row.className || ''));
+ if (system) {
+ previousAuthor = '';
+ previousDirection = 'unknown';
+ continue;
+ }
+ const content = query(row, `[id="message-content-${messageId}"]`)[0];
+ const body = discordMessageText(content);
+ const author = compact(attribute(username, 'data-text') || username?.textContent, 240)
+ || (!username ? previousAuthor : '');
+ const avatar = username?.closest('h3')?.parentElement?.querySelector('img[src]');
+ const avatarId = discordAvatarUserId(avatar);
+ let direction = 'unknown';
+ if (ownAvatarId && avatarId) direction = ownAvatarId === avatarId ? 'outgoing' : 'incoming';
+ else if (author && ownName && author !== ownName) direction = 'incoming';
+ else if (!username) direction = previousDirection;
+ previousAuthor = author;
+ previousDirection = direction;
+ if (!body) continue;
+ const timestamp = attribute(query(row, `[id="message-timestamp-${messageId}"]`)[0], 'datetime');
+ messages.push({
+ id: `discord:${channel.channelId}:${messageId}`,
+ direction,
+ text: body,
+ ...(author ? { author } : {}),
+ ...(timestamp ? { timestamp } : {}),
+ });
+ }
+ return messages.slice(-MAX_ITEMS);
+ };
+
const collectMessages = (root, composer) => {
+ const channel = discordChannel(composer);
+ if (channel?.root === root) return collectDiscordMessages(channel);
const candidates = queryMany(root, MESSAGE_SELECTORS)
.filter(node => visible(node) && !ignoredMessageNode(node, composer))
.map((node, index) => {
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index 43cb794ad..cd731f603 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -4951,6 +4951,75 @@
}
};
+ const verifiedDiscordManagementControl = (clicked) => {
+ if (params.adapterName !== 'discord'
+ || !/^(?:www\.)?discord\.com$/.test(location.hostname)
+ || !/^\/channels\/\d+(?:\/\d+)?\/?$/.test(location.pathname)) return false;
+ const control = _composedClosestElement(clicked,
+ 'button,a[href],input,select,textarea,[role="textbox"],[role="button"],[role="menuitem"],[role="tab"],[role="switch"],[role="radio"],[role="checkbox"]');
+ if (!control || !visible(control) || control.disabled
+ || control.getAttribute('aria-disabled') === 'true'
+ || _composedClosestElement(control,
+ 'article,[role="log"],[data-message-id],[data-list-item-id^="chat-messages"]')) return false;
+ const label = compact(control.getAttribute('aria-label') || control.innerText || control.textContent);
+ const messageCommit = /(?:^|[^\p{L}])(?:send|enviar|envoyer|invia|senden|verzenden|gönder|отправить|送信|发送|發送|보내|إرسال|ارسال)(?:[^\p{L}]|$)/iu;
+ const actionLabels = [control.getAttribute('aria-label'), control.getAttribute('title'),
+ ...(!editable(control) ? [control.innerText || control.textContent, control.value] : [])];
+ if (actionLabels.some(value => messageCommit.test(value || ''))) return false;
+ const modal = _findTopmostBlockingModal();
+ if (modal && !_isComposedAncestor(modal, control)) return false;
+ const rect = control.getBoundingClientRect();
+ if (!_isComposedAncestor(control, _shadowAwareElementFromPoint(
+ rect.left + rect.width / 2, rect.top + rect.height / 2))) return false;
+ const dialog = _composedClosestElement(control, 'dialog,[role="dialog"],[role="alertdialog"]');
+ if (dialog) {
+ // 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 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
+ && ['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;
+ return (creation || serverSettings || channelSettings) && !control.hasAttribute('form');
+ }
+ if (editable(control) || control.form || control.hasAttribute('form')
+ || _composedClosestElement(control, 'form')) return false;
+ const menu = _composedClosestElement(control, '[role="menu"]');
+ if (menu) {
+ const menuActions = {
+ 'guild-header-popout-settings': 'Server Settings',
+ 'guild-header-popout-create-channel': 'Create Channel',
+ 'guild-header-popout-create-category': 'Create Category',
+ };
+ return menu.id === 'guild-header-popout'
+ && control.getAttribute('role') === 'menuitem'
+ && menuActions[control.id] === label;
+ }
+ const nav = _composedClosestElement(control, 'nav,[role="navigation"]');
+ if (!nav || !/\(server\)$/.test(compact(nav.getAttribute('aria-label')))) return false;
+ if (control.matches('a[href]')) {
+ const destination = new URL(control.getAttribute('href'), location.href);
+ return destination.origin === location.origin && !destination.search && !destination.hash
+ && !destination.username && !destination.password
+ && /^\/channels\/\d+\/\d+\/?$/.test(destination.pathname);
+ }
+ return /^(?:Create Channel|Edit Channel|Create Category)$/i.test(label)
+ || (control.hasAttribute('aria-expanded')
+ && (/[, ]server actions$/i.test(label) || /\(category\)$/.test(label)));
+ };
+
const verifiedLinkedInPostEntry = (clicked) => {
if (params.adapterName !== 'linkedin' || !/^\/feed\/?$/.test(location.pathname)) return false;
const button = _composedClosestElement(clicked, 'button,[role="button"]');
@@ -5026,6 +5095,15 @@
let composer = null;
let messageSend = null;
+ const discordManagementTarget = tool === 'press_keys' && String(args.key) === 'Enter'
+ ? active : (targetResolved ? target : null);
+ if (!observationOnly && ['click', 'click_ax', 'set_field', 'press_keys'].includes(tool)
+ && verifiedDiscordManagementControl(discordManagementTarget)) {
+ return {
+ success: true, messageSend: false, conclusive: true,
+ nonMessagingTarget: true, reasonCode: 'non_messaging_target', identityCandidates: [],
+ };
+ }
if (observationOnly) {
composer = layoutComposer;
} else if (tool === 'press_keys') {
diff --git a/test/discord-guard.mjs b/test/discord-guard.mjs
new file mode 100644
index 000000000..d2cb549fc
--- /dev/null
+++ b/test/discord-guard.mjs
@@ -0,0 +1,234 @@
+import { strict as assert } from 'node:assert';
+import { readFile } from 'node:fs/promises';
+import { test } from 'node:test';
+import { chromium, firefox } from 'playwright';
+import { Agent } from '../src/chrome/src/agent/agent.js';
+import { Agent as FirefoxAgent } from '../src/firefox/src/agent/agent.js';
+import { getMessageRecipientGuardPolicy } from '../src/chrome/src/agent/adapters.js';
+import { getMessageRecipientGuardPolicy as firefoxPolicy } from '../src/firefox/src/agent/adapters.js';
+import { advanceChatSession, createChatSession } from '../src/chrome/src/agent/chat-workflow.js';
+
+const url = 'https://discord.com/channels/123/456';
+const fixture = `<!doctype html><style>
+body {margin:0;font:16px sans-serif} nav {width:270px} button,[role=button],[role=menuitem],a {display:block;padding:8px}
+ol {min-height:30px} main {position:fixed;left:320px;top:0;width:550px;height:700px}
+#composer {position:fixed;left:330px;bottom:20px;width:420px;min-height:50px}
+#send {position:fixed;left:760px;bottom:20px}
+[role=dialog] {position:fixed;inset:40px;background:white;z-index:100;padding:20px}
+[role=dialog] main {position:static;height:auto} [role=menu] {position:fixed;left:20px;top:250px;background:white;z-index:10}
+[hidden] {display:none!important}
+</style><nav aria-label="Test (server)">
+<header><div id="server" role="button" tabindex="0" aria-label="Test, server actions" aria-expanded="false"><h2>Test</h2></div></header>
+<button id="create" type="button" aria-label="Create Channel">+</button>
+<button id="category" type="button" aria-expanded="true">Text Channels (category)</button>
+<a id="channel" href="/channels/123/456" aria-label="general (text channel)" aria-current="page">general</a>
+<button id="edit" type="button" aria-label="Edit Channel">Edit Channel</button>
+<button id="nav-send" type="button">Send message</button></nav>
+<section aria-label="User status and settings"><div class="panelTitleContainer_fixture">WebBrain</div><img src="https://cdn.discordapp.com/avatars/11/self.webp?size=56"></section>
+<main aria-label="general (channel)"><h2>general chat</h2><ol role="list" aria-label="Messages in general" data-list-id="chat-messages">
+<li><div id="message" role="article" data-list-item-id="chat-messages___chat-messages-456-1001"><div class="contents"><img src="https://cdn.discordapp.com/avatars/22/other.webp?size=160"><h3><span id="message-username-1001"><span data-text="Ficsit">Ficsit</span></span><time id="message-timestamp-1001" datetime="2026-09-29T01:00:00.000Z"></time></h3><div id="message-content-1001">Hello from the fixture</div></div><div role="group" aria-label="Message Actions"><button id="wave">Wave to say hi!</button><button id="lookalike" aria-expanded="false">Test, server actions</button></div></div></li>
+</ol><div id="composer" role="textbox" aria-label="Message #general" contenteditable="true" data-slate-editor="true">Draft</div><button id="send" type="button">Send</button></main>`;
+const call = (page, action, params={}) => page.evaluate(({action,params}) => new Promise(resolve => {
+ const result = window.__wb_handler({target:'content',action,params},{},resolve);
+ if (result !== true && result !== undefined) resolve(result);
+}), {action,params});
+
+for (const [kind, engine, AgentClass, policy] of [
+ ['chrome',chromium,Agent,getMessageRecipientGuardPolicy],
+ ['firefox',firefox,FirefoxAgent,firefoxPolicy],
+]) {
+ test(`${kind}: Discord management and observation regressions`, async t => {
+ assert.deepEqual(policy(url), {adapterName:'discord',verifyActiveRecipient:true});
+ assert.deepEqual(policy('https://discord.com/channels/@me/456'), {adapterName:'discord',verifyActiveRecipient:true});
+ assert.notEqual(policy('https://discord.com.evil.example/channels/123/456')?.adapterName,'discord');
+ const browser = await engine.launch();
+ const sources = await Promise.all(['accessibility-tree.js','rich-text-toolbar-heuristic.js','chat-observation.js','content.js']
+ .map(file=>readFile(new URL(`../src/${kind}/src/content/${file}`,import.meta.url),'utf8')));
+ const setup = async (extra='') => {
+ const page=await browser.newPage({viewport:{width:1000,height:800}});
+ await page.route('**/*',route=>route.fulfill({contentType:'text/html',body:fixture+extra}));
+ await page.addInitScript(() => {
+ const runtime={onMessage:{addListener:fn=>{window.__wb_handler=fn;}},getURL:path=>path};
+ window.chrome={runtime};window.browser={runtime};
+ window.fixtureClicks=[];
+ document.addEventListener('click',event=>{event.preventDefault();window.fixtureClicks.push(event.target.closest('[id]')?.id);});
+ });
+ await page.goto(url);
+ for (const source of sources) await page.addScriptTag({content:source});
+ const agent=new AgentClass({getActive:()=>({supportsVision:false})});
+ agent._messageRecipientContentProbe=(_,params)=>call(page,'probe_message_recipient_guard',params);
+ const guard=async(tool,args)=>{
+ if(tool==='set_field' && args.selector){
+ await call(page,'get_accessibility_tree',{filter:'all',maxChars:30000});
+ args={...args,ref_id:await page.locator(args.selector).evaluate(el=>window.__wb_ax_ref(el))};
+ }
+ return agent._messageRecipientGuardBlock(1,tool,args,page.url());
+ };
+ const probe=(tool,args)=>call(page,'probe_message_recipient_guard',{tool,args,adapterName:'discord'});
+ return {page,guard,probe};
+ };
+ try {
+ await t.test('server menu works by selector, text, AX ref, index and coordinates with or without a composer',async()=>{
+ const {page,guard,probe}=await setup();
+ try {
+ for (const withComposer of [true,false]) {
+ if(!withComposer) await page.locator('#composer').evaluate(el=>el.remove());
+ await call(page,'get_accessibility_tree',{filter:'all',maxChars:30000});
+ const ref=await page.locator('#server').evaluate(el=>window.__wb_ax_ref(el));
+ const items=await call(page,'get_interactive_elements');
+ const index=items.findIndex(el=>el.id==='server');
+ assert.ok(index>=0);
+ const rect=await page.locator('#server').boundingBox();
+ for (const [tool,args] of [['click',{selector:'#server'}],['click',{text:'Test'}],
+ ['click_ax',{ref_id:ref}],['click',{index}],['click',{x:rect.x+rect.width/2,y:rect.y+rect.height/2}]]) {
+ const classified=await probe(tool,args);
+ assert.equal(classified.nonMessagingTarget,true,JSON.stringify({tool,args,classified}));
+ assert.equal(await guard(tool,args),null);
+ const result=await call(page,tool,args);
+ assert.equal(result.success,true,JSON.stringify(result));
+ }
+ }
+ assert.equal((await page.evaluate(()=>window.fixtureClicks)).filter(id=>id==='server').length,10);
+ for(const selector of ['#create','#category','#channel','#edit']) assert.equal(await guard('click',{selector}),null);
+ } finally {await page.close();}
+ });
+ await t.test('only the known server menu entries bypass message classification',async()=>{
+ const {page,guard,probe}=await setup(`<div role="menu" id="guild-header-popout">
+<div role="menuitem" id="guild-header-popout-settings">Server Settings</div>
+<div role="menuitem" id="guild-header-popout-create-channel">Create Channel</div>
+<div role="menuitem" id="guild-header-popout-create-category">Create Category</div>
+<div role="menuitem" id="unknown">Forward</div></div>`);
+ try {
+ for(const id of ['settings','create-channel','create-category']) assert.equal(await guard('click',{selector:`#guild-header-popout-${id}`}),null);
+ assert.equal((await guard('click',{selector:'#unknown'}))?.noDispatch,true);
+ await page.locator('#guild-header-popout-settings').evaluate(el=>el.textContent='Send invitation');
+ assert.notEqual((await probe('click',{selector:'#guild-header-popout-settings'})).nonMessagingTarget,true);
+ } 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>`);
+ try {
+ 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);
+ await page.locator('#name').focus();
+ assert.equal(await guard('press_keys',{key:'Enter'}),null);
+ assert.equal((await guard('click',{selector:'#server'}))?.noDispatch,true,'background menu stays blocked');
+ } finally {await page.close();}
+ }
+ });
+ await t.test('server settings are recognized by their own navigation, including non-message textareas',async()=>{
+ const {page,guard}=await setup(`<div role="dialog" aria-modal="true"><nav><div role="tablist"><div role="tab" id="roles" aria-label="Manage Roles">Roles</div><div role="tab">Safety Setup</div></div></nav><main><textarea id="description">Server description</textarea><button id="save">Save Changes</button></main><button id="test-send">Send test message</button></div>`);
+ try {
+ for(const selector of ['#roles','#description','#save']) assert.equal(await guard('click',{selector}),null);
+ assert.equal(await guard('set_field',{selector:'#description',value:'new',submit:true}),null);
+ assert.equal((await guard('click',{selector:'#test-send'}))?.noDispatch,true);
+ } finally {await page.close();}
+ });
+ await t.test('channel settings allow the Slate topic editor but not a message composer',async()=>{
+ const {page,guard,probe}=await setup(`<div role="dialog" data-layer="CHANNEL_SETTINGS" aria-modal="true" aria-label="Channel Settings">
+<nav><div role="tablist"><div role="tab">Overview</div><div id="permissions" role="tab">Permissions</div></div></nav>
+<div role="tabpanel"><input type="text" aria-label="Channel Name"><div id="topic" role="textbox" contenteditable="true" data-slate-editor="true" aria-label="Let everyone know how to use this channel!">Send bug reports here</div><button id="save">Save Changes</button><input id="input-send" type="submit" value="Send"></div></div>`);
+ try {
+ for(const selector of ['#permissions','#topic','#save']) assert.equal(await guard('click',{selector}),null);
+ assert.equal(await guard('set_field',{selector:'#topic',value:'new topic',submit:true}),null);
+ await page.locator('#topic').focus();
+ assert.equal(await guard('press_keys',{key:'Enter'}),null);
+ assert.notEqual((await probe('click',{selector:'#input-send'})).nonMessagingTarget,true);
+ await page.locator('#topic').evaluate(el=>el.setAttribute('aria-label','Message #general'));
+ assert.notEqual((await probe('click',{selector:'#save'})).nonMessagingTarget,true);
+ } finally {await page.close();}
+ });
+ await t.test('sends, message actions, lookalikes and foreign origins remain protected',async()=>{
+ const {page,guard,probe}=await setup();
+ try {
+ for(const selector of ['#send','#nav-send','#wave','#lookalike']) {
+ assert.notEqual((await probe('click',{selector})).nonMessagingTarget,true);
+ assert.equal((await guard('click',{selector}))?.noDispatch,true);
+ }
+ await page.locator('#composer').focus();
+ assert.equal((await guard('press_keys',{key:'Enter'}))?.noDispatch,true);
+ assert.equal((await guard('set_field',{selector:'#composer',value:'Draft',submit:true}))?.noDispatch,true);
+ await page.locator('#server').evaluate(el=>el.setAttribute('aria-disabled','true'));
+ assert.notEqual((await probe('click',{selector:'#server'})).nonMessagingTarget,true);
+ await page.locator('#server').evaluate(el=>{
+ el.removeAttribute('aria-disabled');
+ const overlay=document.createElement('div');overlay.id='overlay';
+ overlay.style='position:fixed;inset:0;background:white;z-index:999';document.body.append(overlay);
+ });
+ assert.notEqual((await probe('click',{selector:'#server'})).nonMessagingTarget,true,'covered controls fail closed');
+ await page.goto('https://discord.com.evil.example/channels/123/456');
+ for(const source of sources) await page.addScriptTag({content:source});
+ assert.notEqual((await probe('click',{selector:'#server'})).nonMessagingTarget,true,'origin is checked even with an adapter hint');
+ await page.locator('#composer').focus();
+ const result=await page.evaluate(()=>window.__wb_observe_chat_dom({}));
+ assert.equal(result.success,false);
+ } finally {await page.close();}
+ });
+ await t.test('unknown dialogs and chat editors inside settings never gain management classification',async()=>{
+ const {page,probe}=await setup(`<div role="dialog" aria-modal="true" aria-label="Forward"><h1>Forward</h1><input type="text"><button id="forward">Create Channel</button></div>`);
+ try {
+ assert.notEqual((await probe('click',{selector:'#forward'})).nonMessagingTarget,true);
+ await page.locator('[role=dialog]').evaluate(el=>{
+ el.setAttribute('aria-label','Create Channel');el.querySelector('h1').textContent='Create Channel';
+ const editor=document.createElement('div');editor.setAttribute('data-slate-editor','true');el.append(editor);
+ });
+ assert.notEqual((await probe('click',{selector:'#forward'})).nonMessagingTarget,true);
+ } 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 {
+ 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:123:456');
+ assert.equal(result.threadKey,'dom:discord:123:456');
+ assert.equal(result.conversationIdentity,'#general');
+ assert.deepEqual(result.messages,[{
+ id:'discord:456:1001', direction:'incoming', text:'Hello from the fixture',
+ author:'Ficsit', timestamp:'2026-09-29T01:00:00.000Z',
+ }]);
+ let advanced=advanceChatSession(createChatSession({threadKey:result.threadKey}),result);
+ await page.locator('#wave').evaluate(el=>el.textContent='Changed hover action');
+ const afterHover=await observe();
+ assert.deepEqual(afterHover.messages,result.messages,'message actions do not alter observed content');
+ advanced=advanceChatSession(advanced.session,afterHover);
+ assert.deepEqual(advanced.newMessages,[],'hover does not create a new message');
+
+ await page.locator('[data-list-id=chat-messages]').evaluate(el=>el.insertAdjacentHTML('beforeend',`
+ <li><div role="article" data-list-item-id="chat-messages___chat-messages-456-1002"><div class="contents">
+ <img src="https://cdn.discordapp.com/avatars/11/self.webp?size=160"><h3><span id="message-username-1002"><span data-text="WebBrain">WebBrain</span></span><time id="message-timestamp-1002" datetime="2026-09-29T01:01:00.000Z"></time></h3>
+ <div id="message-content-1002">My answer</div></div></div></li>
+ <li><div role="article" data-list-item-id="chat-messages___chat-messages-999-1003"><div id="message-content-1003">Wrong channel</div></div></li>
+ <li><div role="article" class="isSystemMessage_fixture" data-list-item-id="chat-messages___chat-messages-456-1004"><div id="message-content-1004">A member joined</div></div></li>`));
+ const afterSend=await observe();
+ assert.deepEqual(afterSend.messages.map(item=>item.id),['discord:456:1001','discord:456:1002']);
+ assert.equal(afterSend.messages[1].direction,'outgoing');
+ advanced=advanceChatSession(advanced.session,afterSend);
+ assert.equal(advanced.newMessages[0].direction,'outgoing');
+ assert.equal(advanced.session.state,'we_responded');
+
+ await page.locator('[data-list-id=chat-messages]').evaluate(el=>el.insertAdjacentHTML('beforeend',`
+ <li><div role="article" data-list-item-id="chat-messages___chat-messages-456-1005">
+ <div id="message-reply-context-1005"><div id="message-content-1002">My answer</div></div>
+ <div class="contents"><img src="https://cdn.discordapp.com/avatars/22/other.webp?size=160"><h3><span id="message-username-1005"><span data-text="Ficsit">Ficsit</span></span><time id="message-timestamp-1005" datetime="2026-09-29T01:02:00.000Z"></time></h3>
+ <div id="message-content-1005">A new reply <span role="button"><img class="emoji" alt="🚀" src="/emoji.svg"></span></div></div><div role="group">Reply Forward Add Reaction</div></div></li>`));
+ const afterReply=await observe();
+ assert.equal(afterReply.messages[2].text,'A new reply 🚀');
+ assert.equal(afterReply.messages[2].direction,'incoming');
+ advanced=advanceChatSession(advanced.session,afterReply);
+ assert.deepEqual(advanced.newMessages.map(item=>item.id),['discord:456:1005']);
+ assert.equal(advanced.nextAction,'reply');
+ await page.locator('[data-list-id=chat-messages]').evaluate(el=>el.replaceChildren());
+ assert.equal((await observe()).success,true);
+ await page.locator('#channel').evaluate(el=>el.setAttribute('href','/channels/123/999'));
+ assert.equal((await observe()).success,false,'stale channel identity fails closed');
+ } finally {await page.close();}
+ });
+ } finally {await browser.close();}
+ });
+}