-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
55 lines (52 loc) · 1.59 KB
/
background.js
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
let reactTabId = null;
const reactPageUrl = "https://www.morib.in/";
// URL 전송 함수
function sendUrlToReactTab(currentUrl) {
if (reactTabId !== null) {
chrome.tabs.sendMessage(
reactTabId,
{ action: "urlUpdated", url: currentUrl },
function (response) {
if (chrome.runtime.lastError) {
// console.error("Error sending message to React tab:", chrome.runtime.lastError.message); -> 주석처리 안하면 계속 오류남
} else {
console.log("Message sent successfully to React tab");
}
}
);
} else {
console.error("React tab ID is null");
}
}
// 탭 활성화 감지시 발생하는 리스너
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
const currentUrl = tab.url;
if (currentUrl) {
if (currentUrl.includes(reactPageUrl)) {
reactTabId = activeInfo.tabId;
console.log("React tab activated with ID:", reactTabId);
} else {
sendUrlToReactTab(currentUrl);
}
} else {
console.error("No URL found for the active tab.");
}
});
});
// 탭 업데이트 감지 리스너
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete") {
const currentUrl = tab.url;
if (currentUrl) {
if (currentUrl.includes(reactPageUrl)) {
reactTabId = tabId;
console.log("React tab updated with ID:", reactTabId);
} else {
sendUrlToReactTab(currentUrl);
}
} else {
console.error("No URL found for the updated tab.");
}
}
});