Skip to content

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

Schedule form: keep balanced URL brackets, follow the prompt's URL as it is edited

Claude9baff1be2adb
diff --git a/src/chrome/src/ui/sidepanel.js b/src/chrome/src/ui/sidepanel.js
index f7be8e05a..67899b2f9 100644
--- a/src/chrome/src/ui/sidepanel.js
+++ b/src/chrome/src/ui/sidepanel.js
@@ -4373,12 +4373,28 @@ function isHttpScheduleUrl(value) {
}
}
+// Drops the sentence punctuation and unmatched closing brackets a URL picked
+// up from the prose around it, but keeps a bracket that closes one inside the
+// address (https://en.wikipedia.org/wiki/Function_(mathematics)).
+function trimScheduleUrlProse(raw) {
+ let url = raw;
+ for (;;) {
+ const last = url.slice(-1);
+ const opener = last === ')' ? '(' : last === ']' ? '[' : '';
+ if (/[.,;:!?]/.test(last) || (opener && url.split(last).length > url.split(opener).length)) {
+ url = url.slice(0, -1);
+ } else {
+ return url;
+ }
+ }
+}
+
// The one http(s) address a task's prompt names, so "check https://example.com/
// every day" targets that page rather than whatever tab is open.
function scheduleUrlFromPrompt(prompt) {
const urls = new Set();
for (const match of String(prompt || '').matchAll(/https?:\/\/[^\s<>"'`]+/gi)) {
- const url = match[0].replace(/[)\].,;:!?]+$/, '');
+ const url = trimScheduleUrlProse(match[0]);
if (isHttpScheduleUrl(url)) urls.add(url);
}
return urls.size === 1 ? [...urls][0] : '';
@@ -4654,11 +4670,25 @@ async function renderScheduleComposer(prefillPrompt = '', tabId = currentTabId)
urlInput.className = 'schedule-url';
urlInput.placeholder = 'https://example.com/';
addScheduleField(form, t('sp.schedule_form.target_url'), urlInput);
- const targetUrl = scheduleUrlFromPrompt(prefillPrompt) || initialScheduleUrl;
- if (isHttpScheduleUrl(targetUrl)) {
- urlInput.value = targetUrl;
- targetType.value = 'url';
- }
+ const applyScheduleTarget = (prompt) => {
+ const targetUrl = scheduleUrlFromPrompt(prompt) || initialScheduleUrl;
+ if (isHttpScheduleUrl(targetUrl)) {
+ urlInput.value = targetUrl;
+ targetType.value = 'url';
+ } else {
+ urlInput.value = '';
+ targetType.value = 'current_tab';
+ }
+ };
+ applyScheduleTarget(prefillPrompt);
+ // The target follows the URL the prompt names until the person picks one
+ // themselves, so a prompt typed or edited after /schedule opened is read too.
+ let scheduleTargetChosen = false;
+ urlInput.addEventListener('input', () => { scheduleTargetChosen = true; });
+ targetType.addEventListener('change', () => { scheduleTargetChosen = true; });
+ promptInput.addEventListener('input', () => {
+ if (!scheduleTargetChosen) applyScheduleTarget(promptInput.value);
+ });
const modeInput = document.createElement('select');
modeInput.className = 'schedule-mode';
diff --git a/src/firefox/src/ui/sidepanel.js b/src/firefox/src/ui/sidepanel.js
index c73c6ec28..07a4d0f81 100644
--- a/src/firefox/src/ui/sidepanel.js
+++ b/src/firefox/src/ui/sidepanel.js
@@ -4237,12 +4237,28 @@ function isHttpScheduleUrl(value) {
}
}
+// Drops the sentence punctuation and unmatched closing brackets a URL picked
+// up from the prose around it, but keeps a bracket that closes one inside the
+// address (https://en.wikipedia.org/wiki/Function_(mathematics)).
+function trimScheduleUrlProse(raw) {
+ let url = raw;
+ for (;;) {
+ const last = url.slice(-1);
+ const opener = last === ')' ? '(' : last === ']' ? '[' : '';
+ if (/[.,;:!?]/.test(last) || (opener && url.split(last).length > url.split(opener).length)) {
+ url = url.slice(0, -1);
+ } else {
+ return url;
+ }
+ }
+}
+
// The one http(s) address a task's prompt names, so "check https://example.com/
// every day" targets that page rather than whatever tab is open.
function scheduleUrlFromPrompt(prompt) {
const urls = new Set();
for (const match of String(prompt || '').matchAll(/https?:\/\/[^\s<>"'`]+/gi)) {
- const url = match[0].replace(/[)\].,;:!?]+$/, '');
+ const url = trimScheduleUrlProse(match[0]);
if (isHttpScheduleUrl(url)) urls.add(url);
}
return urls.size === 1 ? [...urls][0] : '';
@@ -4518,11 +4534,25 @@ async function renderScheduleComposer(prefillPrompt = '', tabId = currentTabId)
urlInput.className = 'schedule-url';
urlInput.placeholder = 'https://example.com/';
addScheduleField(form, t('sp.schedule_form.target_url'), urlInput);
- const targetUrl = scheduleUrlFromPrompt(prefillPrompt) || initialScheduleUrl;
- if (isHttpScheduleUrl(targetUrl)) {
- urlInput.value = targetUrl;
- targetType.value = 'url';
- }
+ const applyScheduleTarget = (prompt) => {
+ const targetUrl = scheduleUrlFromPrompt(prompt) || initialScheduleUrl;
+ if (isHttpScheduleUrl(targetUrl)) {
+ urlInput.value = targetUrl;
+ targetType.value = 'url';
+ } else {
+ urlInput.value = '';
+ targetType.value = 'current_tab';
+ }
+ };
+ applyScheduleTarget(prefillPrompt);
+ // The target follows the URL the prompt names until the person picks one
+ // themselves, so a prompt typed or edited after /schedule opened is read too.
+ let scheduleTargetChosen = false;
+ urlInput.addEventListener('input', () => { scheduleTargetChosen = true; });
+ targetType.addEventListener('change', () => { scheduleTargetChosen = true; });
+ promptInput.addEventListener('input', () => {
+ if (!scheduleTargetChosen) applyScheduleTarget(promptInput.value);
+ });
const modeInput = document.createElement('select');
modeInput.className = 'schedule-mode';
diff --git a/test/run.js b/test/run.js
index 3c903ab35..8d2cba09d 100644
--- a/test/run.js
+++ b/test/run.js
@@ -42624,10 +42624,12 @@ test('sidepanel exposes schedule slash commands in both builds', () => {
assert.match(panel, /if \(currentTabId !== tabId\) \{[\s\S]*?replaceCachedScheduleComposer\(tabId, form\.dataset\.composerId, \{[\s\S]*?id: res\.jobId,[\s\S]*?scheduledAt: res\.scheduledAt,[\s\S]*?\}\);[\s\S]*?return;[\s\S]*?\}[\s\S]*?form\.remove\(\);[\s\S]*?renderScheduledJobCreatedMessage/, `${label}: schedule form should reconcile hidden and visible composer confirmations by job id`);
assert.match(panel, /catch \(err\) \{[\s\S]*?if \(currentTabId !== tabId\) \{[\s\S]*?updateCachedScheduleComposerError\(tabId, form\.dataset\.composerId, err\.message\);[\s\S]*?return;[\s\S]*?\}[\s\S]*?submit\.disabled = false;[\s\S]*?errorEl\.textContent = err\.message;/, `${label}: schedule form failures should update hidden cached composers instead of leaving disabled forms`);
assert.match(panel, /renderScheduleComposer\(payload, tabId\)/, `${label}: /schedule should pass the initiating tab into the async composer`);
- assert.match(panel, /const targetUrl = scheduleUrlFromPrompt\(prefillPrompt\) \|\| initialScheduleUrl;\s*if \(isHttpScheduleUrl\(targetUrl\)\) \{\s*urlInput\.value = targetUrl;/, `${label}: schedule form should prefill URL targets from the prompt, else the active tab`);
+ assert.match(panel, /const targetUrl = scheduleUrlFromPrompt\(prompt\) \|\| initialScheduleUrl;\s*if \(isHttpScheduleUrl\(targetUrl\)\) \{\s*urlInput\.value = targetUrl;[\s\S]*?applyScheduleTarget\(prefillPrompt\);/, `${label}: schedule form should prefill URL targets from the prompt, else the active tab`);
+ assert.match(panel, /promptInput\.addEventListener\('input', \(\) => \{\s*if \(!scheduleTargetChosen\) applyScheduleTarget\(promptInput\.value\);/, `${label}: an unchosen target follows the prompt as it is typed or edited`);
+ assert.match(panel, /urlInput\.addEventListener\('input', \(\) => \{ scheduleTargetChosen = true; \}\);[\s\S]*?targetType\.addEventListener\('change', \(\) => \{ scheduleTargetChosen = true; \}\);/, `${label}: a target the person picked is not overwritten by the prompt`);
{
const helpers = vm.createContext({ URL });
- vm.runInContext(['isHttpScheduleUrl', 'scheduleUrlFromPrompt'].map(name => {
+ vm.runInContext(['isHttpScheduleUrl', 'trimScheduleUrlProse', 'scheduleUrlFromPrompt'].map(name => {
const start = panel.indexOf(`function ${name}(`);
return panel.slice(start, panel.indexOf('\n}\n', start) + 2);
}).join('\n'), helpers);
@@ -42638,6 +42640,9 @@ test('sidepanel exposes schedule slash commands in both builds', () => {
assert.equal(pick('Check https://example.com/ twice: https://example.com/'), 'https://example.com/');
assert.equal(pick('Check the open page.'), '');
assert.equal(pick('Run javascript:alert(1) on ftp://example.com'), '');
+ assert.equal(pick('See https://en.wikipedia.org/wiki/Function_(mathematics) daily.'), 'https://en.wikipedia.org/wiki/Function_(mathematics)', `${label}: a bracket that closes one inside the address stays`);
+ assert.equal(pick('(see https://en.wikipedia.org/wiki/Function_(mathematics)).'), 'https://en.wikipedia.org/wiki/Function_(mathematics)', `${label}: prose punctuation after a balanced address is dropped`);
+ assert.equal(pick('Check [https://example.com/a].'), 'https://example.com/a');
}
assert.match(panel, /targetType\.value = 'url'/, `${label}: schedule form should default http(s) pages to URL targets`);
assert.match(panel, /content\.appendChild\(form\)/, `${label}: schedule form should append after initial target defaults are applied`);