diff --git a/src/content-script.ts b/src/content-script.ts
index 4403248..5f011a6 100644
--- a/src/content-script.ts
+++ b/src/content-script.ts
@@ -1,174 +1,90 @@
type RustCompanionAuthData = {
- steamId: string;
- token: string;
-};
+ steamId: string
+ token: string
+}
function handleRustCompanionAuthMessage(data: RustCompanionAuthData) {
- console.debug("Received Rust Companion authentication data:", data);
- createTokenDisplayElements(data.token);
+ console.debug("Received Rust Companion authentication data:", data)
+ createTokenDisplayElements(data.token)
}
function copyTokenToClipboard(token: string) {
- navigator.clipboard
- .writeText(token)
- .then(() => {
- console.debug("Token copied to clipboard");
- })
- .catch((error) => {
- console.error("Failed to copy token to clipboard:", error);
- });
+ navigator.clipboard
+ .writeText(token)
+ .then(() => {
+ console.debug("Token copied to clipboard")
+ })
+ .catch((error) => {
+ console.error("Failed to copy token to clipboard:", error)
+ })
}
function createTokenDisplayElements(token: string) {
- const overlayContainerEl = document.querySelector(
- ".page-wrapper .overlay-container",
- );
- if (!overlayContainerEl) {
- console.error("Failed to find overlay container element");
- return;
- }
-
- const overlayBodyEl = overlayContainerEl.querySelector(".overlay-body");
- if (!overlayBodyEl) {
- console.error("Failed to find overlay body element");
- return;
- }
-
- const tokenTitleEl = document.createElement("h2");
- tokenTitleEl.innerText = "Your token:";
- tokenTitleEl.className = "token-title";
- overlayBodyEl.appendChild(tokenTitleEl);
-
- const tokenTextAreaEl = document.createElement("textarea");
- tokenTextAreaEl.readOnly = true;
- tokenTextAreaEl.className = "token-textarea";
- tokenTextAreaEl.value = token;
- overlayBodyEl.appendChild(tokenTextAreaEl);
-
- const tokenWarningEl = document.createElement("p");
- tokenWarningEl.innerText =
- "Be careful with your token! It can be used to authenticate as you with the Rust Companion API.";
- tokenWarningEl.className = "token-warning";
- overlayBodyEl.appendChild(tokenWarningEl);
-
- const overlayButtonsEl = document.createElement("div");
- overlayButtonsEl.className = "overlay-buttons";
- overlayContainerEl.appendChild(overlayButtonsEl);
-
- const registerFCMButtonEl = document.createElement("button");
- registerFCMButtonEl.className = "button is-primary";
- registerFCMButtonEl.innerHTML = "Register to FCM";
-
- registerFCMButtonEl.addEventListener("click", () => {
- console.debug("Registering device to FCM");
- registerFCMButtonEl.disabled = true;
- registerFCMButtonEl.innerHTML = "Registering...";
- chrome.runtime
- .sendMessage({
- type: "REGISTER_FCM",
- token,
- })
- .then((response) => {
- console.debug("FCM registration response:", response);
- registerFCMButtonEl.disabled = false;
- registerFCMButtonEl.innerHTML = "Register to FCM";
- });
- });
-
- overlayButtonsEl.appendChild(registerFCMButtonEl);
-
- const tokenCopyButtonEl = document.createElement("button");
- tokenCopyButtonEl.className = "button is-secondary";
- tokenCopyButtonEl.innerHTML = "Copy to clipboard";
-
- let timeoutId: ReturnType | null = null;
- tokenCopyButtonEl.addEventListener("click", () => {
- console.debug("Copying token to clipboard");
- tokenCopyButtonEl.innerHTML = "Copied!";
- copyTokenToClipboard(token);
-
- if (timeoutId !== null) {
- clearTimeout(timeoutId);
- }
-
- timeoutId = setTimeout(() => {
- tokenCopyButtonEl.innerHTML = "Copy to clipboard";
- }, 3 * 1000);
- });
-
- overlayButtonsEl.appendChild(tokenCopyButtonEl);
+ const overlayContainerEl = document.querySelector(
+ ".page-wrapper .overlay-container",
+ )
+ if (!overlayContainerEl) {
+ console.error("Failed to find overlay container element")
+ return
+ }
+
+ const overlayBodyEl = overlayContainerEl.querySelector(".overlay-body")
+ if (!overlayBodyEl) {
+ console.error("Failed to find overlay body element")
+ return
+ }
+
+ const tokenTitleEl = document.createElement("h2")
+ tokenTitleEl.innerText = "Your token:"
+ tokenTitleEl.className = "token-title"
+ overlayBodyEl.appendChild(tokenTitleEl)
+
+ const tokenTextAreaEl = document.createElement("textarea")
+ tokenTextAreaEl.readOnly = true
+ tokenTextAreaEl.className = "token-textarea"
+ tokenTextAreaEl.value = token
+ overlayBodyEl.appendChild(tokenTextAreaEl)
+
+ const tokenWarningEl = document.createElement("p")
+ tokenWarningEl.innerText =
+ "Be careful with your token! It can be used to authenticate as you with the Rust Companion API."
+ tokenWarningEl.className = "token-warning"
+ overlayBodyEl.appendChild(tokenWarningEl)
+
+ const overlayButtonsEl = document.createElement("div")
+ overlayButtonsEl.className = "overlay-buttons"
+ overlayContainerEl.appendChild(overlayButtonsEl)
+
+ const tokenCopyButtonEl = document.createElement("button")
+ tokenCopyButtonEl.className = "button is-primary"
+ tokenCopyButtonEl.innerHTML = "Copy to clipboard"
+
+ let timeoutId: ReturnType | null = null
+ tokenCopyButtonEl.addEventListener("click", () => {
+ console.debug("Copying token to clipboard")
+ tokenCopyButtonEl.innerHTML = "Copied!"
+ copyTokenToClipboard(token)
+
+ if (timeoutId !== null) {
+ clearTimeout(timeoutId)
+ }
+
+ timeoutId = setTimeout(() => {
+ tokenCopyButtonEl.innerHTML = "Copy to clipboard"
+ }, 3 * 1000)
+ })
+
+ overlayButtonsEl.appendChild(tokenCopyButtonEl)
}
window.addEventListener(
- "message",
- (event) => {
- if (event.data.rustCompanionAuth) {
- console.debug("Propagating rust companion authentication data");
- chrome.runtime.sendMessage(event.data);
-
- const token = event.data.rustCompanionAuth.token;
-
- const overlayContainerEl = document.querySelector(
- ".page-wrapper .overlay-container",
- );
- if (!overlayContainerEl) {
- console.error("Failed to find overlay container element");
- return;
- }
-
- const overlayBodyEl = overlayContainerEl.querySelector(".overlay-body");
- if (!overlayBodyEl) {
- console.error("Failed to find overlay body element");
- return;
- }
-
- const tokenTitleEl = document.createElement("h2");
- tokenTitleEl.innerText = "Your token:";
- tokenTitleEl.className = "token-title";
- overlayBodyEl.appendChild(tokenTitleEl);
-
- const tokenTextAreaEl = document.createElement("textarea");
- tokenTextAreaEl.readOnly = true;
- tokenTextAreaEl.className = "token-textarea";
- tokenTextAreaEl.value = token;
- overlayBodyEl.appendChild(tokenTextAreaEl);
-
- const tokenWarningEl = document.createElement("p");
- tokenWarningEl.innerText =
- "Be careful with your token! It can be used to authenticate as you with the Rust Companion API.";
- tokenWarningEl.className = "token-warning";
- overlayBodyEl.appendChild(tokenWarningEl);
-
- const overlayButtonsEl = document.createElement("div");
- overlayButtonsEl.className = "overlay-buttons";
- overlayContainerEl.appendChild(overlayButtonsEl);
-
- const tokenCopyButtonEl = document.createElement("button");
- tokenCopyButtonEl.className = "button is-primary";
- tokenCopyButtonEl.innerHTML = "Copy to clipboard";
- overlayButtonsEl.appendChild(tokenCopyButtonEl);
-
- let timeoutId = null as NodeJS.Timeout | null;
- tokenCopyButtonEl.addEventListener("click", () => {
- navigator.clipboard
- .writeText(token)
- .then(() => {
- console.debug("Copied token to clipboard");
- tokenCopyButtonEl.innerHTML = "Copied!";
-
- if (timeoutId !== null) {
- clearTimeout(timeoutId);
- }
-
- timeoutId = setTimeout(() => {
- tokenCopyButtonEl.innerHTML = "Copy to clipboard";
- }, 3 * 1000);
- })
- .catch((error) => {
- console.error("Failed to copy token to clipboard:", error);
- });
- });
- }
- },
- false,
-);
+ "message",
+ (event: MessageEvent) => {
+ if (event.data?.rustCompanionAuth) {
+ handleRustCompanionAuthMessage(
+ event.data.rustCompanionAuth as RustCompanionAuthData,
+ )
+ }
+ },
+ false,
+)