Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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: 2 additions & 5 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "name in manifest.json",
"description": "description in manifest.json",
"name": "InlineNote",
"description": "An inline AI note-taking extension. Hover, press, and get LLM-powered insights seamlessly embedded into your reading flow.",
"options_ui": {
"page": "src/pages/options/index.html",
"open_in_tab": true
Expand All @@ -12,9 +12,6 @@
"32": "icon-32.png"
}
},
"chrome_url_overrides": {
"newtab": "src/pages/newtab/index.html"
},
"icons": {
"128": "icon-128.png"
},
Expand Down
Binary file modified public/dev-icon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/dev-icon-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/icon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/icon-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 1 addition & 18 deletions src/pages/background/config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
// src/pages/background/config.ts

export interface APIConfig {
baseURL: string;
apiKey: string;
model: string;
maxTokens: number;
temperature: number;
systemPrompt: string;
debug?: boolean;
}

export const apiConfig: APIConfig = {
// ⚠️ TODO: T012 将迁移到 chrome.storage.local
baseURL: 'https://api.deepseek.com',
apiKey: 'YOUR_API_KEY_HERE', // 替换为你的 API Key
model: 'deepseek-chat',
maxTokens: 1000,
temperature: 0.7,
systemPrompt: '你是一个专业的助手,请简洁地解释用户提供的内容。',
export const apiConfig = {
debug: false,
};
1 change: 1 addition & 0 deletions src/pages/background/storage.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface StorageSchema {
selectedProvider: string;
selectedPromptId: string;
disabledSites: string[];
showThinkContent: boolean;
}

export const DEFAULT_PROMPTS: PromptTemplate[] = [
Expand Down
16 changes: 14 additions & 2 deletions src/pages/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { SelectedBlock } from './types/content.types';
import { KeyboardController } from './services/KeyboardController';
import { ExportPreviewModal } from './components/ExportPreviewModal';
import type { NoteData } from '../background/messageTypes';
import { apiConfig } from '@pages/background/config';

let noteCard: NoteCard | null = null;
let keyboardController: KeyboardController | null = null;
Expand Down Expand Up @@ -121,7 +122,12 @@ async function handleExportShortcut(): Promise<boolean> {
return true;
}

function createNoteCard(targetElement: HTMLElement, originalText: string, pageUrl: string) {
function createNoteCard(
targetElement: HTMLElement,
originalText: string,
pageUrl: string,
rect?: { width: number }
) {
// Prevent duplicate note cards for the same text being processed
if (processingNoteId) {
console.log('[ContentScript] Already processing a note, skipping duplicate creation');
Expand All @@ -136,6 +142,7 @@ function createNoteCard(targetElement: HTMLElement, originalText: string, pageUr
id: noteId,
originalText,
targetElement,
rect,
onClose: (closedNoteId: string) => {
// Remove note from currentPageNotes by id
currentPageNotes = currentPageNotes.filter((n) => n.id !== closedNoteId);
Expand Down Expand Up @@ -330,7 +337,8 @@ function SelectionDisplay() {
createNoteCard(
selectedBlockRef.current.element,
selectedBlockRef.current.text,
window.location.href
window.location.href,
{ width: selectedBlockRef.current.rect.width }
);
}
};
Expand All @@ -346,6 +354,10 @@ function SelectionDisplay() {
};
}, []);

if (!apiConfig.debug) {
return null;
}

if (!selectedBlock) {
return (
<div className="fixed bottom-4 left-4 bg-amber-400 text-black px-4 py-2 rounded-lg z-50">
Expand Down
2 changes: 1 addition & 1 deletion src/pages/content/services/HighlightController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class HighlightController {
this.highlightLayer.style.cssText = `
position: fixed;
pointer-events: none;
border: 2px dashed rgba(255, 200, 0, 0.6);
border: 2px dashed rgba(96, 165, 250, 0.6);
border-radius: 4px;
z-index: 2147483647;
transition: all 0.1s ease;
Expand Down
140 changes: 139 additions & 1 deletion src/pages/content/services/NoteCard.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { NoteCard } from './NoteCard';

// Mutable storage for test control
let mockShowThinkContent = false;
let chromeGetSpy: ReturnType<typeof vi.spyOn> | undefined;

// Mock onChanged for storage listener
const mockOnChanged = {
addListener: vi.fn(),
removeListener: vi.fn(),
};

// Type for chrome.storage
interface ChromeStorage {
local: {
get: ReturnType<typeof vi.fn>;
set?: ReturnType<typeof vi.fn>;
remove?: ReturnType<typeof vi.fn>;
};
onChanged?: typeof mockOnChanged;
}

// Helper to safely access chrome.storage
function getChromeStorage(): { chrome?: { storage?: ChromeStorage } } {
return globalThis as unknown as { chrome?: { storage?: ChromeStorage } };
}

describe('NoteCard', () => {
beforeEach(() => {
vi.useFakeTimers();
document.body.innerHTML = '<div id="test-target"></div>';

// Spy on the existing chrome.storage.local.get from setup.ts
const chromeStorageLocal = getChromeStorage().chrome?.storage?.local;
if (chromeStorageLocal?.get) {
chromeGetSpy = vi
.spyOn(chromeStorageLocal, 'get')
.mockImplementation(
(_keys: string[], callback: (result: { showThinkContent?: boolean }) => void) => {
callback({ showThinkContent: mockShowThinkContent });
}
);
}

// Mock chrome.storage.onChanged
const chrome = getChromeStorage().chrome;
if (chrome?.storage) {
chrome.storage.onChanged = mockOnChanged;
}
});

afterEach(() => {
vi.useRealTimers();
chromeGetSpy?.mockRestore();
chromeGetSpy = undefined;
mockShowThinkContent = false; // Reset for next test
mockOnChanged.addListener.mockClear();
mockOnChanged.removeListener.mockClear();
});

it('should create a NoteCard instance', () => {
Expand Down Expand Up @@ -293,4 +346,89 @@ describe('NoteCard', () => {
card.toggleCollapse();
expect(toggleBtn.textContent).toBe('▾');
});

describe('Filtering', () => {
beforeEach(() => {
mockShowThinkContent = false;
});

it('should show thinking process when showThinkContent is true', async () => {
mockShowThinkContent = true;
const target = document.querySelector('#test-target') as HTMLElement;
const card = new NoteCard({
id: 'test-note-1',
originalText: 'Test text',
targetElement: target,
});

card.mount();
card.appendAIResponse('Hello <think>reasoning</think> world');

vi.runAllTimers();
const aiResponse = card.getShadowRoot()?.querySelector('.ai-response');
card.finalizeAIResponse();

const text = aiResponse?.textContent?.replace(/\s+/g, ' ').trim();
expect(text).toContain('reasoning');
expect(text).toBe('Hello reasoning world');
});

it('should NOT leak partial <think> tags during streaming', async () => {
const target = document.querySelector('#test-target') as HTMLElement;
const card = new NoteCard({
id: 'test-note-1',
originalText: 'Test text',
targetElement: target,
});

card.mount();
card.appendAIResponse('He');
card.appendAIResponse('ll');
card.appendAIResponse('o <th');

vi.runAllTimers();
const aiResponse = card.getShadowRoot()?.querySelector('.ai-response');
card.finalizeAIResponse();

const text = aiResponse?.textContent?.trim();
expect(text).not.toContain('<th');
expect(text).toBe('Hello');
});

it('should correctly handle completed <think> tag during streaming', () => {
const target = document.querySelector('#test-target') as HTMLElement;
const card = new NoteCard({
id: 'test-note-1',
originalText: 'Test text',
targetElement: target,
});

card.mount();
card.appendAIResponse('Hello <think>thinking</think> world');
card.finalizeAIResponse();

const aiResponse = card.getShadowRoot()?.querySelector('.ai-response');
const text = aiResponse?.textContent?.replace(/\s+/g, ' ').trim();
expect(text).not.toContain('<think');
expect(text).toBe('Hello world');
});

it('should strip escaped &lt;think&gt; tags', () => {
const target = document.querySelector('#test-target') as HTMLElement;
const card = new NoteCard({
id: 'test-note-1',
originalText: 'Test text',
targetElement: target,
});

card.mount();
card.appendAIResponse('Hello <think>secret</think> world');
card.finalizeAIResponse();

const aiResponse = card.getShadowRoot()?.querySelector('.ai-response');
const html = aiResponse?.innerHTML || '';
expect(html).not.toContain('&lt;think&gt;');
expect(html).not.toContain('secret');
});
});
});
Loading
Loading