// background.js
// 구글 계정 정보를 가져오는 함수
function getGoogleAccountInfo(callback) {
chrome.identity.getAuthToken({ interactive: true }, (token) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
alert(`Failed to get Google account info: ${chrome.runtime.lastError.message}`);
return;
}
fetch('https://www.googleapis.com/oauth2/v1/userinfo?alt=json', {
headers: {
'Authorization': `Bearer ${token}`
}
})
.then(response => response.json())
.then(data => {
callback(data.email);
})
.catch(error => {
console.error(error);
alert(`Failed to get Google account info: ${error.message}`);
});
});
}
// 활성화된 탭이 변경될 때 이벤트 처리
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
if (tab) {
const tabInfo = {
url: tab.url,
title: tab.title
};
// 구글 계정 정보와 탭 정보를 alert로 표시
getGoogleAccountInfo((email) => {
chrome.scripting.executeScript({
target: { tabId: activeInfo.tabId },
func: showAlert,
args: [tabInfo, email]
});
});
}
});
});
// 모든 탭 클릭을 감지하기 위해 탭 업데이트 이벤트 추가
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.active) {
const tabInfo = {
url: tab.url,
title: tab.title
};
// 구글 계정 정보와 탭 정보를 alert로 표시
getGoogleAccountInfo((email) => {
chrome.scripting.executeScript({
target: { tabId: tabId },
func: showAlert,
args: [tabInfo, email]
});
});
}
});
// alert를 띄우는 함수
function showAlert(tabInfo, email) {
alert(`URL: ${tabInfo.url}\nTitle: ${tabInfo.title}\nEmail: ${email}`);
}
첫댓글 {
"manifest_version": 3,
"name": "Active Tab Tracker",
"version": "1.0",
"permissions": ["identity", "tabs", "activeTab", "scripting"],
"background": {
"service_worker": "background.js"
},
"oauth2": {
"client_id": "701644259365-h3uc3q1f85c9g13o2f7j0ung9afuef8s.apps.googleusercontent.com",
"scopes": ["https://www.googleapis.com/auth/userinfo.email"]
},
"action": {
"default_popup": "popup.html"
}
}
function getGoogleAccountInfo(callback) {
chrome.identity.getAuthToken({ interactive: true }, (token) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
alert(`Failed to get Google account info: ${chrome.runtime.lastError.message}`);
return;
}
fetch('https://www.googleapis.com/oauth2/v1/userinfo?alt=json', {
headers: {
'Authorization': `Bearer ${token}`
}
})
.then(response => response.json())
.then(data => {
callback(data.email);
})
.catch(error => {
console.error(error);
alert(`Failed to ge
t Google account info: ${error.message}`);
});
});
}
// 활성화된 탭이 변경될 때 이벤트 처리
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
if (tab) {
const tabInfo = {
url: tab.url,
title: tab.title
};
// 구글 계정 정보와 탭 정보를 alert로 표시
getGoogleAccountInfo((email) => {
chrome.scripting.executeScript({
target: { tabId: activeInfo.tabId },
func: showAlert,
args: [tabInfo, email]
});
});
}
});
});
// 모든 탭 클릭을 감지하기 위해 탭 업데이트 이벤트 추가
chrome.tabs.onUpdated.add
Listener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.active) {
const tabInfo = {
url: tab.url,
title: tab.title
};
// 구글 계정 정보와 탭 정보를 alert로 표시
getGoogleAccountInfo((email) => {
chrome.scripting.executeScript({
target: { tabId: tabId },
func: showAlert,
args: [tabInfo, email]
});
});
}
});
// alert를 띄우는 함수
function showAlert(tabInfo, email) {
alert(`URL: ${tabInfo.url}\nTitle: ${tabInfo.title}\nEmail: ${email}`);
}