IdlePixel Chat History

Adds the previous 5 chat messages when you log in

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         IdlePixel Chat History
// @namespace    lbtechnology.info
// @version      1.1.0
// @description  Adds the previous 5 chat messages when you log in
// @author       Lux-Ferre
// @license      MIT
// @match        *://idle-pixel.com/login/play*
// @grant        none
// @require      https://greasyfork.org/scripts/441206-idlepixel/code/IdlePixel+.js?anticache=20220905
// ==/UserScript==

(function() {
    'use strict';

    class ChatHistoryPlugin extends IdlePixelPlusPlugin {
        constructor() {
            super("chathist", {
                about: {
                    name: GM_info.script.name,
                    version: GM_info.script.version,
                    author: GM_info.script.author,
                    description: GM_info.script.description
                }
            });
            this.previous = "";
            this.tradeHistStream = [];
        }

        onLogin(){
            this.tradeHistStream = []
            
            const content = `chathist:logon:hello`
            const payload = {
                content: content, 
                onResponse: function(player, content, callbackId) {
                    return true;
                    },
                onOffline: function(player, content) {
                    console.log(content)
                },
                timeout: 2000 // callback expires after 2 seconds
            }
            IdlePixelPlus.sendCustomMessage("luxbot", payload)
        }
    
        onCustomMessageReceived(player, content, callbackId){
            const customData = this.parseCustom(player, content, callbackId)
            if (!(customData.plugin === "chathist" || customData.anwinFormatted)){
                return
            }
            if (customData.player === "luxbot"){
                if (customData.command === "addMessage"){
                    this.tradeHistStream.push(customData.payload)
                } else if (customData.command === "endstream"){
                    this.addToChat(this.tradeHistStream)
                }
            }
        }
    
        parseCustom(player, content, callbackId){
            const customData = {
                player: player,
                callbackId: callbackId,
                anwinFormatted: false
            }
            const splitPayload = content.split(":")
            if(splitPayload.length >= 3){
                customData.anwinFormatted = true
                customData.plugin = splitPayload[0]
                customData.command = splitPayload[1]
                customData.payload = splitPayload.slice(2).join(":")
            } else {
                customData.anwinFormatted = false
                customData.plugin = "unknown"
                customData.command = "unknown"
                customData.payload = content
            }
    
            return customData

        }
    
        addToChat(chatHistList){
            chatHistList.forEach(message => {
                Chat.add_to_chat_box(message)
                if ("chatlinks" in IdlePixelPlus.plugins){
                    IdlePixelPlus.plugins['chatlinks'].onChat("")
                }
            })
        }
    }

    const plugin = new ChatHistoryPlugin();
    IdlePixelPlus.registerPlugin(plugin);
    
})();