다음은 TypeScript를 사용하여 크롬 확장 앱에서 프로파일 사용자 정보를 가져오는 방법을 보여주는 코드입니다. 필요한 파일과 코드를 정리했습니다.
1. manifest.json (권한 설정이 이미 완료되었다고 가정)
manifest.json 파일은 따로 제공하지 않습니다. 확장 앱의 권한 설정에서 "identity"가 포함되어 있어야 합니다.
2. background.ts
// background.ts
function getUserProfile(): void {
chrome.identity.getProfileUserInfo((userInfo) => {
console.log('User ID:', userInfo.id);
console.log('User Email:', userInfo.email);
});
}
function authenticateAndFetchProfile(): void {
chrome.identity.getAuthToken({ interactive: true }, (token) => {
if (chrome.runtime.lastError) {
console.error('Error:', chrome.runtime.lastError.message);
} else {
getUserProfile();
}
});
}
// 크롬 확장 프로그램이 시작될 때 프로파일 정보를 가져옴
chrome.runtime.onInstalled.addListener(() => {
authenticateAndFetchProfile();
});
설명
• background.ts: 크롬 확장 앱이 설치되거나 업데이트될 때 authenticateAndFetchProfile 함수가 호출되어 사용자 인증을 수행하고 프로파일 정보를 가져옵니다.
이 코드를 통해 크롬 확장 앱에서 프로파일 사용자 정보를 가져올 수 있습니다.
반응형