diff --git a/apps/desktop/extensions/ai-sidebar/manifest.json b/apps/desktop/extensions/ai-sidebar/manifest.json index b7836be..6925b1f 100644 --- a/apps/desktop/extensions/ai-sidebar/manifest.json +++ b/apps/desktop/extensions/ai-sidebar/manifest.json @@ -76,7 +76,8 @@ "http://127.0.0.1/*" ], "js": [ - "w3bs-links.js" + "w3bs-links.js", + "phone-links.js" ], "run_at": "document_start", "all_frames": true diff --git a/apps/desktop/extensions/ai-sidebar/phone-links.js b/apps/desktop/extensions/ai-sidebar/phone-links.js new file mode 100644 index 0000000..51c2f68 --- /dev/null +++ b/apps/desktop/extensions/ai-sidebar/phone-links.js @@ -0,0 +1,59 @@ +// tel: and sms: links. Chromium hands them to the OS, which on most desktops +// has no dialer, so a click does nothing or asks to open xdg-open. Send them to +// smshub.dev/handle instead: it texts from the user's smshub number, or offers +// the phone. Same click rules as w3bs-links.js (middle / modifier = new tab). +// +// Left alone: links marked data-tron-native (smshub's own "use this device" +// button, so the handler page can still give the link to the OS) and every link +// on smshub.dev itself. On smshub.dev the root gets data-tron-phone-links so the +// page hides its "make smshub your handler" button. +(() => { + const HANDLER = 'https://smshub.dev/handle?uri='; + const HANDLER_HOSTS = /(^|\.)smshub\.dev$/i; + const PHONE = /^\s*(tel|sms|smsto|callto):/i; + + /** The smshub handler URL for a link's href, or null to leave the click alone. */ + const phoneHandlerUrl = (href, host) => { + if (!href || !PHONE.test(href) || HANDLER_HOSTS.test(host || '')) return null; + return HANDLER + encodeURIComponent(href.trim()); + }; + globalThis.__tronPhoneHandlerUrl = phoneHandlerUrl; + + if (HANDLER_HOSTS.test(location.hostname)) { + document.documentElement.dataset.tronPhoneLinks = 'smshub'; + return; + } + + const phoneLink = (event) => { + const path = typeof event.composedPath === 'function' ? event.composedPath() : [event.target]; + for (const node of path) { + if (node instanceof HTMLAnchorElement || node instanceof SVGAElement) { + if (node.hasAttribute('data-tron-native')) return null; + const href = node instanceof SVGAElement ? node.href.baseVal : node.getAttribute('href'); + return phoneHandlerUrl(href, location.hostname); + } + } + return null; + }; + + const open = (event) => { + if (event.defaultPrevented || (event.type === 'auxclick' && event.button !== 1)) return; + const url = phoneLink(event); + if (!url) return; + event.preventDefault(); + event.stopImmediatePropagation(); + if (event.type === 'auxclick' || event.ctrlKey || event.metaKey || event.shiftKey) { + window.open(url, '_blank', 'noopener'); + } else { + // A frame cannot always navigate its top; open the handler where the user is looking. + try { + window.top.location.assign(url); + } catch { + window.open(url, '_blank', 'noopener'); + } + } + }; + + document.addEventListener('click', open, true); + document.addEventListener('auxclick', open, true); +})(); diff --git a/apps/desktop/extensions/ai-sidebar/phone-links.test.js b/apps/desktop/extensions/ai-sidebar/phone-links.test.js new file mode 100644 index 0000000..763bdbf --- /dev/null +++ b/apps/desktop/extensions/ai-sidebar/phone-links.test.js @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest'; +import { readFileSync } from 'node:fs'; +import vm from 'node:vm'; + +const SRC = readFileSync(new URL('./phone-links.js', import.meta.url), 'utf8'); + +// Runs the content script in a bare context: no DOM, just what it touches at load. +function load(hostname) { + const listeners = []; + const ctx = { + location: { hostname }, + document: { documentElement: { dataset: {} }, addEventListener: (type) => listeners.push(type) }, + }; + ctx.globalThis = ctx; + vm.runInNewContext(SRC, ctx); + return { fn: ctx.__tronPhoneHandlerUrl, dataset: ctx.document.documentElement.dataset, listeners }; +} + +describe('phone-links content script', () => { + const { fn, listeners } = load('example.com'); + + it('sends tel: and sms: to smshub.dev/handle', () => { + expect(fn('tel:+18885264640', 'example.com')).toBe('https://smshub.dev/handle?uri=tel%3A%2B18885264640'); + expect(fn(' sms:+19166599471?body=hi ', 'example.com')).toBe( + 'https://smshub.dev/handle?uri=sms%3A%2B19166599471%3Fbody%3Dhi', + ); + expect(fn('SMSTO:5551234567', 'example.com')).toMatch(/^https:\/\/smshub\.dev\/handle\?uri=/); + }); + + it('leaves other links alone', () => { + expect(fn('https://example.com', 'example.com')).toBeNull(); + expect(fn('mailto:a@b.test', 'example.com')).toBeNull(); + expect(fn('/tel:nope', 'example.com')).toBeNull(); + expect(fn(null, 'example.com')).toBeNull(); + }); + + it('listens for click and middle-click', () => { + expect(listeners).toEqual(['click', 'auxclick']); + }); + + it('stays out of smshub.dev and marks it', () => { + const smshub = load('smshub.dev'); + expect(smshub.listeners).toEqual([]); + expect(smshub.dataset.tronPhoneLinks).toBe('smshub'); + expect(fn('tel:+18885264640', 'www.smshub.dev')).toBeNull(); + }); +});