Sign up/Login Library

A library for securely storing and retrieving user credentials from JSONBin

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/470767/1219648/Sign%20upLogin%20%20Library.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Sign up/Login  Library
// @namespace    http://tampermonkey.net/
// @version      1
// @description  A library for securely storing and retrieving user credentials from JSONBin
// @author       longkidkoolstar
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// ==/UserScript==

const binId = 'YOUR_BIN_ID_HERE';
const accessKey = 'YOUR_ACCESS_KEY_HERE';

class LoginLibrary {
  constructor() {
    // Check if user is logged in
    if (this.isLoggedIn()) {
      console.log('User is logged in');
      this.retrieveUserCredentials();
    } else {
      console.log('User is not logged in');
      this.promptUserCredentials();
    }
  }

  isLoggedIn() {
    return GM_getValue('username') && GM_getValue('password');
  }

  promptUserCredentials() {
    // Prompt user to enter username and password
    const username = prompt('Enter your username:');
    const password = prompt('Enter your password:');
    // Save username and password to Tampermonkey
    GM_setValue('username', username);
    GM_setValue('password', password);
    // Save username and password to JSONBin
    this.saveUserCredentialsToJSONBin(username, password);
  }

  retrieveUserCredentials() {
    const username = GM_getValue('username');
    const password = GM_getValue('password');
    console.log(`Username: ${username}, Password: ${password}`);
  }

  saveUserCredentialsToJSONBin(username, password) {
    GM_xmlhttpRequest({
      method: 'GET',
      url: `https://api.jsonbin.io/v3/b/${binId}`,
      headers: {
        'X-Bin-Access-Token': accessKey,
        'X-Access-Key': accessKey
      },
      onload: function (response) {
        const existingData = JSON.parse(response.responseText).record;
        let isUsernameFound = false;
        let isPasswordCorrect = false;
        let isUsernameTaken = false;
        for (const user in existingData.users) {
          if (existingData.users.hasOwnProperty(user)) {
            if (existingData.users[user].username === username) {
              isUsernameFound = true;
              if (existingData.users[user].password === password) {
                isPasswordCorrect = true;
              }
              break;
            }
          }
        }
        if (isUsernameFound && isPasswordCorrect) {
          console.log('User logged in successfully.');
          // Save username and password to Tampermonkey
          GM_setValue('username', username);
          GM_setValue('password', password);
        } else if (!isUsernameFound || !isPasswordCorrect) {
          for (const user in existingData.users) {
            if (existingData.users.hasOwnProperty(user)) {
              if (existingData.users[user].username === username) {
                isUsernameTaken = true;
                break;
              }
            }
          }
          if (!isUsernameTaken) {
            const userData = {
              username: username,
              password: password
            };
            const mergedData = {
              ...existingData,
              users: {
                ...existingData.users,
                [username]: userData
              }
            };
            GM_xmlhttpRequest({
              method: 'PUT',
              url: `https://api.jsonbin.io/v3/b/${binId}`,
              headers: {
                'Content-Type': 'application/json',
                'X-Bin-Access-Token': accessKey,
                'X-Access-Key': accessKey
              },
              data: JSON.stringify(mergedData),
              onload: function (response) {
                console.log('User registered successfully.');
              }
            });
          } else {
            const newUsername = prompt('That username is already taken. Please enter a new username:');
            this.saveUserCredentialsToJSONBin(newUsername, password);
          }
        }
      }
    });
  }
}

new LoginLibrary();