Skip to content

Commit

Permalink
feat/extension-get-username (#158)
Browse files Browse the repository at this point in the history
* feat: 백준룸즈의 로컬스토리지에서 username, provider 받아와서 서버에 보내기

* feat: 로그인 시 로컬스토리지에 userInfo 저장 / 로그아웃 시 삭제

* feat: userInfo에서 username대신 providerId 받아오기

* refactor: authContext -> authProvider
  • Loading branch information
kiuuon authored Dec 6, 2023
1 parent 9c30243 commit 21265c7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 49 deletions.
1 change: 1 addition & 0 deletions client/src/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FaArrowRight } from 'react-icons/fa6';
import Message from './Message';
import { useAuthContext } from '../contexts/AuthProvider';

import { ChatEvent, MessageInterface } from '../types/Message';
import { Socket } from 'socket.io-client';

Expand Down
8 changes: 7 additions & 1 deletion client/src/components/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { useAuthContext } from '../contexts/AuthProvider';

export default function Profile() {
const { user } = useAuthContext();

const userInfo = { provider: user?.provider, providerId: user?.providerId };
if (
!localStorage.getItem('userInfo') ||
localStorage.getItem('userInfo') !== JSON.stringify(userInfo)
) {
localStorage.setItem('userInfo', JSON.stringify(userInfo));
}
// TODO: AuthContext쪽 UserType 재정의
return (
<div className="mb-6 flex items-center justify-center gap-3">
Expand Down
21 changes: 10 additions & 11 deletions client/src/types/RoomCreateType.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { User } from "./UserType";
import { User } from './UserType';

export interface RoomCreateType {
code: string;
host: User;
users: User[];
endAt: string;
updatedAt: string;
deletedAt: string;
id: number;
createdAt: string;
}

code: string;
host: User;
users: User[];
endAt: string;
updatedAt: string;
deletedAt: string;
id: number;
createdAt: string;
}
40 changes: 10 additions & 30 deletions extension/background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let isActive = false;
let userInfo;

chrome.action.setBadgeText({ text: 'off' });
chrome.action.setBadgeBackgroundColor({ color: '#c0c0c0' });
Expand All @@ -7,52 +8,31 @@ chrome.runtime.onMessage.addListener(function (req) {
if (req.data === 'toggle') {
isActive = !isActive;
} else {
isActive = req.data;
isActive = req.isActive;
}

if (isActive) {
chrome.action.setBadgeText({ text: 'on' });
chrome.action.setBadgeBackgroundColor({ color: '#528BFF' });
userInfo = req.userInfo;
} else {
chrome.action.setBadgeText({ text: 'off' });
chrome.action.setBadgeBackgroundColor({ color: '#c0c0c0' });
}
});

chrome.webRequest.onBeforeRequest.addListener(
function (details) {
if (isActive) {
if (details.method === 'POST') {
const sourceCode = details.requestBody.formData.source[0];
console.log(sourceCode);
// fetch('', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify(sourceCode),
// });
}
}
},
{ urls: ['https://www.acmicpc.net/submit/*'] },
['requestBody'],
);

chrome.webRequest.onHeadersReceived.addListener(
function (details) {
if (isActive) {
if (details.method === 'POST') {
const submitURL = details.responseHeaders.filter((item) => item.name === 'location')[0].value;
console.log(submitURL);

// fetch('', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify(submitURL),
// });
fetch('https://api.baekjoonrooms.com/submission', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: { submitURL, provider: userInfo.provider, providerId: userInfo.providerId },
});
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Multiplayer rooms for BOJ",
"version": "0.1",
"permissions": ["webRequest"],
"host_permissions": ["https://www.acmicpc.net/submit/*"],
"host_permissions": ["https://www.acmicpc.net/submit/*", "https://api.baekjoonrooms.com/*"],
"background": {
"service_worker": "background.js"
},
Expand Down
14 changes: 8 additions & 6 deletions extension/scripts/content.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const observer = new MutationObserver((mutationRecords) => {
mutationRecords.forEach(injectScript);
});
const observer = new MutationObserver(injectScript);
let isRoom = false;

const targetNode = document.body;
const config = { childList: true, subtree: true };
Expand All @@ -9,14 +8,17 @@ observer.observe(targetNode, config);

function injectScript() {
const urlPattern = /^http:\/\/localhost:5173\/room\/.*$/;
if (urlPattern.test(window.location.href)) {
chrome.runtime.sendMessage({ data: true });
if (urlPattern.test(window.location.href) && !isRoom) {
const userInfo = JSON.parse(localStorage.getItem('userInfo'));
chrome.runtime.sendMessage({ isActive: true, userInfo });
isRoom = true;
}

const roomExitButton = document.getElementById('room-exit-button');
if (roomExitButton) {
roomExitButton.addEventListener('click', () => {
chrome.runtime.sendMessage({ data: false });
chrome.runtime.sendMessage({ isActive: false });
isRoom = false;
});
}
}

0 comments on commit 21265c7

Please sign in to comment.