-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusePageMeta.ts
More file actions
55 lines (44 loc) · 1.39 KB
/
usePageMeta.ts
File metadata and controls
55 lines (44 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useEffect, useState } from 'react';
import { OgImageFetcher } from '@utils/OGFetch';
export interface PageMeta {
url: string;
title: string;
description: string;
imgUrl: string;
}
export const usePageMeta = () => {
const [meta, setMeta] = useState<PageMeta>({
url: '',
title: '',
description: '',
imgUrl: '',
});
useEffect(() => {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
const activeTab = tabs[0];
if (!activeTab?.url) return;
const currentUrl = activeTab.url;
chrome.storage.local.set({ bookmarkedUrl: currentUrl });
const imageUrl = await OgImageFetcher({ url: currentUrl });
// 개발중에는 잠시 주석처리
// const isInternalChromePage =
// /^chrome:\/\//.test(currentUrl) ||
// /^edge:\/\//.test(currentUrl) ||
// /^about:/.test(currentUrl);
// // chrome-extension:// 은 내부 페이지로 취급하지 않음
// if (isInternalChromePage || !imageUrl?.title) {
// window.close();
// return;
// }
const newMeta = {
url: currentUrl,
title: imageUrl.title ?? '',
description: imageUrl.description ?? '',
imgUrl: imageUrl.image ?? '',
};
setMeta(newMeta);
chrome.storage.local.set({ titleSave: newMeta.title });
});
}, []);
return meta;
};