forked from Inve1951/BetterDiscordStuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AvatarHover.plugin.coffee
163 lines (143 loc) · 5.85 KB
/
AvatarHover.plugin.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#META { "name": "AvatarHover", "website": "https://inve1951.github.io/BetterDiscordStuff/" } *//
class global.AvatarHover
getName: -> "Avatar Hover"
getDescription: -> "When hovering, resize the avatar. Use Ctrl / Ctrl+Shift."
getAuthor: -> "noVaLue, square"
getVersion: -> "0.6.0"
hoverCard = AsyncKeystate = null
load: ->
window.SuperSecretSquareStuff ?= new Promise (c, r) ->
require("request").get "https://raw.githubusercontent.com/Inve1951/BetterDiscordStuff/master/plugins/0circle.plugin.js", (err, res, body) ->
return r err ? res if err or 200 isnt res?.statusCode
Object.defineProperties window.SuperSecretSquareStuff, {libLoaded: value: c; code: value: body}
`(0,eval)(body)`
start: ->
{AsyncKeystate, createElement} = await SuperSecretSquareStuff
getSettings()
updateQualifier()
BdApi.injectCSS "css-AvatarHover", css
hoverCard = createElement "div", id: "AvatarHover"
document.addEventListener "keydown", handleKeyUpDown, yes
document.addEventListener "keyup", handleKeyUpDown, yes
document.addEventListener "mouseover", handleMouseOverOut, yes
document.addEventListener "mouseout", handleMouseOverOut, yes
window.addEventListener "blur", handleFocusLoss, yes
stop: ->
document.removeEventListener "keydown", handleKeyUpDown, yes
document.removeEventListener "keyup", handleKeyUpDown, yes
document.removeEventListener "mouseover", handleMouseOverOut, yes
document.removeEventListener "mouseout", handleMouseOverOut, yes
window.removeEventListener "blur", handleFocusLoss, yes
hoverCard.remove()
BdApi.clearCSS "css-AvatarHover"
qualifier = null
updateQualifier = ->
qualifier = [
".icon-3o6xvg" if settings.isHoverGuilds
".avatarDefault-35WC3R, .avatarSpeaking-1wJCNq,
.channel .avatar-small" if settings.isHoverChannels
"#friends .avatar-small,
.activityFeed-28jde9 .image-33JSyf" if settings.isHoverFriends
".message-1PNnaP .image-33JSyf, .embedAuthorIcon--1zR3L" if settings.isHoverChatMessages
".membersWrap-2h-GB4 .image-33JSyf" if settings.isHoverChatUsers
].filter((s) -> s?).join ", "
handleKeyUpDown = ({key}) ->
return unless key in ["Control", "Shift"]
updateHoverCard()
handleFocusLoss = ->
updateHoverCard()
handleMouseOverOut = ({type, target}) ->
return unless target.matches qualifier
updateHoverCard "mouseover" is type and target
lastTarget = null
updateHoverCard = (target = lastTarget) ->
lastTarget = target
isShown = settings.isShown or AsyncKeystate.key "Control"
isLarge = settings.isLarge or AsyncKeystate.key "Shift"
return hoverCard.remove() unless isShown and target
size = isLarge and 256 or 128
boundsTarget = target.getBoundingClientRect()
boundsWindow =
width: window.innerWidth
height: window.innerHeight
left = Math.max 0, boundsTarget.left + (boundsTarget.width - size) / 2
left = boundsWindow.width - size if left + size > boundsWindow.width
top =
if size > boundsWindow.height
(boundsWindow.height - size) / 2
else if boundsTarget.bottom + size > boundsWindow.height
boundsTarget.top - size
else boundsTarget.bottom
hoverCard.style[k] = v for k, v of {
backgroundColor: settings.avatarBackgroundColor
backgroundImage: ("IMG" is target.tagName and target.src or getComputedStyle(target).backgroundImage).replace /\?size=\d{3,4}/, "?size=#{size}"
borderColor: settings.avatarBorderColor
borderRadius: settings.avatarBorderRadius
borderWidth: settings.avatarBorderSize
width: "#{size}px"
height: "#{size}px"
top: "#{top}px"
left: "#{left}px"
}
return hoverCard.remove() if "none" is hoverCard.style.backgroundImage
document.body.appendChild hoverCard
defaultSettings =
avatarBackgroundColor: "#303336"
avatarBorderRadius: "4px"
avatarBorderSize: "1px"
avatarBorderColor: "black"
isShown: no
isLarge: no
isHoverGuilds: no
isHoverChannels: yes
isHoverFriends: yes
isHoverChatMessages: yes
isHoverChatUsers: yes
settings = null
getSettings = ->
return if settings?
settings = bdPluginStorage.get("AvatarHover", "settings") ? {}
settings[k] ?= v for k, v of defaultSettings
@updateSettings: ->
for {name, type, value, checked} in document.querySelectorAll "#settings_AvatarHover input"
settings[name] = if "checkbox" is type then checked else value or defaultSettings[name]
bdPluginStorage.set "AvatarHover", "settings", settings
updateQualifier()
getSettingsPanel: ->
getSettings()
"""<div id="settings_AvatarHover">#{
(makeInput k, v for k, v of {
avatarBackgroundColor: "Background color"
avatarBorderRadius: "Border radius"
avatarBorderSize: "Border size"
avatarBorderColor: "Border color"
spacer: null
isShown: "Force-show avatar"
isLarge: "Force large avatar"
isHoverGuilds: "Guilds"
isHoverChannels: "Channels / DM users"
isHoverFriends: "Friends list"
isHoverChatMessages: "Chat messages"
isHoverChatUsers: "Chat users"
}).join ""
}</div>"""
makeInput = (name, label) ->
return "<br/>" unless label?
type = Boolean is defaultSettings[name].constructor and (isCheckbox = true) and "checkbox" or "text"
_default = if isCheckbox
if settings[name] then "checked" else ""
else """placeholder="#{defaultSettings[name]}" value="#{settings[name]}\""""
"""<label><input type="#{type}" name="#{name}" #{_default} onChange="AvatarHover.updateSettings()"/> #{label}</label><br/>"""
css = """
#AvatarHover {
background-size: cover;
border-style: solid;
display: block;
pointer-events: none;
position: fixed;
z-index: 99999;
}
#settings_AvatarHover {
color: #87909C;
}
"""