Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import { TtsEngine } from './js/tts-engine.js';
import { pickTts } from './js/tts-webspeech.js';
import { promptLogin } from './js/login-prompt.js';
import { injectIntoOpenTabs, readSelection, selectionErrorMessage } from './js/site-access.js';
import { summaryNotification } from './js/utils.js';

const apiClient = new ApiClient();

// Only a pointer, not the summary: macOS and others cut long notification text
// off, and the full summary is in the popup.
const SUMMARY_READY_MESSAGE = 'Your summary is ready. Open the extension to read it.';

const engine = new TtsEngine({
// chrome.tts in Chrome; Firefox has no chrome.tts, so the Web Speech API.
tts: pickTts({
Expand Down Expand Up @@ -348,7 +351,7 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
message: 'Text may be too short or incomplete for summarization.'
});
} else {
notify('Summary Ready!', summaryNotification(summary.summary));
notify('Summary Ready!', SUMMARY_READY_MESSAGE);
}
} catch (error) {
console.error('Failed to summarize:', error);
Expand Down
29 changes: 0 additions & 29 deletions extension/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,6 @@ export function truncate(text, max) {
return chars.length <= max ? text : chars.slice(0, max).join('') + '…';
}

/** Longest notification message we send; Firefox cuts longer ones off. */
export const NOTIFICATION_MAX = 200;

/**
* Text shortened for a notification: whitespace collapsed, at most `max`
* characters including the "…", cut at a word boundary when one is near.
* Counts graphemes, so emoji and accented letters are never split.
*/
export function truncateForNotification(text, max = NOTIFICATION_MAX) {
const clean = String(text ?? '').replace(/\s+/g, ' ').trim();
const graphemes = [...new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(clean)].map((s) => s.segment);
if (graphemes.length <= max) return clean;
const cut = graphemes.slice(0, max - 1).join('');
const space = cut.lastIndexOf(' ');
// Back up to the last word only if that keeps most of the text.
const head = space >= cut.length * 0.7 ? cut.slice(0, space) : cut;
return `${head.replace(/[\s.,;:!?-]+$/, '')}…`;
}

const FULL_SUMMARY_HINT = ' Open the extension for the full summary.';

/** A summary short enough for a notification (Firefox cuts long ones off). */
export function summaryNotification(summary) {
if (!summary) return 'Summary generated successfully.';
const short = truncateForNotification(summary);
if (short === summary.replace(/\s+/g, ' ').trim()) return short;
return truncateForNotification(summary, NOTIFICATION_MAX - FULL_SUMMARY_HINT.length) + FULL_SUMMARY_HINT;
}

export function relativeDate(iso, now = Date.now()) {
const date = new Date(iso);
const mins = Math.floor((now - date.getTime()) / 60000);
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "TTS Study Assistant",
"version": "0.3.4",
"version": "0.3.5",
"description": "Save and listen to text from any webpage",
"minimum_chrome_version": "121",
"permissions": [
Expand Down
2 changes: 1 addition & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tts-study-assistant",
"version": "0.3.4",
"version": "0.3.5",
"description": "Instant text-to-speech browser extension for Chrome and Firefox, with study features",
"scripts": {
"test": "node --test tests/*.test.mjs"
Expand Down
6 changes: 3 additions & 3 deletions extension/tests/background.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ for (const [browser, withTts] of [['chrome', true], ['firefox', false]]) {
}
});

test(`${browser}: a long summary from the context menu is shortened for the notification`, async () => {
test(`${browser}: a summary from the context menu is announced, not shown, in the notification`, async () => {
const { chrome, calls } = await loadBackground(`${browser}-long-summary`, { withTts });
chrome.storage.local.get = async () => ({ access_token: 'token' }); // logged in
const summary = 'This summary is far longer than a notification can show. '.repeat(20);
Expand All @@ -180,8 +180,8 @@ for (const [browser, withTts] of [['chrome', true], ['firefox', false]]) {
}
const last = calls.notifications.at(-1);
assert.equal(last.title, 'Summary Ready!');
assert.ok([...last.message].length <= 200, `${[...last.message].length} characters`);
assert.match(last.message, /Open the extension for the full summary\.$/);
assert.equal(last.message, 'Your summary is ready. Open the extension to read it.');
assert.ok(!last.message.includes('This summary'), 'the summary text is not in the notification');
});

test(`${browser}: no tab update listener and no openPopup message`, async () => {
Expand Down
38 changes: 1 addition & 37 deletions extension/tests/utils.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { formatTime, NOTIFICATION_MAX, RATES, siteDomainFor, snapRate, summaryNotification, truncate, truncateForNotification } from '../js/utils.js';
import { formatTime, RATES, siteDomainFor, snapRate, truncate } from '../js/utils.js';

test('snapRate maps any stored speed to one of the four steps', () => {
assert.deepEqual(RATES, [0.5, 1, 1.5, 2]);
Expand Down Expand Up @@ -40,39 +40,3 @@ test('truncate by code points', () => {
assert.equal(truncate('hello', 10), 'hello');
assert.equal(truncate('😀😀😀', 2), '😀😀…');
});

test('truncateForNotification leaves short text alone, collapsing whitespace', () => {
assert.equal(truncateForNotification('Short summary.'), 'Short summary.');
assert.equal(truncateForNotification(' two\n\nlines '), 'two lines');
assert.equal(truncateForNotification('x'.repeat(NOTIFICATION_MAX)), 'x'.repeat(NOTIFICATION_MAX));
assert.equal(truncateForNotification(undefined), '');
});

test('truncateForNotification cuts at a word boundary within the limit', () => {
const text = 'The quick brown fox jumps over the lazy dog. '.repeat(10);
const short = truncateForNotification(text, 50);
assert.ok([...short].length <= 50, short);
assert.ok(short.endsWith('…'));
assert.ok(text.startsWith(short.slice(0, -1)), 'a prefix of the text');
assert.match(short, /\w…$/, 'no trailing space or punctuation before the ellipsis');
assert.equal(short, 'The quick brown fox jumps over the lazy dog. The…');
});

test('truncateForNotification hard-cuts one long word', () => {
assert.equal(truncateForNotification('a'.repeat(30), 10), 'aaaaaaaaa…');
});

test('truncateForNotification never splits emoji or accented letters', () => {
const family = '👨‍👩‍👧'; // one grapheme, five code points
const short = truncateForNotification(family.repeat(20), 5);
assert.equal(short, family.repeat(4) + '…');
assert.equal(truncateForNotification('e\u0301'.repeat(20), 4), 'e\u0301'.repeat(3) + '…');
});

test('summaryNotification keeps short summaries and points long ones to the popup', () => {
assert.equal(summaryNotification('A short summary.'), 'A short summary.');
assert.equal(summaryNotification(''), 'Summary generated successfully.');
const long = summaryNotification('word '.repeat(200));
assert.ok([...long].length <= NOTIFICATION_MAX, `${[...long].length} characters`);
assert.match(long, /… Open the extension for the full summary\.$/);
});