-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.ts
More file actions
39 lines (35 loc) · 1.13 KB
/
background.ts
File metadata and controls
39 lines (35 loc) · 1.13 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
console.log('백그라운드 기능');
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.type === 'FETCH_OG_META') {
fetch(message.url)
.then((res) => res.text())
.then((html) => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const getMeta = (prop) =>
doc
.querySelector(`meta[property="${prop}"]`)
?.getAttribute('content') || '';
const makeAbsoluteUrl = (base, img) => {
try {
return img ? new URL(img, base).href : '';
} catch {
return img;
}
};
const image = getMeta('og:image');
sendResponse({
title: getMeta('og:title'),
description: getMeta('og:description'),
siteName: getMeta('og:site_name'),
image: makeAbsoluteUrl(message.url, image),
url: getMeta('og:url') || message.url,
});
})
.catch((err) => {
console.error('OG fetch 실패:', err);
sendResponse(null);
});
return true; // async 응답
}
});