Steam OpenID auto approval

Adds a checkbox to remember OpenID logins

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Steam OpenID auto approval
// @namespace   w8v1khnnd8e1s7ef
// @description Adds a checkbox to remember OpenID logins
// @match       https://steamcommunity.com/openid/login
// @grant       GM.setValue
// @grant       GM.getValue
// @version     1.1
// @run-at      document-end
// @inject-into content
// @license     MIT
// ==/UserScript==

(async function () {
	"use strict";

	const openidForm = document.getElementById("openidForm");
	const loginForm = document.getElementById("loginForm");
	const errors = document.getElementById("error_display");
	
	// Abort if we're logged out or errors are showing
	if (!openidForm || loginForm || errors?.offsetHeight) {
		return;
	}

	let prefKey;
	{
		let realm;
		try {
			realm = JSON.parse(atob(openidForm.elements.openidparams.value))["openid.realm"];
		} catch (ignore) {}
		if (!realm) {
			return;
		}

		const accountID = document.querySelector("[data-miniprofile]")?.dataset.miniprofile ?? "default";
		prefKey = `autologin_${accountID}_${realm}`;
	}


	const loginButton = openidForm.elements.imageLogin;

	if (await GM.getValue(prefKey)) {
		loginButton.type = "hidden";
		loginButton.after("Signing in...");
		openidForm.submit();
	} else {
		// Create elements:
		// " ☑ Automatically sign into this site"
		const label = document.createElement("label");
		const checkbox = document.createElement("input");

		checkbox.type = "checkbox";
		label.title = "Automatically accept sign-in requests from this site in the future."

		label.append(checkbox, " Automatically sign into this site");

		openidForm.addEventListener("submit", async () => {
			if (checkbox.checked) {
				await GM.setValue(prefKey, true);
			}
		});

		loginButton.after(" ", label);
	}
})();