Fedi.CrowdedGames.Group/JS/setting.js
CatAClock 89e2c54d3e Bug fixes (#10)
Another somewhat overhaul & fixing a couple of stupid bugs that just so happen to crop up.

Reviewed-on: #10
2025-05-03 22:22:40 +00:00

103 lines
3 KiB
JavaScript

import * as MastodonAPI from "./MastodonAPI.js";
import * as BlueskyAPI from "./BlueskyAPI.js";
import * as TumblrAPI from "./TumblrAPI.js";
import * as Cookie from "./Cookies.js";
// Settings buttons
let LocalButton = document.getElementsByClassName("Local")[0];
let RemoteButton = document.getElementsByClassName("Remote")[0];
let MastodonLoginButton = document.getElementsByClassName("Login Mastodon")[0];
let MastodonWebInput = document.getElementsByClassName("WebInput Mastodon")[0];
let MastodonLogoutButton = document.getElementsByClassName("Logout Mastodon")[0];
let BlueskyLoginButton = document.getElementsByClassName("Login Bluesky")[0];
let BlueskyWebInput = document.getElementsByClassName("WebInput Bluesky")[0];
let BlueskyLogoutButton = document.getElementsByClassName("Logout Bluesky")[0];
// original link
let Origin = location.origin + "/HTML/setting.html"
// Change weather the timelines are public or remote
LocalButton.onclick = (event) => {
// Toggle the cookie
if (Cookie.IsCookieReal("Local")) {
Cookie.ExpireCookie("Local");
} else {
Cookie.InputCookie("Local", "true");
}
}
RemoteButton.onclick = (event) => {
// Toggle the cookie
if (Cookie.IsCookieReal("Romote")) {
Cookie.ExpireCookie("Remote");
} else {
Cookie.InputCookie("Remote", "true");
}
}
// Mastodon buttons
// Login
MastodonLoginButton.onclick = (event) => {
if (MastodonWebInput.value != "") {
let text = MastodonWebInput.value
MastodonAPI.HandleAuthentication(text);
}
}
// Logout
MastodonLogoutButton.onclick = (event) => {
Cookie.ExpireCookie(Cookie.MastodonClientID);
Cookie.ExpireCookie(Cookie.MastodonClientSecret);
Cookie.ExpireCookie(Cookie.MastodonAccessToken);
Cookie.ExpireCookie(Cookie.MastodonTokenType);
document.location.href = Origin;
}
// Bluesky Buttons
// Login
BlueskyLoginButton.onclick = (event) => {
if (BlueskyWebInput.value != "") {
BlueskyAPI.HandleAuthorization();
}
}
// Logout
BlueskyLogoutButton.onclick = (event) => {
Cookie.ExpireCookie(Cookie.BlueskyPKCEVerifier);
Cookie.ExpireCookie(Cookie.BlueskyPKCEChallenge);
Cookie.ExpireCookie(Cookie.BlueskyNonce);
Cookie.ExpireCookie(Cookie.BlueskyAccessToken);
Cookie.ExpireCookie(Cookie.BlueskyRefreshToken);
Cookie.ExpireCookie(Cookie.BlueskyPublicKey);
Cookie.ExpireCookie(Cookie.BlueskyPrivateKey);
document.location.href = Origin;
}
// if an access token is found, login.
async function CheckLogin() {
// Check for a mastodon token.
if (Cookie.IsCookieReal(Cookie.MastodonAccessToken)) {
// Swap the buttons
MastodonLoginButton.remove();
MastodonWebInput.remove();
MastodonLogoutButton.setAttribute("style", "");
} else {
// Auto log in
await MastodonAPI.GainToken(Cookie.MastodonWebsite);
}
// Check for a bluesky token.
if (Cookie.IsCookieReal(Cookie.BlueskyAccessToken)) {
// Swap the buttons
BlueskyLoginButton.remove();
BlueskyWebInput.remove();
BlueskyLogoutButton.setAttribute("style", "");
} else {
// Auto log in
await BlueskyAPI.GainTokens();
}
}
// Runs on website start.
// Remove traces of "login".
CheckLogin();