forked from alecmev/trimless-gmail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
182 lines (158 loc) · 5.27 KB
/
Copy pathcontentScript.js
File metadata and controls
182 lines (158 loc) · 5.27 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
let isEnabled;
// Initialize extension state
chrome.storage.local.get(null).then(items => {
isEnabled = items['trimless-enabled'];
});
const untrimTimer = new (function() {
this.again = 0;
this.isTicking = false;
this.more = function() {
if (!isEnabled) {
ununtrim();
return;
}
if (untrimTimer.again < 4) {
++untrimTimer.again;
}
if (!untrimTimer.isTicking) {
untrimTimer.again = 2;
untrimTimer.isTicking = true;
untrimTimer.stuff();
}
}
this.stuff = function() {
if (!isEnabled) {
ununtrim();
untrimTimer.again = 0;
untrimTimer.isTicking = false;
return;
}
if (untrimTimer.again) {
--untrimTimer.again;
setTimeout(() => untrimTimer.stuff(), 1000);
}
else {
untrimTimer.isTicking = false;
}
untrim();
}
})();
let untrimReplies = false;
async function applyOptions() {
const options = await chrome.storage.sync.get(null);
applyOptionsInterface(options);
untrimReplies = options['trimless-reply-enabled'];
untrimTimer.more();
}
function untrim() {
const ad = function(what) {
const tmpad = $(this);
if (!tmpad.text().trim().length) {
tmpad.hide().removeClass(what).addClass('trimless-' + what);
}
};
// "View entire message"
$(".iX > a").each(function() {
const tmpvem = $(this);
$.get(this.href, function(data) {
tmpvem.parents().eq(1).html($('font[size=-1]', data).last().html());
});
});
applyOptions();
$('.adP').removeClass('adP').addClass('trimless-adP');
$('.adO').removeClass('adO').addClass('trimless-adO');
$('.adL > .im, .adL.im').add(
$('.h5').removeClass('h5').addClass('im').addClass('trimless-h5')
).addClass('trimless-content');
$('.ajU, .ajV, .adm').hide().addClass('trimless-button');
$('.adL').each(function() { ad.apply(this, ['adL']); });
$('.adM').each(function() { ad.apply(this, ['adM']); });
if (untrimReplies) {
// Otherwise the main textarea steals the focus
$('.ajR[style="user-select: none;"]').click(function(e) {
e.stopPropagation();
});
// Harder to undo, since this part isn't read-only
$('.ajR[style="user-select: none;"] > .uC').click();
}
const tmpah1 = $('.et .aH1');
if (tmpah1.is(':visible')) {
tmpah1.click();
const tmpextra = $('.editable > .gmail_extra');
if (!tmpextra.prev('br').length) {
tmpextra.prepend('<br />');
}
}
}
function ununtrim() {
const ad = function(what) {
const tmpad = $(this);
if (!tmpad.text().trim().length) {
tmpad.removeClass('trimless-' + what).addClass(what).show();
}
}
$('.trimless-adM').each(function() { ad.apply(this, ['adM']); });
$('.trimless-adL').each(function() { ad.apply(this, ['adL']); });
$('.trimless-button').removeClass('trimless-button').show();
$('.trimless-content').removeClass('trimless-content');
$('.trimless-h5').removeClass('trimless-h5')
.removeClass('im').addClass('h5');
$('.trimless-adO').removeClass('trimless-adO').addClass('adO');
$('.trimless-adP').removeClass('trimless-adP').addClass('adP');
}
function untrimOnClick(event) {
if (isEnabled && !$(event.target).is('.aH1')) {
untrimTimer.more();
}
}
function applyOptionsInterface(options) {
if (!document.getElementById('trimless-style')) {
$('head').append('<style id="trimless-style"></style>');
}
let trimlessStyle = '';
if (options['trimless-color-enabled']) {
trimlessStyle +=
'.trimless-content, .trimless-content * {' +
'color: ' + options['trimless-color-value'] +
' !important;' +
'border-color: ' + options['trimless-color-border'] +
' !important;' +
'}';
}
if (options['trimless-indentation-enabled']) {
trimlessStyle +=
'.trimless-content {' +
'padding-left: ' + options['trimless-indentation-value'] +
'px !important;' +
'}';
}
$('#trimless-style').html(trimlessStyle);
}
// Initialize
untrimTimer.more();
$(window).on('hashchange', untrimTimer.more);
$(document).on('click', untrimOnClick);
$(window).on('load', untrimTimer.more);
$(applyOptions);
document.addEventListener('visibilitychange', untrimTimer.more);
// Handle extension messages
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
sendResponse({ trimless: true });
return true;
});
// Handle storage changes
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === 'sync') {
applyOptions();
return;
}
if (changes['trimless-enabled']) {
isEnabled = changes['trimless-enabled'].newValue;
chrome.runtime.sendMessage(isEnabled);
if (isEnabled) {
untrimTimer.more();
} else {
ununtrim();
}
}
});