InstaSynchP Input History

Plugin to browse your chat history

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        InstaSynchP Input History
// @namespace   InstaSynchP
// @description Plugin to browse your chat history
// @version     1.1.2
// @author      Zod-
// @source      https://github.com/Zod-/InstaSynchP-Input-History
// @license     MIT

// @include     *://instasync.com/r/*
// @include     *://*.instasync.com/r/*
// @grant       none
// @run-at      document-start

// @require     https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js?version=37716
// ==/UserScript==

function InputHistory(version) {
    "use strict";
    this.version = version;
    this.name = 'InstaSynchP Input History';
    this.history = [''];
    this.index = 0;
}

InputHistory.prototype = {
    get history() {
        return this._history;
    },
    set history(value) {
        this._history = value;
    },

    get index() {
        return this._index % this._history.length;
    },
    set index(value) {
        if (value < 0) {
            value = this._history.length - 1;
        }
        this._index = value;
        //disable autocomplete when browsing history
        if (window.plugins.autocomplete) {
            window.plugins.autocomplete.autocompleteEnabled = (this.index === 0);
        }
    }
};

InputHistory.prototype.executeOnce = function () {
    "use strict";
    var th = this;
    events.on(th, 'SendChat', function (message) {
        if (th.index !== 0) {
            //remove the string from the array
            th.history.splice(th.index, 1);
        }
        //add the string to the array at position 1
        th.history.splice(1, 0, message);
        th.index = 0;
    });

    function checkAutocomplete() {
        if (window.plugins.autocomplete && window.plugins.autocomplete.menuActive && th.index === 0) {
            return true;
        }
        return false;
    }

    events.on(th, 'InputKeydown[38]', function () {
        if (checkAutocomplete()) {
            return;
        }
        th.index += 1;
        th.writeHistory();
    });
    events.on(th, 'InputKeydown[40]', function () {
        if (checkAutocomplete()) {
            return;
        }
        th.index -= 1;
        th.writeHistory();
    });
};

InputHistory.prototype.writeHistory = function () {
    "use strict";
    var len = this.history[this.index].length;
    $('#cin').val(this.history[this.index]);
    $('#cin')[0].setSelectionRange(len, len);
};

InputHistory.prototype.resetVariables = function () {
    "use strict";
    this.index = 0;
};
window.plugins = window.plugins || {};
window.plugins.inputHistory = new InputHistory('1.1.2');