forked from AcaMate/AcaMate_Web
1. Console에 바로 나오던 메세지들 개발 환경에 따라 나오게 필터링 하는 서비스 개발 2. script 쪽에도 추가 하여 js 에서도 필터링 되게 구현
112 lines
3.7 KiB
JavaScript
112 lines
3.7 KiB
JavaScript
window.postWithHeader = function(url, method, headerKey, headerValue) {
|
|
fetch(url, {
|
|
method: method,
|
|
headers: {
|
|
[headerKey] : headerValue
|
|
}
|
|
}).then(res => {
|
|
if (res.redirected) {
|
|
window.location.href = res.url;
|
|
}
|
|
});
|
|
};
|
|
|
|
window.fetchWithHeaderAndReturnUrl = async function(args) {
|
|
try {
|
|
let url = args.url;
|
|
const queryParams = Object.entries(args)
|
|
.filter(([key]) => !['url', 'method', 'headerKey', 'headerValue'].includes(key))
|
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
.join('&');
|
|
if (queryParams) {
|
|
url += (url.includes('?') ? '&' : '?') + queryParams;
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
method: args.method,
|
|
headers: {
|
|
[args.headerKey]: args.headerValue
|
|
}
|
|
});
|
|
|
|
const contentType = response.headers.get('content-type');
|
|
if (!response.ok) {
|
|
window.appLogger.error('API 호출 실패:', response.status, response.statusText);
|
|
return null;
|
|
}
|
|
if (!contentType || !contentType.includes('application/json')) {
|
|
const text = await response.text();
|
|
window.appLogger.error('JSON이 아닌 응답:', text);
|
|
return null;
|
|
}
|
|
|
|
const data = await response.json();
|
|
window.appLogger.log('API 응답 데이터:', data);
|
|
return data;
|
|
} catch (error) {
|
|
window.appLogger.error('API 호출 중 오류 발생:', error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
window.loadConfig = async function(configFile) {
|
|
try {
|
|
window.appLogger.log('설정 파일 로드 시도:', configFile);
|
|
const response = await fetch(configFile);
|
|
if (!response.ok) {
|
|
window.appLogger.error('설정 파일 로드 실패:', response.status, response.statusText);
|
|
return null;
|
|
}
|
|
const config = await response.json();
|
|
window.appLogger.log('설정 파일 로드 성공:', configFile);
|
|
return config;
|
|
} catch (error) {
|
|
window.appLogger.error('설정 파일 로드 중 오류 발생:', error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
window.encryptText = async function(text, key, iv) {
|
|
try {
|
|
// XOR 암호화 구현
|
|
let result = '';
|
|
for (let i = 0; i < text.length; i++) {
|
|
const charCode = text.charCodeAt(i) ^ key.charCodeAt(i % key.length);
|
|
result += String.fromCharCode(charCode);
|
|
}
|
|
|
|
// UTF-8로 인코딩 후 Base64로 변환
|
|
const utf8Encoder = new TextEncoder();
|
|
const bytes = utf8Encoder.encode(result);
|
|
const base64 = btoa(String.fromCharCode.apply(null, bytes));
|
|
return base64;
|
|
} catch (error) {
|
|
window.appLogger.error('암호화 중 오류 발생:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
window.decryptText = async function(encryptedText, key, iv) {
|
|
try {
|
|
// Base64 디코딩 후 UTF-8 디코딩
|
|
const binary = atob(encryptedText);
|
|
const bytes = new Uint8Array(binary.length);
|
|
for (let i = 0; i < binary.length; i++) {
|
|
bytes[i] = binary.charCodeAt(i);
|
|
}
|
|
const utf8Decoder = new TextDecoder();
|
|
const text = utf8Decoder.decode(bytes);
|
|
|
|
// XOR 복호화
|
|
let result = '';
|
|
for (let i = 0; i < text.length; i++) {
|
|
const charCode = text.charCodeAt(i) ^ key.charCodeAt(i % key.length);
|
|
result += String.fromCharCode(charCode);
|
|
}
|
|
return result;
|
|
} catch (error) {
|
|
window.appLogger.error('복호화 중 오류 발생:', error);
|
|
throw error;
|
|
}
|
|
};
|