This commit is contained in:
Dawid Wysokiński 2022-11-16 06:39:33 +01:00
parent ca555fb628
commit f8ef9ee252
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
3 changed files with 67 additions and 9 deletions

View File

@ -0,0 +1,31 @@
import { LoginMessage, Message, MessageType } from './message';
const COOKIE_NAME = 'sid';
chrome.runtime.onMessage.addListener(
(message: Message, sender, sendResponse) => {
switch (message.type) {
case MessageType.LOGIN:
handleLogin(message);
}
}
);
const handleLogin = async (message: LoginMessage) => {
const cookie = await chrome.cookies.get({
name: COOKIE_NAME,
url: message.url,
});
console.log(cookie);
const url = new URL(message.url);
url.pathname = '/game.php';
url.searchParams.set('screen', 'overview_villages');
const resp = await fetch(url.toString(), {
method: 'GET',
credentials: 'include',
redirect: 'manual',
});
console.log(resp.status, resp.redirected);
};

View File

@ -1,12 +1,4 @@
const handleClick = (e: MouseEvent) => {
if (!(e.currentTarget instanceof HTMLAnchorElement)) {
return;
}
e.preventDefault();
console.log(e.currentTarget.href);
};
import { LoginMessage, MessageType } from './message';
const renderUI = () => {
document
@ -29,6 +21,30 @@ const renderUI = () => {
});
};
const handleClick = (e: MouseEvent) => {
if (!(e.currentTarget instanceof HTMLAnchorElement)) {
return;
}
e.preventDefault();
const url = new URL(e.currentTarget.href);
const server = extractServerFromURL(url);
chrome.runtime.sendMessage({
type: MessageType.LOGIN,
server,
url: url.protocol + '//' + url.host.replace('www', server),
} as LoginMessage);
};
const extractServerFromURL = (url: URL): string => {
const split = url.pathname.split('/');
if (split.length <= 0) {
throw new Error(`invalid path: ${url.pathname}`);
}
return split[split.length - 1];
};
const init = () => {
renderUI();
};

11
src/message.ts Normal file
View File

@ -0,0 +1,11 @@
export enum MessageType {
LOGIN,
}
export type LoginMessage = {
type: MessageType.LOGIN;
url: string;
server: string;
};
export type Message = LoginMessage;