diff --git a/README.md b/README.md index 2a18d90..5cfb06c 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ Server will start on port 3000 (or use PORT environment variable to customize) - Events: `page_view`, `search` (`search_scope`: `single`/`group`/`all`, `library_count`), `search_complete` (`library_count`, `failed_count`, `result_count`, `available_count`, `duration_ms`), `search_cancel` (`library_count`, `completed_count`, `duration_ms`), `my_library_select` - Entry source: open the app with `?ref=` (e.g. `?ref=ext` for the Chrome extension, `?ref=bm` for the bookmarklet; lowercase letters, digits, `-`, `_`, up to 20 chars). The first `page_view` gets `utm_source=` in `page_location`, so GA4 shows it as the session source. `page_view` and `search` also carry `entry_ref`; register it as an event-scoped custom dimension in GA4 to break down searches by source. - Bookmarklet: the bottom of the page shows a "πŸ“š 동넀 λ„μ„œκ΄€" button to drag to the bookmarks bar (the link text becomes the bookmark name; bookmarklets have no favicon, so the emoji stands in for one). On any site, select a book title (or type one when nothing is selected) and click it to open `/?title=&ref=bm`. When the app is opened with `ref` but no `library`, it searches the user's "λ‚΄ λ„μ„œκ΄€" (up to 5 recently searched libraries, stored in this site's localStorage) instead of every library; with no saved libraries it only fills in the title. The search wizard shows that scope as "λ‚΄ λ„μ„œκ΄€ (…)" and the search button keeps searching those libraries until the library, region, module or filter selection changes. Older bookmarklets that include `library` keep searching that library. +- Chrome extension: [`extension/`](extension/README.md) does the same from the right-click menu or the toolbar icon and opens `/?title=<title>&ref=ext`. Load it with "Load unpacked" in `chrome://extensions`. ## Library Regions diff --git a/extension/README.md b/extension/README.md new file mode 100644 index 0000000..ed53fee --- /dev/null +++ b/extension/README.md @@ -0,0 +1,40 @@ +# 동넀 λ„μ„œκ΄€μ—μ„œ μ°ΎκΈ° (크둬 ν™•μž₯) + +μ›Ήμ—μ„œ λ³Έ μ±… 제λͺ©μ„ λ“œλž˜κ·Έν•΄ [동넀 λ„μ„œκ΄€](https://dongne.onrender.com/)μ—μ„œ λ°”λ‘œ μ°ΎλŠ” 크둬 ν™•μž₯μž…λ‹ˆλ‹€. +같은 일을 ν•˜λŠ” λΆλ§ˆν΄λ¦Ώμ€ μ‚¬μ΄νŠΈ 맨 μ•„λž˜ "πŸ“š 동넀 λ„μ„œκ΄€" λ²„νŠΌμž…λ‹ˆλ‹€. + +## μ“°λŠ” 법 + +- 제λͺ©μ„ λ“œλž˜κ·Έν•˜κ³  우클릭 β†’ **πŸ“š 동넀 λ„μ„œκ΄€μ—μ„œ "…" μ°ΎκΈ°** +- λ˜λŠ” 제λͺ©μ„ λ“œλž˜κ·Έν•˜κ³  νˆ΄λ°”μ˜ ν™•μž₯ μ•„μ΄μ½˜μ„ λˆ„λ¦…λ‹ˆλ‹€. 선택이 μ—†μœΌλ©΄ μ‚¬μ΄νŠΈλ§Œ μ—½λ‹ˆλ‹€. + +μƒˆ νƒ­μ—μ„œ `https://dongne.onrender.com/?title=<제λͺ©>&ref=ext` κ°€ μ—΄λ¦½λ‹ˆλ‹€. λ„μ„œκ΄€μ€ λ„£μ§€ μ•ŠμœΌλ―€λ‘œ μ‚¬μ΄νŠΈκ°€ +λ‚΄ λ„μ„œκ΄€(κ·Έ μ‚¬μ΄νŠΈμ—μ„œ μ΅œκ·Όμ— κ²€μƒ‰ν•œ λ„μ„œκ΄€ μ΅œλŒ€ 5κ³³)μ—μ„œ μ°Ύκ³ , λ‚΄ λ„μ„œκ΄€μ΄ μ—†μœΌλ©΄ 제λͺ©λ§Œ μ±„μ›Œ λ‘‘λ‹ˆλ‹€. +GA μ—μ„œλŠ” `entry_ref=ext` 둜 κ΅¬λΆ„λ©λ‹ˆλ‹€. + +## μ„€μΉ˜ (μ••μΆ•ν•΄μ œλœ ν™•μž₯ ν”„λ‘œκ·Έλž¨) + +1. `chrome://extensions` λ₯Ό μ—½λ‹ˆλ‹€. +2. 였λ₯Έμͺ½ μœ„ **개발자 λͺ¨λ“œ** λ₯Ό μΌ­λ‹ˆλ‹€. +3. **μ••μΆ•ν•΄μ œλœ ν™•μž₯ ν”„λ‘œκ·Έλž¨μ„ λ‘œλ“œν•©λ‹ˆλ‹€** β†’ 이 `extension/` 폴더λ₯Ό κ³ λ¦…λ‹ˆλ‹€. +4. νˆ΄λ°”μ˜ 퍼즐 μ•„μ΄μ½˜μ—μ„œ "동넀 λ„μ„œκ΄€μ—μ„œ μ°ΎκΈ°" λ₯Ό κ³ μ •ν•˜λ©΄ μ•„μ΄μ½˜μ΄ λ³΄μž…λ‹ˆλ‹€. + +μ½”λ“œλ₯Ό 고친 λ’€μ—λŠ” `chrome://extensions` μ—μ„œ μƒˆλ‘œκ³ μΉ¨(↻)을 λˆ„λ¦…λ‹ˆλ‹€. + +## κΆŒν•œ + +| κΆŒν•œ | μ“°λŠ” κ³³ | +|---|---| +| `contextMenus` | 우클릭 메뉴 | +| `activeTab`, `scripting` | νˆ΄λ°” μ•„μ΄μ½˜μ„ λˆŒλ €μ„ λ•Œλ§Œ κ·Έ νƒ­μ˜ μ„ νƒν•œ ν…μŠ€νŠΈλ₯Ό μ½μŠ΅λ‹ˆλ‹€ | + +νŽ˜μ΄μ§€ λ‚΄μš©μ„ 미리 μ½κ±°λ‚˜ λ‹€λ₯Έ 곳으둜 보내지 μ•ŠμŠ΅λ‹ˆλ‹€. λΉŒλ“œ 과정은 μ—†μŠ΅λ‹ˆλ‹€ (순수 JS, Manifest V3). + +## ꡬ성 + +| 파일 | λ‚΄μš© | +|---|---| +| `manifest.json` | ν™•μž₯ μ„€μ • | +| `background.js` | 우클릭 λ©”λ‰΄Β·νˆ΄λ°” μ•„μ΄μ½˜ 처리 | +| `searchUrl.js` | 검색 μ£Όμ†Œ λ§Œλ“€κΈ° (ν…ŒμŠ€νŠΈ: `searchUrl.test.js`, `npm run test:server` 에 포함) | +| `icons/` | μ‚¬μ΄νŠΈ 둜고(`public/logo.jpg`)의 μ±… 더미λ₯Ό 잘라 λ§Œλ“  16Β·32Β·48Β·128px μ•„μ΄μ½˜ | diff --git a/extension/background.js b/extension/background.js new file mode 100644 index 0000000..1f9c905 --- /dev/null +++ b/extension/background.js @@ -0,0 +1,33 @@ +import { buildSearchUrl } from "./searchUrl.js"; + +const MENU_ID = "dongne-search"; + +chrome.runtime.onInstalled.addListener(() => { + chrome.contextMenus.create({ + id: MENU_ID, + title: "πŸ“š 동넀 λ„μ„œκ΄€μ—μ„œ \"%s\" μ°ΎκΈ°", + contexts: ["selection"], + }); +}); + +const openSearch = (text, tab) => + chrome.tabs.create({ url: buildSearchUrl(text), index: tab ? tab.index + 1 : undefined }); + +chrome.contextMenus.onClicked.addListener((info, tab) => { + if (info.menuItemId === MENU_ID) openSearch(info.selectionText, tab); +}); + +// νˆ΄λ°” μ•„μ΄μ½˜: μ„ νƒν•œ ν…μŠ€νŠΈλ‘œ μ°Ύκ³ , 선택이 μ—†κ±°λ‚˜ 읽을 수 μ—†λŠ” νŽ˜μ΄μ§€(chrome:// λ“±)λ©΄ μ‚¬μ΄νŠΈλ§Œ μ—°λ‹€ +chrome.action.onClicked.addListener(async (tab) => { + let text = ""; + try { + const [result] = await chrome.scripting.executeScript({ + target: { tabId: tab.id }, + func: () => String(getSelection()), + }); + text = result?.result ?? ""; + } catch { + // 슀크립트λ₯Ό 넣을 수 μ—†λŠ” νŽ˜μ΄μ§€ + } + openSearch(text, tab); +}); diff --git a/extension/icons/icon-128.png b/extension/icons/icon-128.png new file mode 100644 index 0000000..34ae327 Binary files /dev/null and b/extension/icons/icon-128.png differ diff --git a/extension/icons/icon-16.png b/extension/icons/icon-16.png new file mode 100644 index 0000000..b647133 Binary files /dev/null and b/extension/icons/icon-16.png differ diff --git a/extension/icons/icon-32.png b/extension/icons/icon-32.png new file mode 100644 index 0000000..f27c547 Binary files /dev/null and b/extension/icons/icon-32.png differ diff --git a/extension/icons/icon-48.png b/extension/icons/icon-48.png new file mode 100644 index 0000000..48899a3 Binary files /dev/null and b/extension/icons/icon-48.png differ diff --git a/extension/manifest.json b/extension/manifest.json new file mode 100644 index 0000000..716cc27 --- /dev/null +++ b/extension/manifest.json @@ -0,0 +1,28 @@ +{ + "manifest_version": 3, + "name": "동넀 λ„μ„œκ΄€μ—μ„œ μ°ΎκΈ°", + "short_name": "동넀 λ„μ„œκ΄€", + "version": "0.1.0", + "description": "μ›Ήμ—μ„œ λ³Έ μ±… 제λͺ©μ„ λ“œλž˜κ·Έν•΄ λ‚΄ 동넀 λ„μ„œκ΄€μ—μ„œ 빌릴 수 μžˆλŠ”μ§€ λ°”λ‘œ μ°ΎμŠ΅λ‹ˆλ‹€.", + "homepage_url": "https://dongne.onrender.com/", + "permissions": ["contextMenus", "activeTab", "scripting"], + "background": { + "service_worker": "background.js", + "type": "module" + }, + "action": { + "default_title": "μ„ νƒν•œ 제λͺ©μ„ 동넀 λ„μ„œκ΄€μ—μ„œ μ°ΎκΈ°", + "default_icon": { + "16": "icons/icon-16.png", + "32": "icons/icon-32.png", + "48": "icons/icon-48.png", + "128": "icons/icon-128.png" + } + }, + "icons": { + "16": "icons/icon-16.png", + "32": "icons/icon-32.png", + "48": "icons/icon-48.png", + "128": "icons/icon-128.png" + } +} diff --git a/extension/searchUrl.js b/extension/searchUrl.js new file mode 100644 index 0000000..fdb3a95 --- /dev/null +++ b/extension/searchUrl.js @@ -0,0 +1,22 @@ +// ν™•μž₯이 μ—¬λŠ” 검색 μ£Όμ†Œ. 뢁마클릿(src/utils/bookmarklet.ts)κ³Ό 같은 κ·œμΉ™μ΄λ‹€. +export const SITE_ORIGIN = "https://dongne.onrender.com"; + +/** μ„ νƒν•œ ν…μŠ€νŠΈλ₯Ό 제λͺ©μœΌλ‘œ λ‹€λ“¬λŠ”λ‹€. μ€„λ°”κΏˆΒ·μ—°μ† 곡백은 ν•œ μΉΈ, μ΅œλŒ€ 100자 */ +export const normalizeTitle = (text) => + String(text ?? "") + .replace(/\s+/g, " ") + .trim() + .slice(0, 100) + .trim(); + +/** + * 제λͺ©μ΄ 있으면 `/?title=…&ref=ext`, μ—†μœΌλ©΄ `/?ref=ext` λ₯Ό λŒλ €μ€€λ‹€. + * λ„μ„œκ΄€μ€ λ„£μ§€ μ•ŠλŠ”λ‹€. μ‚¬μ΄νŠΈκ°€ ref λ₯Ό 보고 λ‚΄ λ„μ„œκ΄€μ—μ„œ μ°ΎλŠ”λ‹€. + */ +export const buildSearchUrl = (text, origin = SITE_ORIGIN) => { + const url = new URL("/", origin); + const title = normalizeTitle(text); + if (title) url.searchParams.set("title", title); + url.searchParams.set("ref", "ext"); + return url.href; +}; diff --git a/extension/searchUrl.test.js b/extension/searchUrl.test.js new file mode 100644 index 0000000..baa706a --- /dev/null +++ b/extension/searchUrl.test.js @@ -0,0 +1,32 @@ +import { describe, it, expect } from "vitest"; +import { buildSearchUrl, normalizeTitle } from "./searchUrl.js"; + +describe("ν™•μž₯ 검색 μ£Όμ†Œ", () => { + it("μ„ νƒν•œ 제λͺ©κ³Ό ref=ext 둜 μ‚¬μ΄νŠΈλ₯Ό μ—°λ‹€", () => { + const url = new URL(buildSearchUrl("μ±„μ‹μ£Όμ˜μž")); + + expect(url.origin).toBe("https://dongne.onrender.com"); + expect(url.searchParams.get("title")).toBe("μ±„μ‹μ£Όμ˜μž"); + expect(url.searchParams.get("ref")).toBe("ext"); + expect(url.searchParams.has("library")).toBe(false); + }); + + it("μ€„λ°”κΏˆΒ·μ—°μ† 곡백을 ν•œ 칸으둜 쀄이고 100자둜 자λ₯Έλ‹€", () => { + expect(normalizeTitle(" λΉ„μ „κ³΅μžλ„\n이해할 수 μžˆλŠ”\tLLM ")).toBe("λΉ„μ „κ³΅μžλ„ 이해할 수 μžˆλŠ” LLM"); + expect(normalizeTitle("κ°€".repeat(150))).toHaveLength(100); + }); + + it("선택이 μ—†μœΌλ©΄ 제λͺ© 없이 ref=ext 만 뢙인닀", () => { + const url = new URL(buildSearchUrl(" ")); + + expect(url.searchParams.has("title")).toBe(false); + expect(url.searchParams.get("ref")).toBe("ext"); + expect(new URL(buildSearchUrl(undefined)).searchParams.has("title")).toBe(false); + }); + + it("& Β· # 같은 λ¬Έμžλ„ 제λͺ©μ— κ·ΈλŒ€λ‘œ λ‹΄λŠ”λ‹€", () => { + const url = new URL(buildSearchUrl("R&D #1 κ°€μ΄λ“œ")); + + expect(url.searchParams.get("title")).toBe("R&D #1 κ°€μ΄λ“œ"); + }); +}); diff --git a/vitest.config.node.ts b/vitest.config.node.ts index e23ad91..c940180 100644 --- a/vitest.config.node.ts +++ b/vitest.config.node.ts @@ -4,7 +4,7 @@ export default defineConfig({ test: { globals: true, environment: "node", - include: ["server.test.ts", "mcpAnalytics.test.ts", "librarySearch.test.ts"], + include: ["server.test.ts", "mcpAnalytics.test.ts", "librarySearch.test.ts", "extension/**/*.test.js"], exclude: ["src/**", "node_modules/**", "package.test.ts"], }, });