Open links and images in new browser tabs

- A middle-click on a link opens the link in a new tab. Scrolling
  starts at once, as before. The new engine command linkAt reads the
  link under the pointer in an isolated world. When it finds a web
  link, the tab stops the scroll and opens the link.
- The image menu of a page starts with Open image in new tab.

A new tab opens in front, like Open link in new tab. The engine smoke
test checks linkAt, and the interaction test checks both actions with
the real engine. Cards 333909d1 and f7ee842d.
This commit is contained in:
Knotty Wyvern
2026-10-31 19:41:27 -07:00
parent 68c302a271
commit 2ca7752c9d
7 changed files with 65 additions and 5 deletions
+8
View File
@@ -292,6 +292,14 @@ export async function createEngine({ directory, emit = () => {}, canEmitFrame =
}
return null;
}
case 'linkAt': {
// A middle-click on a link opens it in a new tab. The isolated world keeps page scripts from changing the answer.
if (![args.x, args.y].every(value => Number.isInteger(value) && value >= 0 && value <= 5120)) throw new Error('Invalid pointer position.');
const href = await tab(args.tabId).view.webContents.executeJavaScriptInIsolatedWorld(902, [{
code: `document.elementFromPoint(${args.x}, ${args.y})?.closest('a[href], area[href]')?.href ?? ''`,
}]).catch(() => '');
try { return { url: href ? webUrl(href, { blank: false }) : null }; } catch { return { url: null }; }
}
case 'copiedImage': {
// A part stays below the line limit of the host connection.
if (!copied || copied.token !== args.token || copied.tabId !== args.tabId || !Number.isSafeInteger(args.offset) || args.offset < 0 || args.offset >= copied.data.length) {
+8
View File
@@ -161,6 +161,13 @@ app.whenReady().then(async () => {
assert.equal(whole.subarray(0, 6).toString(), 'GIF89a');
assert.equal(whole.at(-1), 0x3b);
await wc.executeJavaScript('document.querySelector("#copy-image").remove();true;');
// A middle-click asks for the link under the pointer. Only web links count.
await wc.executeJavaScript(`document.body.insertAdjacentHTML('beforeend', '<a id="middle-link" href="${process.env.RS_TEST_URL}linked" style="position:absolute;left:600px;top:40px;width:120px;height:30px;display:block">Linked</a><a id="script-link" href="javascript:void(0)" style="position:absolute;left:600px;top:100px;width:120px;height:30px;display:block">Script</a>');true;`);
assert.deepEqual(await engine.command('linkAt', { tabId: first, x: 650, y: 50 }), { url: `${process.env.RS_TEST_URL}linked` });
assert.deepEqual(await engine.command('linkAt', { tabId: first, x: 650, y: 110 }), { url: null });
assert.deepEqual(await engine.command('linkAt', { tabId: first, x: 900, y: 600 }), { url: null });
await assert.rejects(engine.command('linkAt', { tabId: first, x: -1, y: 50 }), /Invalid pointer position/);
await wc.executeJavaScript('document.querySelector("#middle-link").remove(); document.querySelector("#script-link").remove();true;');
// An image that opens as the page itself is the main resource of the tab.
const direct = (await engine.command('newTab', { url: `${process.env.RS_TEST_URL}direct.gif` })).tabId;
const directContents = engine.tabs.get(direct).view.webContents;
@@ -174,6 +181,7 @@ app.whenReady().then(async () => {
assert.deepEqual([directCopy.mime, directCopy.size], ['image/gif', large.size]);
await engine.command('closeTab', { tabId: direct });
passed.push('Copy image keeps the loaded GIF bytes without a second request, also for an image that opens as the page');
passed.push('A middle-click finds the web link under the pointer');
await until(async () => await wc.executeJavaScript("getComputedStyle(document.querySelector('.advertisement')).display") === 'none');
assert.equal(await wc.executeJavaScript('typeof window.adExecuted'), 'undefined');
passed.push('network and cosmetic ad blocking remain active');