2022-12-02 04:50:41 +00:00
|
|
|
import { LoginMessage, ErrorResponse, MessageType } from './message';
|
2022-11-27 09:20:27 +00:00
|
|
|
|
|
|
|
let isLoggingIn = false;
|
|
|
|
|
|
|
|
const renderUI = () => {
|
|
|
|
document
|
|
|
|
.querySelectorAll('.worlds-container:first-of-type .world-select')
|
|
|
|
.forEach((el) => {
|
|
|
|
const cloned = el.cloneNode(true);
|
|
|
|
if (!(cloned instanceof HTMLAnchorElement)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cloned.addEventListener('click', handleClick);
|
|
|
|
|
|
|
|
const span = cloned.childNodes.item(1);
|
|
|
|
if (!span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
span.textContent += ' (SS)';
|
|
|
|
|
|
|
|
el.parentNode?.insertBefore(cloned, el.nextSibling);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClick = async (e: MouseEvent) => {
|
|
|
|
if (!(e.currentTarget instanceof HTMLAnchorElement)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (isLoggingIn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isLoggingIn = true;
|
|
|
|
|
|
|
|
const url = new URL(e.currentTarget.href);
|
|
|
|
const server = extractServerFromURL(url);
|
|
|
|
chrome.runtime
|
|
|
|
.sendMessage({
|
|
|
|
type: MessageType.LOGIN,
|
|
|
|
server,
|
|
|
|
loginUrl: url.toString(),
|
|
|
|
url: url.protocol + '//' + url.host.replace('www', server),
|
|
|
|
} as LoginMessage)
|
2022-12-02 04:50:41 +00:00
|
|
|
.then((resp: ErrorResponse) => {
|
2022-11-27 09:20:27 +00:00
|
|
|
isLoggingIn = false;
|
|
|
|
if (!resp?.error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.error(resp.error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
init();
|