downthecrop / betterdgg

Better Better Destiny.gg // Make Destiny.gg Great Again!
http://downthecrop.xyz/bbdgg
Other
2 stars 3 forks source link

Sweetie's Name Colors #80

Closed downthecrop closed 7 years ago

downthecrop commented 7 years ago

Yo I'm going to be hanging out with family over the next couple weeks but I'm posting this here if anyone wants to add this in. Sweetie said he never got around to a decent localStorage JSON format for each of the personal settings. But it should be pretty easy.

// ==UserScript==
// @name        DestinyGG Color Tags
// @namespace   YEE
// @description Custom User Colors In Destiny.gg
// @include     *://www.destiny.gg/embed/chat
// @version     1
// @grant       none
// ==/UserScript==
/* jshint esversion: 6 */
(function() {
    var Main, CSS, Commands
    var pushChat, pushError, settings

    pushChat = function(string) {
        destiny.chat.gui.push(new ChatInfoMessage(string))
    }
    pushError = function(string) {
        destiny.chat.gui.push(new ChatErrorMessage(string))
    }
    settings = (function() {
        var _observers, _notify

        _observers = []
        _notify = function(key, value) {
            for (let i = 0; i < _observers.length; i++)
                _observers[i].call(this, key, value)
        }
        return {
            addObserver: function(obs) {
                if (_observers.indexOf(obs) < 0)
                    _observers.push(obs)
            },
            removeObserver: function(obs) {
                let i = _observers.indexOf(obs)
                if (i === -1)
                    return false
                _observers.splice(i, 1)
                return true
            },
            put: function(key, value) {
                localStorage.setItem(key, value)
                _notify(key, value)
            },
            get: function(key, defValue) {
                let value = localStorage.getItem(key)
                if (value === null) {
                    value = defValue
                    settings.put(key, defValue)
                }
                return value
            }
        }
    })();

    Main = {
        init: function() {
            Commands.init()
            CSS.init()
        }
    }

    CSS = {
        colorTemplate: '{ color: {}; }',
        userTemplate: '.user-msg[data-username="{}"]>.user',
        style: undefined,
        init: function() {
            this.style = document.createElement('style')
            this.style.type = 'text/css'
            window.document.head.appendChild(this.style)
            if (!settings.get('taggedUsers')) {
                settings.put('taggedUsers', '')
                settings.put('taggedColors', '')
            }
            settings.addObserver((key, val) => {
                if (key === 'taggedColors') this.tagUpdate(settings.get('taggedUsers'), val)
            })
            CSS.tagUpdate(settings.get('taggedUsers'), settings.get('taggedColors'))
        },
        tagUpdate: function(users, colors) {
            let res = ''
            users = users.toLowerCase().split(' ').join('').split(',')
            colors = colors.toLowerCase().split(' ').join('').split(',')
            for (let i = 0; i < users.length; i++) {
                res += this.userTemplate.replace('{}', users[i]) + this.colorTemplate.replace('{}', colors[i])
            }
            if (this.style.styleSheet)
                this.style.styleSheet.cssText = res
            else {
                this.style.innerHTML = ''
                this.style.appendChild(document.createTextNode(res))
            }
        }
    }

    Commands = {
        targets: [],
        colors: [],
        fnHandleCommand: undefined,
        init: function() {
            Commands.injectTag()
        },
        injectTag: function() {
            this.fnHandleCommand = destiny.chat.handleCommand
            destiny.chat.handleCommand = function(string) {
                var match
                string = string.trim()
                if (match = string.match(/^(tag)\s(\S+)\s(\S+)/)) {
                    Commands.targets.push(match[2])
                    Commands.colors.push(match[3])
                    Commands.tagUpdate()
                } else if (match = string.match(/^(untag)\s(\S+)$/)) {
                    let idx = Commands.targets.indexOf(match[2])
                    if (idx !== -1) {
                        Commands.targets.splice(idx, 1)
                        Commands.colors.splice(idx, 1)
                    }
                    Commands.tagUpdate()
                } else if (match = string.match(/^tag\s(\w+)$/)) {
                    pushError('Not Implemented Yet: Will display color of tagged user')
                } else if (match = string.match(/^tag$/)) {
                    pushChat('Not Implemented Yet: Will display all users tagged')
                } else {
                    Commands.fnHandleCommand.apply(this, arguments)
                }
            }
        },
        tagUpdate: function() {
            let taggedUsers = this.targets.join(','),
                taggedColors = this.colors.join(',')
            settings.put('taggedUsers', taggedUsers)
            settings.put('taggedColors', taggedColors)
            pushChat('Your taglist has been updated')
        }
    }
    Main.init()
}).call(this)
PinkishCow commented 7 years ago

Ill do it when i wake up tonight

9inevolt commented 7 years ago

👍 Seems like a good idea. I think some people asked me for this before.

One thing you might consider is create a single structure of the users and colors like

{
  "downthecrop": {
     "color": "red"
  },
  "purplecow": {
    "color": "white"
  }
}

And just use JSON.stringify/parse to store the entire thing in localStorage whenever you need to. This would let you get rid of the parallel arrays and comma splicing.