Save unposted text in reply area

Save unposted text in the reply area before changing pages

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Save unposted text in reply area
// @author       Joshh
// @namespace    https://tljoshh.com
// @version      0.1.1
// @description  Save unposted text in the reply area before changing pages
// @match        *://*.websight.blue/thread/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=websight.blue
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const replyForm = document.querySelector('#reply-form');
    const replyBox = document.querySelector('#reply-content');
    const localStorageKey = 'unpostedMessages';
    const setMessage = (threadId, text) => {
        const messages = localStorage.getItem(localStorageKey);
        if (messages === null) {
            const obj = {};
            obj[threadId] = text;
            localStorage.setItem(localStorageKey, JSON.stringify(obj));
        }
        else {
            const obj = JSON.parse(messages);
            obj[threadId] = text;
            localStorage.setItem(localStorageKey, JSON.stringify(obj));
        }
    }
    const getMessage = threadId => {
        const messages = localStorage.getItem(localStorageKey);
        let message = null;
        if (messages !== null) {
            const obj = JSON.parse(messages);
            message = obj[threadId];
        }
        return message;
    }
    const removeMessage = (threadId) => {
        const messages = localStorage.getItem(localStorageKey);
        if(messages !== null) {
            const obj = JSON.parse(messages);
            console.log(obj);
            delete obj[threadId];
            console.log(obj);
            localStorage.setItem(localStorageKey, JSON.stringify(obj));
        }
    }
    const hasMessage = threadId => {
        const message = getMessage(threadId);
        let messageExists = false;
        if (typeof message !== 'undefined') {
            messageExists = true;
        }
        return messageExists;
    }
    const hasMessages = () => {
        const messages = localStorage.getItem(localStorageKey);
        let messagesExist = false;
        if(messages !== null) {
            const obj = JSON.parse(messages);
            if(Object.keys(obj).length) {
                messagesExist = true;
            }
        }
        return messagesExist;
    }
    const getThreadId = () => {
        const regex = new RegExp(/https:\/\/(.*)\.websight\.blue\/thread\/([\d]+)\/(.*)/);
        const matchGroups = window.location.href.match(regex);
        return matchGroups[2];
    }
    const loadMessage = () => {
        const threadId = getThreadId();
        const messageExists = hasMessage(threadId);
        if(messageExists) {
            const message = getMessage(threadId);
            replyBox.value = message;
        }
    }

    // Check for an unposted message in the reply box and save it if one exists
    window.addEventListener('beforeunload', (event) => {
        const value = replyBox.value;
        const notEmpty = value !== "";
        const threadId = getThreadId();
        if(notEmpty) {
            // Add the unposted message
            const threadId = getThreadId();
            setMessage(threadId, value);
        } else if(!notEmpty && hasMessage(threadId)) {
            // If there was a saved unposted message and it's been cleared from reply box, remove it from storage
            removeMessage(threadId);
        }
    });

    // Remove any previously saved messages in the thread when the reply box is submitted
    replyForm.addEventListener('submit', (event) => {
        const threadId = getThreadId();
        const hasMessage = hasMessage(threadId);
        if(hasMessage) {
            removeMessage(threadId);
        }
    });

    // Check and load any previously saved messages
    loadMessage();
})();