-
Notifications
You must be signed in to change notification settings - Fork 10
/
chat_add_maximize_button.user.js
145 lines (127 loc) · 4.79 KB
/
chat_add_maximize_button.user.js
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
// ==UserScript==
// @name Torn: Chats: Add maximize button
// @namespace lugburz.chats.add_maximize_button
// @version 0.2.1
// @description Add maximize button to all chat windows.
// @author Lugburz
// @match https://www.torn.com/*
// @grant GM_addStyle
// ==/UserScript==
// Number of pixels to increase chat window dimensions by.
var WIDTH = 600;
var HEIGHT = 200;
// Whether or not to allow only one maximized chat window.
// If set to true, all other chat window will be unmaximized.
var ONLY_ONE = true;
GM_addStyle(`
.maximize_ {
display: block;
width: 26px !important;
float: right;
height: 34px;
background: url(/images/v2/chat/tab_icons.svg);
background-position-x: -582px;
}
`);
function isBoxVisible(box) {
return $(box).find('div[class^=chat-box-content_]').size() > 0;
}
function isBoxMaximized(box) {
const title = $(box).find('div[class^=chat-box-title_]');
return $(title).find('div.maximize_').attr('maximized') > 0;
}
function unmaxAll() {
const boxes = $('#chatRoot').find('div[class^=chat-box_]');
$(boxes).each(function() {
if (isBoxVisible($(this)) && isBoxMaximized($(this))) {
maxUnmax($(this), false);
}
});
}
function maxUnmax(box, doMax) {
const content = $(box).find('div[class^=chat-box-content_]');
const textarea = $(box).find('textarea[class^=chat-box-textarea_]');
const title = $(box).find('div[class^=chat-box-title_]');
if (doMax) {
$(box).width($(box).width() + WIDTH);
$(content).width($(content).width() + WIDTH);
$(content).height($(content).height() + HEIGHT);
$(content).find('div[class^=viewport_]').height($(content).find('div[class^=viewport_]').height() + HEIGHT);
$(content).find('div[class^=viewport_]').css('max-height', $(content).find('div[class^=viewport_]').height() + HEIGHT);
$(textarea).width($(textarea).width() + WIDTH);
$(title).parent().width($(title).parent().width() + WIDTH);
$(title).parent().css("max-width", $(title).parent().width() + WIDTH);
$(title).find('div.maximize_').attr('maximized', 1);
$(title).find('div.maximize_').attr('title', 'Unmaximize chat');
} else {
$(box).css('width', 'auto');
$(content).width($(content).width() - WIDTH);
$(content).height($(content).height() - HEIGHT);
$(content).find('div[class^=viewport_]').height($(content).find('div[class^=viewport_]').height() - HEIGHT);
$(textarea).width($(textarea).width() - WIDTH);
$(title).parent().width($(title).parent().width() - WIDTH);
$(title).find('div.maximize_').attr('maximized', 0);
$(title).find('div.maximize_').attr('title', 'Maximize chat');
}
}
function addMaxButton(box, force=false) {
const title = $(box).find('div[class^=chat-box-title_]');
if ($(title).find('div.maximize_').size() == 0) {
$(title).append('<div class="maximize_" title="Maximize chat"></div>');
$(title).find('div.maximize_').on('click', function() {
event.stopPropagation();
if (isBoxMaximized(box)) {
maxUnmax($(box), false);
} else {
if (ONLY_ONE)
unmaxAll();
maxUnmax($(box), true);
}
});
if (force || isBoxVisible(box)) {
$(title).find('div.maximize_').show();
} else {
$(title).find('div.maximize_').hide();
}
}
}
function addOnClick(box) {
const title = $(box).find('div[class^=chat-box-title_]');
$(title).on('click', function() {
if (!isBoxVisible(box)) {
// chat is hidden -> showing
$(title).find('div.maximize_').show();
} else {
// chat is shown -> hiding
if (isBoxMaximized(box)) {
$(box).css("width", "auto");
$(title).find('div.maximize_').attr('maximized', 0);
$(title).find('div.maximize_').attr('title', 'Maximize chat');
}
$(title).find('div.maximize_').hide();
}
});
}
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if ($(node).find('div[class^=chat-box-title_]').size() > 0) {
addMaxButton(node, true);
addOnClick(node);
}
}
}
});
(function() {
'use strict';
// Your code here...
$('#chatRoot').ready(function() {
const boxes = $('#chatRoot').find('div[class^=chat-box_]');
$(boxes).each(function() {
addMaxButton($(this));
addOnClick($(this));
});
});
const wrapper = document.querySelector('#chatRoot')
observer.observe(wrapper, { subtree: true, childList: true })
})();