From 8eab672e5fb55462f7a4d1e67f42fc452cfd9b02 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:22:03 +0900 Subject: [PATCH 01/13] =?UTF-8?q?feat:=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/techpick-extension/src/pages/BookmarkPage.tsx | 10 ++++++++++ frontend/techpick-extension/src/pages/index.ts | 1 + frontend/techpick-extension/src/routers/index.tsx | 9 ++------- frontend/techpick-extension/tsconfig.app.tsbuildinfo | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 frontend/techpick-extension/src/pages/BookmarkPage.tsx diff --git a/frontend/techpick-extension/src/pages/BookmarkPage.tsx b/frontend/techpick-extension/src/pages/BookmarkPage.tsx new file mode 100644 index 000000000..19d649b1f --- /dev/null +++ b/frontend/techpick-extension/src/pages/BookmarkPage.tsx @@ -0,0 +1,10 @@ +import { CurrentTabInfo } from '@/'; + +export function BookmarkPage() { + return ( + <> +

Bookmark page

+ + + ); +} diff --git a/frontend/techpick-extension/src/pages/index.ts b/frontend/techpick-extension/src/pages/index.ts index e69de29bb..05f07a39b 100644 --- a/frontend/techpick-extension/src/pages/index.ts +++ b/frontend/techpick-extension/src/pages/index.ts @@ -0,0 +1 @@ +export { BookmarkPage } from './BookmarkPage'; diff --git a/frontend/techpick-extension/src/routers/index.tsx b/frontend/techpick-extension/src/routers/index.tsx index 05488b5af..a5632a365 100644 --- a/frontend/techpick-extension/src/routers/index.tsx +++ b/frontend/techpick-extension/src/routers/index.tsx @@ -1,15 +1,10 @@ import { createMemoryRouter } from 'react-router-dom'; -import { CurrentTabInfo } from '../components'; +import { BookmarkPage } from '../pages'; export const router = createMemoryRouter([ { path: '/', - element: ( - <> -

Main Page

- - - ), + element: , }, { path: '/login', diff --git a/frontend/techpick-extension/tsconfig.app.tsbuildinfo b/frontend/techpick-extension/tsconfig.app.tsbuildinfo index 3d8a09577..241e35bb4 100644 --- a/frontend/techpick-extension/tsconfig.app.tsbuildinfo +++ b/frontend/techpick-extension/tsconfig.app.tsbuildinfo @@ -1 +1 @@ -{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/chorme-extension/background.ts","./src/components/CurrentTabInfo.css.ts","./src/components/CurrentTabInfo.tsx","./src/components/index.ts","./src/features/index.ts","./src/hooks/index.ts","./src/lib/index.ts","./src/pages/index.ts","./src/routers/index.tsx","./src/widgets/index.ts"],"version":"5.6.2"} \ No newline at end of file +{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/chorme-extension/background.ts","./src/components/CurrentTabInfo.css.ts","./src/components/CurrentTabInfo.tsx","./src/components/index.ts","./src/features/index.ts","./src/hooks/index.ts","./src/lib/index.ts","./src/pages/BookmarkPage.tsx","./src/pages/index.ts","./src/routers/index.tsx","./src/widgets/index.ts"],"version":"5.6.2"} \ No newline at end of file From 9812a46b810d5e4b66b401c67be57eb8074f39e0 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:49:50 +0900 Subject: [PATCH 02/13] =?UTF-8?q?chore:=20=EC=A0=88=EB=8C=80=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/.pnp.cjs | 57 +++++++++++++++++++ frontend/techpick-extension/package.json | 3 +- .../src/pages/BookmarkPage.tsx | 2 +- frontend/techpick-extension/tsconfig.app.json | 4 ++ .../techpick-extension/tsconfig.node.json | 4 ++ frontend/techpick-extension/vite.config.ts | 2 + frontend/yarn.lock | 38 +++++++++++++ 7 files changed, 108 insertions(+), 2 deletions(-) diff --git a/frontend/.pnp.cjs b/frontend/.pnp.cjs index 0f2978e96..c0c83f3be 100644 --- a/frontend/.pnp.cjs +++ b/frontend/.pnp.cjs @@ -11772,6 +11772,15 @@ const RAW_RUNTIME_STATE = "linkType": "HARD"\ }]\ ]],\ + ["globrex", [\ + ["npm:0.1.2", {\ + "packageLocation": "../../../../.yarn/berry/cache/globrex-npm-0.1.2-ddda94f2d0-10c0.zip/node_modules/globrex/",\ + "packageDependencies": [\ + ["globrex", "npm:0.1.2"]\ + ],\ + "linkType": "HARD"\ + }]\ + ]],\ ["goober", [\ ["npm:2.1.14", {\ "packageLocation": "../../../../.yarn/berry/cache/goober-npm-2.1.14-d5c148d4a9-10c0.zip/node_modules/goober/",\ @@ -17391,6 +17400,7 @@ const RAW_RUNTIME_STATE = ["typescript-eslint", "virtual:d9650954c7f1725ea712d2e40695de9ea5d1c0fa9b89379134c62909d440d91b03d6c37e2823804a3404472b90a5bec73c82193190dd3c4db4c9e9edd75db91e#npm:8.6.0"],\ ["vite", "virtual:2c30557d8ca5e7c67e7558d45bc44bef6c7a622b34a97fa8102f3235f92769c87777d34ed37059c12d8f3a21841ea06cf4badc5dc796697b0f765c17db6e24e5#npm:5.4.6"],\ ["vite-plugin-static-copy", "virtual:b4c1c1be62fb935fd619e316b1ceced91b915e641eccd2188c44772d8f7ececf8da6978d623a01aeb3db3c58211d21e4eb7a46f09a4d6380d4cf0903f1a77fea#npm:1.0.6"],\ + ["vite-tsconfig-paths", "virtual:b4c1c1be62fb935fd619e316b1ceced91b915e641eccd2188c44772d8f7ececf8da6978d623a01aeb3db3c58211d21e4eb7a46f09a4d6380d4cf0903f1a77fea#npm:5.0.1"],\ ["zustand", "virtual:030fba93bd384e47e333ffeafe50202dd67d42973f7b6557f722bba726bfa5cb5e6c413f0acf63548e008bdfeb220e614152ce82b9eafc5b7d9d24ae29f598ed#npm:4.5.5"]\ ],\ "linkType": "SOFT"\ @@ -17717,6 +17727,28 @@ const RAW_RUNTIME_STATE = "linkType": "HARD"\ }]\ ]],\ + ["tsconfck", [\ + ["npm:3.1.3", {\ + "packageLocation": "../../../../.yarn/berry/cache/tsconfck-npm-3.1.3-614e755dcd-10c0.zip/node_modules/tsconfck/",\ + "packageDependencies": [\ + ["tsconfck", "npm:3.1.3"]\ + ],\ + "linkType": "SOFT"\ + }],\ + ["virtual:74c185d179e23ac09391c7413eea4c1ac3c77afe3340615008f45524dfd5b58fc4bffdeccdfe3e0b4ce317ebf712bdf878ad2018155efb09e7d6bffbc4569f7f#npm:3.1.3", {\ + "packageLocation": "./.yarn/__virtual__/tsconfck-virtual-a03c0309f2/5/.yarn/berry/cache/tsconfck-npm-3.1.3-614e755dcd-10c0.zip/node_modules/tsconfck/",\ + "packageDependencies": [\ + ["tsconfck", "virtual:74c185d179e23ac09391c7413eea4c1ac3c77afe3340615008f45524dfd5b58fc4bffdeccdfe3e0b4ce317ebf712bdf878ad2018155efb09e7d6bffbc4569f7f#npm:3.1.3"],\ + ["@types/typescript", null],\ + ["typescript", null]\ + ],\ + "packagePeers": [\ + "@types/typescript",\ + "typescript"\ + ],\ + "linkType": "HARD"\ + }]\ + ]],\ ["tsconfig-paths", [\ ["npm:3.15.0", {\ "packageLocation": "../../../../.yarn/berry/cache/tsconfig-paths-npm-3.15.0-ff68930e0e-10c0.zip/node_modules/tsconfig-paths/",\ @@ -18381,6 +18413,31 @@ const RAW_RUNTIME_STATE = "linkType": "HARD"\ }]\ ]],\ + ["vite-tsconfig-paths", [\ + ["npm:5.0.1", {\ + "packageLocation": "../../../../.yarn/berry/cache/vite-tsconfig-paths-npm-5.0.1-a4cd1cbcaf-10c0.zip/node_modules/vite-tsconfig-paths/",\ + "packageDependencies": [\ + ["vite-tsconfig-paths", "npm:5.0.1"]\ + ],\ + "linkType": "SOFT"\ + }],\ + ["virtual:b4c1c1be62fb935fd619e316b1ceced91b915e641eccd2188c44772d8f7ececf8da6978d623a01aeb3db3c58211d21e4eb7a46f09a4d6380d4cf0903f1a77fea#npm:5.0.1", {\ + "packageLocation": "./.yarn/__virtual__/vite-tsconfig-paths-virtual-74c185d179/5/.yarn/berry/cache/vite-tsconfig-paths-npm-5.0.1-a4cd1cbcaf-10c0.zip/node_modules/vite-tsconfig-paths/",\ + "packageDependencies": [\ + ["vite-tsconfig-paths", "virtual:b4c1c1be62fb935fd619e316b1ceced91b915e641eccd2188c44772d8f7ececf8da6978d623a01aeb3db3c58211d21e4eb7a46f09a4d6380d4cf0903f1a77fea#npm:5.0.1"],\ + ["@types/vite", null],\ + ["debug", "virtual:1ff4b5f90832ba0a9c93ba1223af226e44ba70c1126a3740d93562b97bc36544e896a5e95908196f7458713e6a6089a34bfc67362fc6df7fa093bd06c878be47#npm:4.3.7"],\ + ["globrex", "npm:0.1.2"],\ + ["tsconfck", "virtual:74c185d179e23ac09391c7413eea4c1ac3c77afe3340615008f45524dfd5b58fc4bffdeccdfe3e0b4ce317ebf712bdf878ad2018155efb09e7d6bffbc4569f7f#npm:3.1.3"],\ + ["vite", "virtual:2c30557d8ca5e7c67e7558d45bc44bef6c7a622b34a97fa8102f3235f92769c87777d34ed37059c12d8f3a21841ea06cf4badc5dc796697b0f765c17db6e24e5#npm:5.4.6"]\ + ],\ + "packagePeers": [\ + "@types/vite",\ + "vite"\ + ],\ + "linkType": "HARD"\ + }]\ + ]],\ ["vm-browserify", [\ ["npm:1.1.2", {\ "packageLocation": "../../../../.yarn/berry/cache/vm-browserify-npm-1.1.2-f96404b36f-10c0.zip/node_modules/vm-browserify/",\ diff --git a/frontend/techpick-extension/package.json b/frontend/techpick-extension/package.json index 599f14af6..313f51483 100644 --- a/frontend/techpick-extension/package.json +++ b/frontend/techpick-extension/package.json @@ -35,6 +35,7 @@ "typescript": "^5.5.3", "typescript-eslint": "^8.0.1", "vite": "^5.4.1", - "vite-plugin-static-copy": "^1.0.6" + "vite-plugin-static-copy": "^1.0.6", + "vite-tsconfig-paths": "^5.0.1" } } diff --git a/frontend/techpick-extension/src/pages/BookmarkPage.tsx b/frontend/techpick-extension/src/pages/BookmarkPage.tsx index 19d649b1f..95fd36fb3 100644 --- a/frontend/techpick-extension/src/pages/BookmarkPage.tsx +++ b/frontend/techpick-extension/src/pages/BookmarkPage.tsx @@ -1,4 +1,4 @@ -import { CurrentTabInfo } from '@/'; +import { CurrentTabInfo } from '@/components'; export function BookmarkPage() { return ( diff --git a/frontend/techpick-extension/tsconfig.app.json b/frontend/techpick-extension/tsconfig.app.json index f0a235055..c574a553a 100644 --- a/frontend/techpick-extension/tsconfig.app.json +++ b/frontend/techpick-extension/tsconfig.app.json @@ -1,5 +1,9 @@ { "compilerOptions": { + "baseUrl": ".", + "paths": { + "@/*": ["src/*"] + }, "target": "ES2020", "useDefineForClassFields": true, "lib": ["ES2020", "DOM", "DOM.Iterable"], diff --git a/frontend/techpick-extension/tsconfig.node.json b/frontend/techpick-extension/tsconfig.node.json index 0d3d71446..eb8456ff3 100644 --- a/frontend/techpick-extension/tsconfig.node.json +++ b/frontend/techpick-extension/tsconfig.node.json @@ -1,5 +1,9 @@ { "compilerOptions": { + "baseUrl": ".", + "paths": { + "@/*": ["src/*"] + }, "target": "ES2022", "lib": ["ES2023"], "module": "ESNext", diff --git a/frontend/techpick-extension/vite.config.ts b/frontend/techpick-extension/vite.config.ts index 867292129..1ebe3a20e 100644 --- a/frontend/techpick-extension/vite.config.ts +++ b/frontend/techpick-extension/vite.config.ts @@ -3,6 +3,7 @@ import react from '@vitejs/plugin-react'; import { resolve } from 'path'; import { viteStaticCopy } from 'vite-plugin-static-copy'; import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin'; +import tsconfigPaths from 'vite-tsconfig-paths'; // https://vitejs.dev/config/ export default defineConfig({ @@ -28,5 +29,6 @@ export default defineConfig({ targets: [{ src: './src/chorme-extension/manifest.json', dest: '.' }], }), vanillaExtractPlugin(), + tsconfigPaths(), ], }); diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 2b831ab9b..93693b138 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -8318,6 +8318,13 @@ __metadata: languageName: node linkType: hard +"globrex@npm:^0.1.2": + version: 0.1.2 + resolution: "globrex@npm:0.1.2" + checksum: 10c0/a54c029520cf58bda1d8884f72bd49b4cd74e977883268d931fd83bcbd1a9eb96d57c7dbd4ad80148fb9247467ebfb9b215630b2ed7563b2a8de02e1ff7f89d1 + languageName: node + linkType: hard + "goober@npm:^2.1.10": version: 2.1.14 resolution: "goober@npm:2.1.14" @@ -13128,6 +13135,7 @@ __metadata: typescript-eslint: "npm:^8.0.1" vite: "npm:^5.4.1" vite-plugin-static-copy: "npm:^1.0.6" + vite-tsconfig-paths: "npm:^5.0.1" zustand: "npm:4.5.5" languageName: unknown linkType: soft @@ -13340,6 +13348,20 @@ __metadata: languageName: node linkType: hard +"tsconfck@npm:^3.0.3": + version: 3.1.3 + resolution: "tsconfck@npm:3.1.3" + peerDependencies: + typescript: ^5.0.0 + peerDependenciesMeta: + typescript: + optional: true + bin: + tsconfck: bin/tsconfck.js + checksum: 10c0/64f7a8ed0a6d36b0902dfc0075e791d2242f7634644f124343ec0dec4f3f70092f929c5a9f59496d51883aa81bb1e595deb92a219593575d2e75b849064713d1 + languageName: node + linkType: hard + "tsconfig-paths-webpack-plugin@npm:^4.0.1": version: 4.1.0 resolution: "tsconfig-paths-webpack-plugin@npm:4.1.0" @@ -13845,6 +13867,22 @@ __metadata: languageName: node linkType: hard +"vite-tsconfig-paths@npm:^5.0.1": + version: 5.0.1 + resolution: "vite-tsconfig-paths@npm:5.0.1" + dependencies: + debug: "npm:^4.1.1" + globrex: "npm:^0.1.2" + tsconfck: "npm:^3.0.3" + peerDependencies: + vite: "*" + peerDependenciesMeta: + vite: + optional: true + checksum: 10c0/3c68a4d5df21ed4ef81749c20e91c5978989ed06bffc01688b3f1a0fe65951b461a68f0c017ad930a088cfe7a8cc04d0c8d955dfb8719d5edc7fb0ba9bf38a73 + languageName: node + linkType: hard + "vite@npm:^5.0.0, vite@npm:^5.0.11, vite@npm:^5.4.1": version: 5.4.6 resolution: "vite@npm:5.4.6" From 7d280ffafc65aeee44b56f666a89741a87fe2166 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:33:14 +0900 Subject: [PATCH 03/13] =?UTF-8?q?build:=20build=EC=8B=9C=EC=97=90=EB=8A=94?= =?UTF-8?q?=20=EB=B9=8C=EB=93=9C=EB=A5=BC=20=ED=95=9C=EB=B2=88=ED=95=98?= =?UTF-8?q?=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/techpick-extension/vite.config.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/frontend/techpick-extension/vite.config.ts b/frontend/techpick-extension/vite.config.ts index 1ebe3a20e..0233cd530 100644 --- a/frontend/techpick-extension/vite.config.ts +++ b/frontend/techpick-extension/vite.config.ts @@ -8,14 +8,14 @@ import tsconfigPaths from 'vite-tsconfig-paths'; // https://vitejs.dev/config/ export default defineConfig({ build: { - watch: { - include: 'src/**', - exclude: 'node_modules/**', - }, rollupOptions: { input: { - background: resolve(__dirname, 'src/chorme-extension/background.ts'), + background: resolve(__dirname, 'src/chrome-extension/background.ts'), popup: resolve(__dirname, './index.html'), + contentscript: resolve( + __dirname, + './src/chrome-extension/contentscript.ts' + ), }, output: { entryFileNames: '[name].js', @@ -26,7 +26,7 @@ export default defineConfig({ plugins: [ react(), viteStaticCopy({ - targets: [{ src: './src/chorme-extension/manifest.json', dest: '.' }], + targets: [{ src: './src/chrome-extension/manifest.json', dest: '.' }], }), vanillaExtractPlugin(), tsconfigPaths(), From 1baf6867752518a0fe0d2fede23d2c0ab6c36fe5 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Tue, 24 Sep 2024 18:44:03 +0900 Subject: [PATCH 04/13] =?UTF-8?q?feat:=20=EC=83=88=EB=A1=9C=EC=9A=B4=20?= =?UTF-8?q?=EB=A7=81=ED=81=AC=EB=A1=9C=20=EC=9D=B4=EB=8F=99=ED=95=A0=20?= =?UTF-8?q?=EC=8B=9C=20og=20image=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8A=94=20?= =?UTF-8?q?=EB=8F=99=EC=9E=91=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/chorme-extension/background.ts | 0 .../src/chrome-extension/background.ts | 29 +++++++++ .../src/chrome-extension/contentscript.ts | 19 ++++++ .../manifest.json | 8 ++- .../src/components/CurrentTabInfo.css.ts | 2 +- .../src/components/CurrentTabInfo.tsx | 65 +++++++++++++------ .../techpick-extension/src/constants/index.ts | 1 + .../src/constants/message.ts | 1 + .../techpick-extension/src/types/index.ts | 1 + .../techpick-extension/src/types/message.ts | 16 +++++ .../tsconfig.app.tsbuildinfo | 2 +- 11 files changed, 120 insertions(+), 24 deletions(-) delete mode 100644 frontend/techpick-extension/src/chorme-extension/background.ts create mode 100644 frontend/techpick-extension/src/chrome-extension/background.ts create mode 100644 frontend/techpick-extension/src/chrome-extension/contentscript.ts rename frontend/techpick-extension/src/{chorme-extension => chrome-extension}/manifest.json (56%) create mode 100644 frontend/techpick-extension/src/constants/index.ts create mode 100644 frontend/techpick-extension/src/constants/message.ts create mode 100644 frontend/techpick-extension/src/types/index.ts create mode 100644 frontend/techpick-extension/src/types/message.ts diff --git a/frontend/techpick-extension/src/chorme-extension/background.ts b/frontend/techpick-extension/src/chorme-extension/background.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/frontend/techpick-extension/src/chrome-extension/background.ts b/frontend/techpick-extension/src/chrome-extension/background.ts new file mode 100644 index 000000000..5a99daac1 --- /dev/null +++ b/frontend/techpick-extension/src/chrome-extension/background.ts @@ -0,0 +1,29 @@ +import { RequestMessageType, ResponseOgImageType } from '@/types'; + +// TODO: 1. 현재는, 촤초 불러올 시에만 og:image를 가져오는 코드를 동작하게 할 수 있다. 공식 문서를 찾아보고 왜 안되는지 원인을 찾아보기. +// TODO: + +// 1. script에서 og image를 받아온다. +let ResponseOgImage: ResponseOgImageType = { + type: '', + ogImageUrl: null, +}; + +chrome.runtime.onMessage.addListener((response: ResponseOgImageType) => { + if (response.type === 'OG_IMAGE_FROM_SCRIPT') { + console.log('get message from script', response); + ResponseOgImage = response; + } +}); + +// 2. popup에서 요청을 받는다. +chrome.runtime.onMessage.addListener((message: RequestMessageType) => { + if (message === 'REQUEST_OG_IMAGE_TO_BACK') { + console.log('get message on background', message); + // 3. script에 요청을 보낸다. + chrome.runtime.sendMessage({ + type: 'OG_IMAGE_TO_COMPONENT', + ogImageUrl: ResponseOgImage.ogImageUrl, + }); + } +}); diff --git a/frontend/techpick-extension/src/chrome-extension/contentscript.ts b/frontend/techpick-extension/src/chrome-extension/contentscript.ts new file mode 100644 index 000000000..43730f582 --- /dev/null +++ b/frontend/techpick-extension/src/chrome-extension/contentscript.ts @@ -0,0 +1,19 @@ +// import { REQUEST_OG_IMAGE } from '@/constants'; +import { ResponseOgImageType } from '@/types'; + +console.log('adadadas'); + +const ogImageTag = + document.querySelector("meta[property='og:image']") || + document.querySelector("meta[name='og:image']"); + +const ogImageUrl = + ogImageTag instanceof HTMLMetaElement ? ogImageTag.content : null; + +console.log('ogImageUrl', ogImageUrl); + +// 1. back에 응답을 보내준다. +chrome.runtime.sendMessage({ + type: 'OG_IMAGE_FROM_SCRIPT', + ogImageUrl, +}); diff --git a/frontend/techpick-extension/src/chorme-extension/manifest.json b/frontend/techpick-extension/src/chrome-extension/manifest.json similarity index 56% rename from frontend/techpick-extension/src/chorme-extension/manifest.json rename to frontend/techpick-extension/src/chrome-extension/manifest.json index a4574d751..58352b4e3 100644 --- a/frontend/techpick-extension/src/chorme-extension/manifest.json +++ b/frontend/techpick-extension/src/chrome-extension/manifest.json @@ -3,10 +3,16 @@ "name": "TechPick Extension", "description": "TechPick Extension description", "version": "0.1", - "permissions": ["tabs"], + "permissions": ["tabs", "activeTab", "scripting"], "background": { "service_worker": "background.js" }, + "content_scripts": [ + { + "matches": ["https://*/*", "http://*/*", "file://*/*"], + "js": ["contentscript.js"] + } + ], "action": { "default_popup": "index.html" } diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.css.ts b/frontend/techpick-extension/src/components/CurrentTabInfo.css.ts index dbc513884..cb49fb61b 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.css.ts +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.css.ts @@ -1,3 +1,3 @@ import { style } from '@vanilla-extract/css'; -export const container = style({ width: '300px', backgroundColor: 'red' }); +export const container = style({ width: '300px', backgroundColor: 'white' }); diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx index 060deb529..cab2e7fe1 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx @@ -1,33 +1,56 @@ import React, { useEffect, useState } from 'react'; -import { container } from './CurrentTabInfo.css'; +// import { REQUEST_OG_IMAGE } from '@/constants'; +import { ResponseOgImageType } from '@/types'; + +interface TabInfo { + title: string; + url: string; + ogImage?: string | null; +} export const CurrentTabInfo: React.FC = () => { - const [tabInfo, setTabInfo] = useState<{ title: string; url: string } | null>( - null - ); + const [tabInfo, setTabInfo] = useState(null); useEffect(() => { - const getCurrentTab = async () => { - chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { - if (tabs[0]) { - setTabInfo({ - title: tabs[0].title || 'No Title', - url: tabs[0].url || 'No URL', - }); - } - }); - }; + // 현재 탭 정보 가져오기 + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + const tab = tabs[0]; + setTabInfo({ title: tab.title || 'No Title', url: tab.url || 'No URL' }); + return; + }); - getCurrentTab(); - }, []); + // 1. 요청 보내기 + console.log('send message'); + chrome.runtime.sendMessage('REQUEST_OG_IMAGE_TO_BACK'); - if (!tabInfo) return
Loading...
; + // 2. 응답 기다리기. + console.log('waiting message'); + chrome.runtime.onMessage.addListener((message: ResponseOgImageType) => { + if (message.type === 'OG_IMAGE_TO_COMPONENT') { + console.log('get message on CurrentTabInfo', message); + + setTabInfo((prev) => { + if (prev) { + return { + ...prev, + ogImage: message.ogImageUrl, + }; + } + return prev; + }); + } + }); + }, []); return ( -
-

Current Tab Info

-

Title: {tabInfo.title}

-

URL: {tabInfo.url}

+
+

{tabInfo?.title}

+

URL: {tabInfo?.url}

+ {tabInfo?.ogImage ? ( + Open Graph + ) : ( +

No OG Image

+ )}
); }; diff --git a/frontend/techpick-extension/src/constants/index.ts b/frontend/techpick-extension/src/constants/index.ts new file mode 100644 index 000000000..f54558745 --- /dev/null +++ b/frontend/techpick-extension/src/constants/index.ts @@ -0,0 +1 @@ +export * from './message'; diff --git a/frontend/techpick-extension/src/constants/message.ts b/frontend/techpick-extension/src/constants/message.ts new file mode 100644 index 000000000..e46d6da57 --- /dev/null +++ b/frontend/techpick-extension/src/constants/message.ts @@ -0,0 +1 @@ +export const REQUEST_OG_IMAGE = 'REQUEST_OG_IMAGE'; diff --git a/frontend/techpick-extension/src/types/index.ts b/frontend/techpick-extension/src/types/index.ts new file mode 100644 index 000000000..f54558745 --- /dev/null +++ b/frontend/techpick-extension/src/types/index.ts @@ -0,0 +1 @@ +export * from './message'; diff --git a/frontend/techpick-extension/src/types/message.ts b/frontend/techpick-extension/src/types/message.ts new file mode 100644 index 000000000..b9a00b361 --- /dev/null +++ b/frontend/techpick-extension/src/types/message.ts @@ -0,0 +1,16 @@ +export type RequestMessageType = string; + +export type ResponseMessageType = { + type: string; +}; + +export type ResponseOgImageType = { + type: 'OG_IMAGE' | string; + ogImageUrl: string | null; +}; + +export const isResponseOgImageType = ( + message: ResponseMessageType +): message is ResponseOgImageType => { + return message.type === 'OG_IMAGE' && 'ogImageUrl' in message; +}; diff --git a/frontend/techpick-extension/tsconfig.app.tsbuildinfo b/frontend/techpick-extension/tsconfig.app.tsbuildinfo index 241e35bb4..cb48780d7 100644 --- a/frontend/techpick-extension/tsconfig.app.tsbuildinfo +++ b/frontend/techpick-extension/tsconfig.app.tsbuildinfo @@ -1 +1 @@ -{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/chorme-extension/background.ts","./src/components/CurrentTabInfo.css.ts","./src/components/CurrentTabInfo.tsx","./src/components/index.ts","./src/features/index.ts","./src/hooks/index.ts","./src/lib/index.ts","./src/pages/BookmarkPage.tsx","./src/pages/index.ts","./src/routers/index.tsx","./src/widgets/index.ts"],"version":"5.6.2"} \ No newline at end of file +{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/chrome-extension/background.ts","./src/chrome-extension/contentscript.ts","./src/components/CurrentTabInfo.css.ts","./src/components/CurrentTabInfo.tsx","./src/components/index.ts","./src/constants/index.ts","./src/constants/message.ts","./src/features/index.ts","./src/hooks/index.ts","./src/lib/index.ts","./src/pages/BookmarkPage.tsx","./src/pages/index.ts","./src/routers/index.tsx","./src/types/index.ts","./src/types/message.ts","./src/widgets/index.ts"],"version":"5.6.2"} \ No newline at end of file From 7d512920d95f85966716cfbdfd75ffb14c460001 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Tue, 24 Sep 2024 20:38:19 +0900 Subject: [PATCH 05/13] =?UTF-8?q?feat:=20tab=20=EC=97=B4=20=EB=95=8C=20?= =?UTF-8?q?=EB=A7=88=EB=8B=A4=20og=20image=20=EA=B0=80=EC=A0=B8=EC=98=A4?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/chrome-extension/background.ts | 49 +++++++------- .../src/chrome-extension/contentscript.ts | 28 ++++---- .../src/components/CurrentTabInfo.tsx | 67 +++++++++++++------ 3 files changed, 84 insertions(+), 60 deletions(-) diff --git a/frontend/techpick-extension/src/chrome-extension/background.ts b/frontend/techpick-extension/src/chrome-extension/background.ts index 5a99daac1..9df7e6ee4 100644 --- a/frontend/techpick-extension/src/chrome-extension/background.ts +++ b/frontend/techpick-extension/src/chrome-extension/background.ts @@ -1,29 +1,28 @@ -import { RequestMessageType, ResponseOgImageType } from '@/types'; +// import { RequestMessageType, ResponseOgImageType } from '@/types'; -// TODO: 1. 현재는, 촤초 불러올 시에만 og:image를 가져오는 코드를 동작하게 할 수 있다. 공식 문서를 찾아보고 왜 안되는지 원인을 찾아보기. -// TODO: +// // TODO: 1. 현재는, 촤초 불러올 시에만 og:image를 가져오는 코드를 동작하게 할 수 있다. 공식 문서를 찾아보고 왜 안되는지 원인을 찾아보기. -// 1. script에서 og image를 받아온다. -let ResponseOgImage: ResponseOgImageType = { - type: '', - ogImageUrl: null, -}; +// // 1. script에서 og image를 받아온다. +// let ResponseOgImage: ResponseOgImageType = { +// type: '', +// ogImageUrl: null, +// }; -chrome.runtime.onMessage.addListener((response: ResponseOgImageType) => { - if (response.type === 'OG_IMAGE_FROM_SCRIPT') { - console.log('get message from script', response); - ResponseOgImage = response; - } -}); +// chrome.runtime.onMessage.addListener((response: ResponseOgImageType) => { +// if (response.type === 'OG_IMAGE_FROM_SCRIPT') { +// console.log('get message from script', response); +// ResponseOgImage = response; +// } +// }); -// 2. popup에서 요청을 받는다. -chrome.runtime.onMessage.addListener((message: RequestMessageType) => { - if (message === 'REQUEST_OG_IMAGE_TO_BACK') { - console.log('get message on background', message); - // 3. script에 요청을 보낸다. - chrome.runtime.sendMessage({ - type: 'OG_IMAGE_TO_COMPONENT', - ogImageUrl: ResponseOgImage.ogImageUrl, - }); - } -}); +// // 2. popup에서 요청을 받는다. +// chrome.runtime.onMessage.addListener((message: RequestMessageType) => { +// if (message === 'REQUEST_OG_IMAGE_TO_BACK') { +// console.log('get message on background', message); +// // 3. script에 요청을 보낸다. +// chrome.runtime.sendMessage({ +// type: 'OG_IMAGE_TO_COMPONENT', +// ogImageUrl: ResponseOgImage.ogImageUrl, +// }); +// } +// }); diff --git a/frontend/techpick-extension/src/chrome-extension/contentscript.ts b/frontend/techpick-extension/src/chrome-extension/contentscript.ts index 43730f582..0bebed04b 100644 --- a/frontend/techpick-extension/src/chrome-extension/contentscript.ts +++ b/frontend/techpick-extension/src/chrome-extension/contentscript.ts @@ -1,19 +1,17 @@ -// import { REQUEST_OG_IMAGE } from '@/constants'; -import { ResponseOgImageType } from '@/types'; +window.addEventListener('message', (event) => { + // 다른 페이지에서 오는 메시지는 무시 + if (event.source !== window || event.data.type !== 'GET_OG_IMAGE') return; -console.log('adadadas'); + // Open Graph 이미지 추출 + const ogImageTag = + document.querySelector("meta[property='og:image']") || + document.querySelector("meta[name='og:image']"); + const ogImageUrl = + ogImageTag instanceof HTMLMetaElement ? ogImageTag.content : null; -const ogImageTag = - document.querySelector("meta[property='og:image']") || - document.querySelector("meta[name='og:image']"); + console.log('script work!!! ogImageUrl:', ogImageUrl); -const ogImageUrl = - ogImageTag instanceof HTMLMetaElement ? ogImageTag.content : null; - -console.log('ogImageUrl', ogImageUrl); - -// 1. back에 응답을 보내준다. -chrome.runtime.sendMessage({ - type: 'OG_IMAGE_FROM_SCRIPT', - ogImageUrl, + // Open Graph 이미지 추출 후 메시지를 페이지에 다시 보냄 + window.postMessage({ type: 'FROM_CONTENT', ogImage: ogImageUrl }, '*'); + chrome.runtime.sendMessage({ type: 'FROM_SCRIPT', ogImage: ogImageUrl }); }); diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx index cab2e7fe1..6f030b375 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx @@ -1,6 +1,4 @@ import React, { useEffect, useState } from 'react'; -// import { REQUEST_OG_IMAGE } from '@/constants'; -import { ResponseOgImageType } from '@/types'; interface TabInfo { title: string; @@ -15,31 +13,60 @@ export const CurrentTabInfo: React.FC = () => { // 현재 탭 정보 가져오기 chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { const tab = tabs[0]; - setTabInfo({ title: tab.title || 'No Title', url: tab.url || 'No URL' }); - return; + setTabInfo({ + title: tab.title || 'No Title', + url: tab.url || 'No URL', + }); + + // // 현재 탭의 content_script에 postMessage를 통해 Open Graph 이미지 요청 + chrome.scripting.executeScript({ + target: { tabId: tab.id || 0 }, + func: () => { + console.log('post message'); + window.postMessage({ type: 'GET_OG_IMAGE' }, '*'); + }, + }); + + chrome.runtime.sendMessage({ greeting: 'hello' }); }); - // 1. 요청 보내기 - console.log('send message'); - chrome.runtime.sendMessage('REQUEST_OG_IMAGE_TO_BACK'); + const messageListener = (event: MessageEvent) => { + console.log('listen!!!', event.data); // 로그 추가 + if (event.source !== window || event.data.type !== 'FROM_CONTENT') return; + + // 기존 상태와 병합하여 새로운 상태 설정 + setTabInfo((prev) => { + if (!prev) return null; // prev가 null일 경우 처리 + return { + ...prev, + ogImage: event.data.ogImage, // 여기서 ogImage를 설정 + }; + }); + }; - // 2. 응답 기다리기. - console.log('waiting message'); - chrome.runtime.onMessage.addListener((message: ResponseOgImageType) => { - if (message.type === 'OG_IMAGE_TO_COMPONENT') { - console.log('get message on CurrentTabInfo', message); + window.addEventListener('message', messageListener); + chrome.runtime.onMessage.addListener( + (message: { type: string; ogImage: string | null }) => { + if (message.type !== 'FROM_SCRIPT') { + console.log('is not FROM_SCRIPT'); + return; + } + + // 기존 상태와 병합하여 새로운 상태 설정 setTabInfo((prev) => { - if (prev) { - return { - ...prev, - ogImage: message.ogImageUrl, - }; - } - return prev; + if (!prev) return null; // prev가 null일 경우 처리 + return { + ...prev, + ogImage: message.ogImage, // 여기서 ogImage를 설정 + }; }); } - }); + ); + + return () => { + window.removeEventListener('message', messageListener); + }; }, []); return ( From 8acb8bc83a03116b8b09a86dfd13416abff575ab Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Tue, 24 Sep 2024 20:43:57 +0900 Subject: [PATCH 06/13] =?UTF-8?q?refactor:=20=EC=93=B0=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/chrome-extension/contentscript.ts | 1 - .../src/components/CurrentTabInfo.tsx | 22 ++----------------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/frontend/techpick-extension/src/chrome-extension/contentscript.ts b/frontend/techpick-extension/src/chrome-extension/contentscript.ts index 0bebed04b..46fe94551 100644 --- a/frontend/techpick-extension/src/chrome-extension/contentscript.ts +++ b/frontend/techpick-extension/src/chrome-extension/contentscript.ts @@ -12,6 +12,5 @@ window.addEventListener('message', (event) => { console.log('script work!!! ogImageUrl:', ogImageUrl); // Open Graph 이미지 추출 후 메시지를 페이지에 다시 보냄 - window.postMessage({ type: 'FROM_CONTENT', ogImage: ogImageUrl }, '*'); chrome.runtime.sendMessage({ type: 'FROM_SCRIPT', ogImage: ogImageUrl }); }); diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx index 6f030b375..6be7f2f6a 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx @@ -18,7 +18,7 @@ export const CurrentTabInfo: React.FC = () => { url: tab.url || 'No URL', }); - // // 현재 탭의 content_script에 postMessage를 통해 Open Graph 이미지 요청 + // 현재 탭의 content_script에 postMessage를 통해 Open Graph 이미지 요청 chrome.scripting.executeScript({ target: { tabId: tab.id || 0 }, func: () => { @@ -27,24 +27,10 @@ export const CurrentTabInfo: React.FC = () => { }, }); + // contentScript로부터 응답을 받는다. chrome.runtime.sendMessage({ greeting: 'hello' }); }); - const messageListener = (event: MessageEvent) => { - console.log('listen!!!', event.data); // 로그 추가 - if (event.source !== window || event.data.type !== 'FROM_CONTENT') return; - - // 기존 상태와 병합하여 새로운 상태 설정 - setTabInfo((prev) => { - if (!prev) return null; // prev가 null일 경우 처리 - return { - ...prev, - ogImage: event.data.ogImage, // 여기서 ogImage를 설정 - }; - }); - }; - - window.addEventListener('message', messageListener); chrome.runtime.onMessage.addListener( (message: { type: string; ogImage: string | null }) => { if (message.type !== 'FROM_SCRIPT') { @@ -63,10 +49,6 @@ export const CurrentTabInfo: React.FC = () => { }); } ); - - return () => { - window.removeEventListener('message', messageListener); - }; }, []); return ( From bc26a87ce20a3c573ade3d469c25948062e508a8 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Tue, 24 Sep 2024 20:45:00 +0900 Subject: [PATCH 07/13] =?UTF-8?q?chore:=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techpick-extension/src/chrome-extension/contentscript.ts | 2 -- frontend/techpick-extension/src/components/CurrentTabInfo.tsx | 3 --- 2 files changed, 5 deletions(-) diff --git a/frontend/techpick-extension/src/chrome-extension/contentscript.ts b/frontend/techpick-extension/src/chrome-extension/contentscript.ts index 46fe94551..3e059aab8 100644 --- a/frontend/techpick-extension/src/chrome-extension/contentscript.ts +++ b/frontend/techpick-extension/src/chrome-extension/contentscript.ts @@ -9,8 +9,6 @@ window.addEventListener('message', (event) => { const ogImageUrl = ogImageTag instanceof HTMLMetaElement ? ogImageTag.content : null; - console.log('script work!!! ogImageUrl:', ogImageUrl); - // Open Graph 이미지 추출 후 메시지를 페이지에 다시 보냄 chrome.runtime.sendMessage({ type: 'FROM_SCRIPT', ogImage: ogImageUrl }); }); diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx index 6be7f2f6a..08b711f3f 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx @@ -22,7 +22,6 @@ export const CurrentTabInfo: React.FC = () => { chrome.scripting.executeScript({ target: { tabId: tab.id || 0 }, func: () => { - console.log('post message'); window.postMessage({ type: 'GET_OG_IMAGE' }, '*'); }, }); @@ -34,8 +33,6 @@ export const CurrentTabInfo: React.FC = () => { chrome.runtime.onMessage.addListener( (message: { type: string; ogImage: string | null }) => { if (message.type !== 'FROM_SCRIPT') { - console.log('is not FROM_SCRIPT'); - return; } From f982bb23584fe794520573b042b266aa5d6ac8b6 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:25:32 +0900 Subject: [PATCH 08/13] =?UTF-8?q?feat:=20=ED=8C=8C=EB=B9=84=EC=BD=98=20?= =?UTF-8?q?=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/chrome-extension/contentscript.ts | 16 ++++++++-------- .../src/components/CurrentTabInfo.tsx | 15 ++++++--------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/frontend/techpick-extension/src/chrome-extension/contentscript.ts b/frontend/techpick-extension/src/chrome-extension/contentscript.ts index 3e059aab8..458686227 100644 --- a/frontend/techpick-extension/src/chrome-extension/contentscript.ts +++ b/frontend/techpick-extension/src/chrome-extension/contentscript.ts @@ -1,14 +1,14 @@ window.addEventListener('message', (event) => { // 다른 페이지에서 오는 메시지는 무시 - if (event.source !== window || event.data.type !== 'GET_OG_IMAGE') return; + if (event.source !== window || event.data.type !== 'REQUEST_FAVICON') return; - // Open Graph 이미지 추출 - const ogImageTag = - document.querySelector("meta[property='og:image']") || - document.querySelector("meta[name='og:image']"); - const ogImageUrl = - ogImageTag instanceof HTMLMetaElement ? ogImageTag.content : null; + // 파비콘 추출 + const faviconLink = + document.querySelector("link[rel='icon']") || + document.querySelector("link[rel='shortcut icon']"); + const faviconUrl = + faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; // Open Graph 이미지 추출 후 메시지를 페이지에 다시 보냄 - chrome.runtime.sendMessage({ type: 'FROM_SCRIPT', ogImage: ogImageUrl }); + chrome.runtime.sendMessage({ type: 'RESPONSE_FAVICON', ogImage: faviconUrl }); }); diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx index 08b711f3f..8e9289278 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import { useEffect, useState } from 'react'; interface TabInfo { title: string; @@ -6,7 +6,7 @@ interface TabInfo { ogImage?: string | null; } -export const CurrentTabInfo: React.FC = () => { +export function CurrentTabInfo() { const [tabInfo, setTabInfo] = useState(null); useEffect(() => { @@ -18,21 +18,18 @@ export const CurrentTabInfo: React.FC = () => { url: tab.url || 'No URL', }); - // 현재 탭의 content_script에 postMessage를 통해 Open Graph 이미지 요청 + // 현재 탭의 content_script에 postMessage를 통해 파비콘 이미지 요청 chrome.scripting.executeScript({ target: { tabId: tab.id || 0 }, func: () => { - window.postMessage({ type: 'GET_OG_IMAGE' }, '*'); + window.postMessage({ type: 'REQUEST_FAVICON' }, '*'); }, }); - - // contentScript로부터 응답을 받는다. - chrome.runtime.sendMessage({ greeting: 'hello' }); }); chrome.runtime.onMessage.addListener( (message: { type: string; ogImage: string | null }) => { - if (message.type !== 'FROM_SCRIPT') { + if (message.type !== 'RESPONSE_FAVICON') { return; } @@ -59,4 +56,4 @@ export const CurrentTabInfo: React.FC = () => { )}
); -}; +} From 24e6a29d116a753e25c8e622d0c975e55ddca561 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:51:53 +0900 Subject: [PATCH 09/13] =?UTF-8?q?feat:=20=EC=84=9C=EB=B9=84=EC=8A=A4?= =?UTF-8?q?=EC=9B=8C=EC=BB=A4=EB=A5=BC=20=EC=9D=B4=EC=9A=A9=ED=95=B4?= =?UTF-8?q?=EC=84=9C=20=EB=8F=99=EC=9E=91=ED=95=98=EB=8A=94=20=EA=B2=83?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/chrome-extension/background.ts | 66 ++++++++++++------- .../src/chrome-extension/contentscript.ts | 24 +++---- .../src/components/CurrentTabInfo.tsx | 58 +++++++++------- 3 files changed, 90 insertions(+), 58 deletions(-) diff --git a/frontend/techpick-extension/src/chrome-extension/background.ts b/frontend/techpick-extension/src/chrome-extension/background.ts index 9df7e6ee4..61b99b6bd 100644 --- a/frontend/techpick-extension/src/chrome-extension/background.ts +++ b/frontend/techpick-extension/src/chrome-extension/background.ts @@ -1,28 +1,46 @@ -// import { RequestMessageType, ResponseOgImageType } from '@/types'; +// background.js +const getFavicon = () => { + const faviconLink = + document.querySelector("link[rel='icon']") || + document.querySelector("link[rel='shortcut icon']"); + const faviconUrl = + faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; -// // TODO: 1. 현재는, 촤초 불러올 시에만 og:image를 가져오는 코드를 동작하게 할 수 있다. 공식 문서를 찾아보고 왜 안되는지 원인을 찾아보기. + console.log('faviconUrl', faviconUrl); -// // 1. script에서 og image를 받아온다. -// let ResponseOgImage: ResponseOgImageType = { -// type: '', -// ogImageUrl: null, -// }; + return faviconUrl; // 파비콘 URL 반환 +}; -// chrome.runtime.onMessage.addListener((response: ResponseOgImageType) => { -// if (response.type === 'OG_IMAGE_FROM_SCRIPT') { -// console.log('get message from script', response); -// ResponseOgImage = response; -// } -// }); +// 메시지를 수신하여 파비콘을 가져오는 리스너 +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.type === 'REQUEST_FAVICON') { + console.log('sender', sender); -// // 2. popup에서 요청을 받는다. -// chrome.runtime.onMessage.addListener((message: RequestMessageType) => { -// if (message === 'REQUEST_OG_IMAGE_TO_BACK') { -// console.log('get message on background', message); -// // 3. script에 요청을 보낸다. -// chrome.runtime.sendMessage({ -// type: 'OG_IMAGE_TO_COMPONENT', -// ogImageUrl: ResponseOgImage.ogImageUrl, -// }); -// } -// }); + const tabId = message.tabId; // optional chaining 사용 + + console.log('background work', tabId); + + if (tabId) { + // tabId가 정의된 경우에만 실행 + chrome.scripting.executeScript( + { + target: { tabId: tabId }, + func: getFavicon, + }, + (results) => { + console.log('background results', results); + + if (results && results[0]) { + const faviconUrl = results[0].result; + sendResponse({ favicon: faviconUrl }); // 응답으로 파비콘 URL 반환 + } else { + sendResponse({ favicon: null }); // 파비콘을 찾지 못했을 경우 + } + } + ); + } else { + sendResponse({ favicon: null }); // tabId가 없을 경우 응답 + } + return true; // 비동기 응답을 위해 true를 반환 + } +}); diff --git a/frontend/techpick-extension/src/chrome-extension/contentscript.ts b/frontend/techpick-extension/src/chrome-extension/contentscript.ts index 458686227..77d915d0d 100644 --- a/frontend/techpick-extension/src/chrome-extension/contentscript.ts +++ b/frontend/techpick-extension/src/chrome-extension/contentscript.ts @@ -1,14 +1,14 @@ -window.addEventListener('message', (event) => { - // 다른 페이지에서 오는 메시지는 무시 - if (event.source !== window || event.data.type !== 'REQUEST_FAVICON') return; +// window.addEventListener('message', (event) => { +// // 다른 페이지에서 오는 메시지는 무시 +// if (event.source !== window || event.data.type !== 'REQUEST_FAVICON') return; - // 파비콘 추출 - const faviconLink = - document.querySelector("link[rel='icon']") || - document.querySelector("link[rel='shortcut icon']"); - const faviconUrl = - faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; +// // 파비콘 추출 +// const faviconLink = +// document.querySelector("link[rel='icon']") || +// document.querySelector("link[rel='shortcut icon']"); +// const faviconUrl = +// faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; - // Open Graph 이미지 추출 후 메시지를 페이지에 다시 보냄 - chrome.runtime.sendMessage({ type: 'RESPONSE_FAVICON', ogImage: faviconUrl }); -}); +// // Open Graph 이미지 추출 후 메시지를 페이지에 다시 보냄 +// chrome.runtime.sendMessage({ type: 'RESPONSE_FAVICON', ogImage: faviconUrl }); +// }); diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx index 8e9289278..1a15eb60f 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx @@ -4,6 +4,7 @@ interface TabInfo { title: string; url: string; ogImage?: string | null; + favicon?: string | null; // favicon 속성 추가 } export function CurrentTabInfo() { @@ -18,37 +19,50 @@ export function CurrentTabInfo() { url: tab.url || 'No URL', }); + console.log('tab.url', tab.url); + + if ( + tab.url && + !(tab.url.startsWith('http://') || tab.url.startsWith('https://')) + ) { + return; + } + // 현재 탭의 content_script에 postMessage를 통해 파비콘 이미지 요청 - chrome.scripting.executeScript({ - target: { tabId: tab.id || 0 }, - func: () => { - window.postMessage({ type: 'REQUEST_FAVICON' }, '*'); - }, - }); - }); + console.log('send message REQUEST_FAVICON'); + chrome.runtime.sendMessage( + { type: 'REQUEST_FAVICON', tabId: tab.id }, + (response) => { + console.log('response', response); - chrome.runtime.onMessage.addListener( - (message: { type: string; ogImage: string | null }) => { - if (message.type !== 'RESPONSE_FAVICON') { - return; - } + // 이전 상태를 기반으로 새로운 상태를 설정 + setTabInfo((prev) => { + if (prev === null) + return { + title: 'No Title', + url: 'No URL', + favicon: response.favicon || null, + }; // prev가 null인 경우 초기값 설정 - // 기존 상태와 병합하여 새로운 상태 설정 - setTabInfo((prev) => { - if (!prev) return null; // prev가 null일 경우 처리 - return { - ...prev, - ogImage: message.ogImage, // 여기서 ogImage를 설정 - }; - }); - } - ); + return { + ...prev, + favicon: response.favicon || null, // 응답에서 받은 파비콘 URL 설정 + }; + }); + } + ); + }); }, []); return (

{tabInfo?.title}

URL: {tabInfo?.url}

+ {tabInfo?.favicon ? ( + Favicon + ) : ( +

No Favicon

+ )} {tabInfo?.ogImage ? ( Open Graph ) : ( From fe9aad3513e5348dc082aed5c4c6d9dc9140d58f Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:15:30 +0900 Subject: [PATCH 10/13] =?UTF-8?q?refactor:=20useGetTabInfo=20hook=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/chrome-extension/background.ts | 65 +++++++++---------- .../src/chrome-extension/manifest.json | 5 +- .../src/components/CurrentTabInfo.tsx | 62 +----------------- .../src/constants/message.ts | 2 +- .../techpick-extension/src/hooks/index.ts | 1 + .../src/hooks/useGetTabInfo.tsx | 46 +++++++++++++ .../tsconfig.app.tsbuildinfo | 2 +- 7 files changed, 87 insertions(+), 96 deletions(-) create mode 100644 frontend/techpick-extension/src/hooks/useGetTabInfo.tsx diff --git a/frontend/techpick-extension/src/chrome-extension/background.ts b/frontend/techpick-extension/src/chrome-extension/background.ts index 61b99b6bd..dd3d51eae 100644 --- a/frontend/techpick-extension/src/chrome-extension/background.ts +++ b/frontend/techpick-extension/src/chrome-extension/background.ts @@ -1,4 +1,5 @@ -// background.js +import { REQUEST_FAVICON } from '@/constants'; + const getFavicon = () => { const faviconLink = document.querySelector("link[rel='icon']") || @@ -6,41 +7,39 @@ const getFavicon = () => { const faviconUrl = faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; - console.log('faviconUrl', faviconUrl); - return faviconUrl; // 파비콘 URL 반환 }; // 메시지를 수신하여 파비콘을 가져오는 리스너 -chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { - if (message.type === 'REQUEST_FAVICON') { - console.log('sender', sender); - - const tabId = message.tabId; // optional chaining 사용 - - console.log('background work', tabId); - - if (tabId) { - // tabId가 정의된 경우에만 실행 - chrome.scripting.executeScript( - { - target: { tabId: tabId }, - func: getFavicon, - }, - (results) => { - console.log('background results', results); - - if (results && results[0]) { - const faviconUrl = results[0].result; - sendResponse({ favicon: faviconUrl }); // 응답으로 파비콘 URL 반환 - } else { - sendResponse({ favicon: null }); // 파비콘을 찾지 못했을 경우 - } - } - ); - } else { - sendResponse({ favicon: null }); // tabId가 없을 경우 응답 - } - return true; // 비동기 응답을 위해 true를 반환 +chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => { + if (message.type !== REQUEST_FAVICON || !message.tabId) { + return; + } + + const tabId = message.tabId; + + if (!tabId) { + sendResponse({ favicon: null }); // tabId가 없을 경우 응답 } + + // tabId가 정의된 경우에만 실행 + chrome.scripting.executeScript( + { + target: { tabId: tabId }, + func: getFavicon, + }, + (results) => { + // 파비콘을 찾지 못했을 경우 + if (!results || !results[0]) { + sendResponse({ favicon: null }); + } + + const faviconUrl = results[0].result; + sendResponse({ favicon: faviconUrl }); + } + ); + + return true; + // 비동기 응답을 위해 true를 반환 + // chrome.scripting.executeScript가 비동기적이다. }); diff --git a/frontend/techpick-extension/src/chrome-extension/manifest.json b/frontend/techpick-extension/src/chrome-extension/manifest.json index 58352b4e3..7c65df198 100644 --- a/frontend/techpick-extension/src/chrome-extension/manifest.json +++ b/frontend/techpick-extension/src/chrome-extension/manifest.json @@ -5,11 +5,12 @@ "version": "0.1", "permissions": ["tabs", "activeTab", "scripting"], "background": { - "service_worker": "background.js" + "service_worker": "background.js", + "type": "module" }, "content_scripts": [ { - "matches": ["https://*/*", "http://*/*", "file://*/*"], + "matches": ["https://*/*", "http://*/*"], "js": ["contentscript.js"] } ], diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx index 1a15eb60f..5c77b9ea4 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx @@ -1,73 +1,17 @@ -import { useEffect, useState } from 'react'; - -interface TabInfo { - title: string; - url: string; - ogImage?: string | null; - favicon?: string | null; // favicon 속성 추가 -} +import { useGetTabInfo } from '@/hooks'; export function CurrentTabInfo() { - const [tabInfo, setTabInfo] = useState(null); - - useEffect(() => { - // 현재 탭 정보 가져오기 - chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { - const tab = tabs[0]; - setTabInfo({ - title: tab.title || 'No Title', - url: tab.url || 'No URL', - }); - - console.log('tab.url', tab.url); - - if ( - tab.url && - !(tab.url.startsWith('http://') || tab.url.startsWith('https://')) - ) { - return; - } - - // 현재 탭의 content_script에 postMessage를 통해 파비콘 이미지 요청 - console.log('send message REQUEST_FAVICON'); - chrome.runtime.sendMessage( - { type: 'REQUEST_FAVICON', tabId: tab.id }, - (response) => { - console.log('response', response); - - // 이전 상태를 기반으로 새로운 상태를 설정 - setTabInfo((prev) => { - if (prev === null) - return { - title: 'No Title', - url: 'No URL', - favicon: response.favicon || null, - }; // prev가 null인 경우 초기값 설정 - - return { - ...prev, - favicon: response.favicon || null, // 응답에서 받은 파비콘 URL 설정 - }; - }); - } - ); - }); - }, []); + const tabInfo = useGetTabInfo(); return (

{tabInfo?.title}

URL: {tabInfo?.url}

{tabInfo?.favicon ? ( - Favicon + Favicon ) : (

No Favicon

)} - {tabInfo?.ogImage ? ( - Open Graph - ) : ( -

No OG Image

- )}
); } diff --git a/frontend/techpick-extension/src/constants/message.ts b/frontend/techpick-extension/src/constants/message.ts index e46d6da57..ab002bd44 100644 --- a/frontend/techpick-extension/src/constants/message.ts +++ b/frontend/techpick-extension/src/constants/message.ts @@ -1 +1 @@ -export const REQUEST_OG_IMAGE = 'REQUEST_OG_IMAGE'; +export const REQUEST_FAVICON = 'REQUEST_FAVICON'; diff --git a/frontend/techpick-extension/src/hooks/index.ts b/frontend/techpick-extension/src/hooks/index.ts index e69de29bb..7480e9ee3 100644 --- a/frontend/techpick-extension/src/hooks/index.ts +++ b/frontend/techpick-extension/src/hooks/index.ts @@ -0,0 +1 @@ +export { useGetTabInfo } from './useGetTabInfo'; diff --git a/frontend/techpick-extension/src/hooks/useGetTabInfo.tsx b/frontend/techpick-extension/src/hooks/useGetTabInfo.tsx new file mode 100644 index 000000000..8a3157fec --- /dev/null +++ b/frontend/techpick-extension/src/hooks/useGetTabInfo.tsx @@ -0,0 +1,46 @@ +import { useEffect, useState } from 'react'; +import { REQUEST_FAVICON } from '@/constants'; + +interface TabInfo { + title: string | undefined; + url: string | undefined; + favicon?: string | undefined | null; +} + +export function useGetTabInfo() { + const [tabInfo, setTabInfo] = useState({ + title: undefined, + url: undefined, + favicon: undefined, + }); + + useEffect(() => { + // 현재 탭 정보 가져오기 + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + const tab = tabs[0]; + setTabInfo({ + title: tab.title, + url: tab.url, + }); + + // http가 아닌 다른 url(ex: chrome)은 권한이 없어서 확장 프로그램에서 에러가 날 수 있습니다. + if (tab.url && !tab.url.startsWith('http')) { + return; + } + + chrome.runtime.sendMessage( + { type: REQUEST_FAVICON, tabId: tab.id }, + (response: { favicon: string | null | undefined }) => { + setTabInfo((prev) => { + return { + ...prev, + favicon: response.favicon, + }; + }); + } + ); + }); + }, []); + + return tabInfo; +} diff --git a/frontend/techpick-extension/tsconfig.app.tsbuildinfo b/frontend/techpick-extension/tsconfig.app.tsbuildinfo index cb48780d7..c0eddbf94 100644 --- a/frontend/techpick-extension/tsconfig.app.tsbuildinfo +++ b/frontend/techpick-extension/tsconfig.app.tsbuildinfo @@ -1 +1 @@ -{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/chrome-extension/background.ts","./src/chrome-extension/contentscript.ts","./src/components/CurrentTabInfo.css.ts","./src/components/CurrentTabInfo.tsx","./src/components/index.ts","./src/constants/index.ts","./src/constants/message.ts","./src/features/index.ts","./src/hooks/index.ts","./src/lib/index.ts","./src/pages/BookmarkPage.tsx","./src/pages/index.ts","./src/routers/index.tsx","./src/types/index.ts","./src/types/message.ts","./src/widgets/index.ts"],"version":"5.6.2"} \ No newline at end of file +{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/chrome-extension/background.ts","./src/chrome-extension/contentscript.ts","./src/components/CurrentTabInfo.css.ts","./src/components/CurrentTabInfo.tsx","./src/components/index.ts","./src/constants/index.ts","./src/constants/message.ts","./src/features/index.ts","./src/hooks/index.ts","./src/hooks/useGetTabInfo.tsx","./src/lib/index.ts","./src/pages/BookmarkPage.tsx","./src/pages/index.ts","./src/routers/index.tsx","./src/types/index.ts","./src/types/message.ts","./src/widgets/index.ts"],"version":"5.6.2"} \ No newline at end of file From 65f1e5e01bcec993c234898787005363a3351fbe Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:04:56 +0900 Subject: [PATCH 11/13] =?UTF-8?q?refactor:=20background.ts=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/chrome-extension/background.ts | 45 ------------------- .../src/chrome-extension/contentscript.ts | 14 ------ .../techpick-extension/src/hooks/index.ts | 2 +- .../hooks/useGetTabInfo/useGetTabInfo.lib.ts | 18 ++++++++ .../{ => useGetTabInfo}/useGetTabInfo.tsx | 36 ++++++++------- 5 files changed, 39 insertions(+), 76 deletions(-) create mode 100644 frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.lib.ts rename frontend/techpick-extension/src/hooks/{ => useGetTabInfo}/useGetTabInfo.tsx (53%) diff --git a/frontend/techpick-extension/src/chrome-extension/background.ts b/frontend/techpick-extension/src/chrome-extension/background.ts index dd3d51eae..e69de29bb 100644 --- a/frontend/techpick-extension/src/chrome-extension/background.ts +++ b/frontend/techpick-extension/src/chrome-extension/background.ts @@ -1,45 +0,0 @@ -import { REQUEST_FAVICON } from '@/constants'; - -const getFavicon = () => { - const faviconLink = - document.querySelector("link[rel='icon']") || - document.querySelector("link[rel='shortcut icon']"); - const faviconUrl = - faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; - - return faviconUrl; // 파비콘 URL 반환 -}; - -// 메시지를 수신하여 파비콘을 가져오는 리스너 -chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => { - if (message.type !== REQUEST_FAVICON || !message.tabId) { - return; - } - - const tabId = message.tabId; - - if (!tabId) { - sendResponse({ favicon: null }); // tabId가 없을 경우 응답 - } - - // tabId가 정의된 경우에만 실행 - chrome.scripting.executeScript( - { - target: { tabId: tabId }, - func: getFavicon, - }, - (results) => { - // 파비콘을 찾지 못했을 경우 - if (!results || !results[0]) { - sendResponse({ favicon: null }); - } - - const faviconUrl = results[0].result; - sendResponse({ favicon: faviconUrl }); - } - ); - - return true; - // 비동기 응답을 위해 true를 반환 - // chrome.scripting.executeScript가 비동기적이다. -}); diff --git a/frontend/techpick-extension/src/chrome-extension/contentscript.ts b/frontend/techpick-extension/src/chrome-extension/contentscript.ts index 77d915d0d..e69de29bb 100644 --- a/frontend/techpick-extension/src/chrome-extension/contentscript.ts +++ b/frontend/techpick-extension/src/chrome-extension/contentscript.ts @@ -1,14 +0,0 @@ -// window.addEventListener('message', (event) => { -// // 다른 페이지에서 오는 메시지는 무시 -// if (event.source !== window || event.data.type !== 'REQUEST_FAVICON') return; - -// // 파비콘 추출 -// const faviconLink = -// document.querySelector("link[rel='icon']") || -// document.querySelector("link[rel='shortcut icon']"); -// const faviconUrl = -// faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; - -// // Open Graph 이미지 추출 후 메시지를 페이지에 다시 보냄 -// chrome.runtime.sendMessage({ type: 'RESPONSE_FAVICON', ogImage: faviconUrl }); -// }); diff --git a/frontend/techpick-extension/src/hooks/index.ts b/frontend/techpick-extension/src/hooks/index.ts index 7480e9ee3..c070287e2 100644 --- a/frontend/techpick-extension/src/hooks/index.ts +++ b/frontend/techpick-extension/src/hooks/index.ts @@ -1 +1 @@ -export { useGetTabInfo } from './useGetTabInfo'; +export { useGetTabInfo } from './useGetTabInfo/useGetTabInfo'; diff --git a/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.lib.ts b/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.lib.ts new file mode 100644 index 000000000..9bd9042ff --- /dev/null +++ b/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.lib.ts @@ -0,0 +1,18 @@ +const getFaviconFromDom = () => { + const faviconLink = + document.querySelector("link[rel='icon']") || + document.querySelector("link[rel='shortcut icon']") || + document.querySelector('link'); + + return faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; +}; + +export const fetchFavicon = async (tabId: number) => { + const results = await chrome.scripting.executeScript({ + target: { tabId }, + func: getFaviconFromDom, + }); + + // 결과에서 파비콘 URL을 반환 + return results && results[0] ? results[0].result : null; +}; diff --git a/frontend/techpick-extension/src/hooks/useGetTabInfo.tsx b/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.tsx similarity index 53% rename from frontend/techpick-extension/src/hooks/useGetTabInfo.tsx rename to frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.tsx index 8a3157fec..a3a71a748 100644 --- a/frontend/techpick-extension/src/hooks/useGetTabInfo.tsx +++ b/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react'; -import { REQUEST_FAVICON } from '@/constants'; +import { fetchFavicon } from './useGetTabInfo.lib'; interface TabInfo { title: string | undefined; @@ -15,31 +15,35 @@ export function useGetTabInfo() { }); useEffect(() => { - // 현재 탭 정보 가져오기 - chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + const getTabInfo = async () => { + // 현재 탭 정보 가져오기 + const tabs = await chrome.tabs.query({ + active: true, + currentWindow: true, + }); const tab = tabs[0]; setTabInfo({ title: tab.title, url: tab.url, }); - // http가 아닌 다른 url(ex: chrome)은 권한이 없어서 확장 프로그램에서 에러가 날 수 있습니다. + // http가 아닌 다른 URL(ex: chrome)은 권한이 없어서 확장 프로그램에서 에러가 날 수 있습니다. if (tab.url && !tab.url.startsWith('http')) { return; } - chrome.runtime.sendMessage( - { type: REQUEST_FAVICON, tabId: tab.id }, - (response: { favicon: string | null | undefined }) => { - setTabInfo((prev) => { - return { - ...prev, - favicon: response.favicon, - }; - }); - } - ); - }); + if (!tab.id) { + return; + } + + const faviconUrl = await fetchFavicon(tab.id); + setTabInfo((prev) => ({ + ...prev, + favicon: faviconUrl, + })); + }; + + getTabInfo(); }, []); return tabInfo; From 35b145cabe3fe928cd041eef6af88cb885e9c766 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:32:31 +0900 Subject: [PATCH 12/13] =?UTF-8?q?feat:=20og:description=20=EA=B0=80?= =?UTF-8?q?=EC=A0=B8=EC=98=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/CurrentTabInfo.tsx | 5 +++ .../hooks/useGetTabInfo/useGetTabInfo.lib.ts | 40 ++++++++++++++----- .../src/hooks/useGetTabInfo/useGetTabInfo.tsx | 14 +++++-- .../tsconfig.app.tsbuildinfo | 2 +- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx index 5c77b9ea4..7205435dd 100644 --- a/frontend/techpick-extension/src/components/CurrentTabInfo.tsx +++ b/frontend/techpick-extension/src/components/CurrentTabInfo.tsx @@ -12,6 +12,11 @@ export function CurrentTabInfo() { ) : (

No Favicon

)} + {tabInfo.ogDescription ? ( +

{tabInfo.ogDescription}

+ ) : ( +

No Og description

+ )}
); } diff --git a/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.lib.ts b/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.lib.ts index 9bd9042ff..f4bc7f99e 100644 --- a/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.lib.ts +++ b/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.lib.ts @@ -1,18 +1,38 @@ -const getFaviconFromDom = () => { - const faviconLink = - document.querySelector("link[rel='icon']") || - document.querySelector("link[rel='shortcut icon']") || - document.querySelector('link'); +// executeScript는 전달된 함수의 실행 컨텍스트가 전역 스코프가 되기 때문에, 외부에서 정의한 함수에 접근할 수 없습니다. +const getPageInfoFromDom = () => { + const getFaviconFromDom = () => { + const faviconLink = + document.querySelector("link[rel='icon']") || + document.querySelector("link[rel='shortcut icon']") || + document.querySelector('link'); - return faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; + return faviconLink instanceof HTMLLinkElement ? faviconLink.href : null; + }; + + const getOgDescriptionFromDom = () => { + const ogDescriptionMeta = document.querySelector( + "meta[property='og:description']" + ); + + return ogDescriptionMeta instanceof HTMLMetaElement + ? ogDescriptionMeta.content + : null; + }; + + const faviconUrl = getFaviconFromDom(); + const ogDescription = getOgDescriptionFromDom(); + + return { + faviconUrl, + ogDescription, + }; }; -export const fetchFavicon = async (tabId: number) => { +export const fetchFaviconAndDescription = async (tabId: number) => { const results = await chrome.scripting.executeScript({ target: { tabId }, - func: getFaviconFromDom, + func: getPageInfoFromDom, }); - // 결과에서 파비콘 URL을 반환 - return results && results[0] ? results[0].result : null; + return results[0].result; }; diff --git a/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.tsx b/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.tsx index a3a71a748..5a04eb8ce 100644 --- a/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.tsx +++ b/frontend/techpick-extension/src/hooks/useGetTabInfo/useGetTabInfo.tsx @@ -1,17 +1,17 @@ import { useEffect, useState } from 'react'; -import { fetchFavicon } from './useGetTabInfo.lib'; +import { fetchFaviconAndDescription } from './useGetTabInfo.lib'; interface TabInfo { title: string | undefined; url: string | undefined; favicon?: string | undefined | null; + ogDescription?: string | undefined | null; } export function useGetTabInfo() { const [tabInfo, setTabInfo] = useState({ title: undefined, url: undefined, - favicon: undefined, }); useEffect(() => { @@ -36,10 +36,16 @@ export function useGetTabInfo() { return; } - const faviconUrl = await fetchFavicon(tab.id); + const result = await fetchFaviconAndDescription(tab.id); + + if (!result) { + return; + } + setTabInfo((prev) => ({ ...prev, - favicon: faviconUrl, + favicon: result?.faviconUrl, + ogDescription: result?.ogDescription, })); }; diff --git a/frontend/techpick-extension/tsconfig.app.tsbuildinfo b/frontend/techpick-extension/tsconfig.app.tsbuildinfo index c0eddbf94..b71980104 100644 --- a/frontend/techpick-extension/tsconfig.app.tsbuildinfo +++ b/frontend/techpick-extension/tsconfig.app.tsbuildinfo @@ -1 +1 @@ -{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/chrome-extension/background.ts","./src/chrome-extension/contentscript.ts","./src/components/CurrentTabInfo.css.ts","./src/components/CurrentTabInfo.tsx","./src/components/index.ts","./src/constants/index.ts","./src/constants/message.ts","./src/features/index.ts","./src/hooks/index.ts","./src/hooks/useGetTabInfo.tsx","./src/lib/index.ts","./src/pages/BookmarkPage.tsx","./src/pages/index.ts","./src/routers/index.tsx","./src/types/index.ts","./src/types/message.ts","./src/widgets/index.ts"],"version":"5.6.2"} \ No newline at end of file +{"root":["./src/App.tsx","./src/main.tsx","./src/vite-env.d.ts","./src/chrome-extension/background.ts","./src/chrome-extension/contentscript.ts","./src/components/CurrentTabInfo.css.ts","./src/components/CurrentTabInfo.tsx","./src/components/index.ts","./src/constants/index.ts","./src/constants/message.ts","./src/features/index.ts","./src/hooks/index.ts","./src/hooks/useGetTabInfo/useGetTabInfo.lib.ts","./src/hooks/useGetTabInfo/useGetTabInfo.tsx","./src/lib/index.ts","./src/pages/BookmarkPage.tsx","./src/pages/index.ts","./src/routers/index.tsx","./src/types/index.ts","./src/types/message.ts","./src/widgets/index.ts"],"version":"5.6.2"} \ No newline at end of file From 9f7705766c97736871828722460601b44ddc6100 Mon Sep 17 00:00:00 2001 From: dmdgpdi <33450285+dmdgpdi@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:37:23 +0900 Subject: [PATCH 13/13] =?UTF-8?q?chore:=20=EC=93=B0=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=20=EC=83=81=EC=88=98=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/techpick-extension/src/constants/index.ts | 1 - frontend/techpick-extension/src/constants/message.ts | 1 - 2 files changed, 2 deletions(-) delete mode 100644 frontend/techpick-extension/src/constants/message.ts diff --git a/frontend/techpick-extension/src/constants/index.ts b/frontend/techpick-extension/src/constants/index.ts index f54558745..e69de29bb 100644 --- a/frontend/techpick-extension/src/constants/index.ts +++ b/frontend/techpick-extension/src/constants/index.ts @@ -1 +0,0 @@ -export * from './message'; diff --git a/frontend/techpick-extension/src/constants/message.ts b/frontend/techpick-extension/src/constants/message.ts deleted file mode 100644 index ab002bd44..000000000 --- a/frontend/techpick-extension/src/constants/message.ts +++ /dev/null @@ -1 +0,0 @@ -export const REQUEST_FAVICON = 'REQUEST_FAVICON';