-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
286 lines (257 loc) · 8.18 KB
/
content-script.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const keybinds = true;
const user_wm_fix = true;
const navbar_fixes = true;
const modal_click_outside = true;
const second_keybinds = true;
const second_tooltips = true;
const favicon = true;
const inlineStyleProscribed = [
// Deprecated
];
const btnReplacePairs = {
// clear: `Clear ()`,
search: `Search (↩)`,
yes: `Yes (Y)`,
no: `No (N)`,
close: `Close (↩)`,
save: `Save (↩)`,
// 'save/print': `Save/Print ()`,
fine: `Fine (Y)`,
'waive fine': `Waive Fine (N)`,
};
const buttonSelector =
'*:is(.modal, #PN_EditDetails, #UP_HOSearch):not(#PN_InHouse) input:is([value="Search"], [value="Yes"], [value="No"], [value="Close"], [value="Save"], [value="Save/Print"], [value="Fine"], [value="Waive Fine"]), #UP_PatronSearch *:is([value="Search"])';
const linkOverrides = [
{
selector: '.topdiv > div:first-of-type > a',
link: 'https://evolve-library.infovisionsoftware.com/beavercounty/library/circulation.aspx',
},
{
selector: '.topdiv a#BT_Home',
link: 'https://evolve-library.infovisionsoftware.com/beavercounty/',
},
{
selector: '.topdiv a#BT_Reports',
link: 'https://evolve-library.infovisionsoftware.com/beavercounty/rmreports.aspx',
},
];
const helperFunctions = {
getVisibleFromArray(array) {
return array.flatMap((button, idx) =>
[...button.getClientRects()].length ? button : []
);
},
setFavicon(img) {
let headTitle = document.querySelector('head');
let favLink = document.createElement('link');
favLink.setAttribute('rel', 'shortcut icon');
favLink.setAttribute('href', img);
headTitle.appendChild(favLink);
},
removeInlineStyles(selectorsArray) {
selectorsArray.forEach((entry, idx) => {
let selector = entry?.selector || entry;
[...document.querySelectorAll(selector)].forEach((el, idx) => {
if (entry.removeProps) {
entry.removeProps.forEach((propName, idx) => {
el.style[propName] = '';
});
return;
}
el.style = '';
});
});
},
formSubmission(eventTarget, eventArgument = '') {
const form = document.getElementById('form1');
form.__EVENTTARGET.value = eventTarget;
form.__EVENTARGUMENT.value = eventArgument;
form.submit();
},
};
const buttonHandlers = {
escapeCurrentWindow() {
let allCloseButtons = [...document.querySelectorAll('.close')];
let visibleCloseButtons = allCloseButtons.flatMap((button, idx) =>
[...button.getClientRects()].length ? button : []
);
visibleCloseButtons.forEach((buttonElement, idx) => {
buttonElement.click();
});
},
handleModalBtn(key) {
helperFunctions
.getVisibleFromArray([
...document.querySelectorAll(
`.modal *[value="${btnReplacePairs[key]}"]`
),
])
.forEach((btn, i) => {
btn.click();
});
},
handleSecondaryMenuBtn(index) {
let btn = document.querySelector(`.user-menu *[smk-key="smk-${index}"]`);
helperFunctions.formSubmission(btn.id);
},
};
const Start = () => {
// ================
// FEATURE PRE REQS
// ================
const prereq_second_keybinds_tooltips = () => {
// Get all secondary navbar buttons
let navbarButtons = [
...document.querySelectorAll('#PN_Toolbar .user-menu a'),
];
navbarButtons.forEach((btn, idx) => {
if (idx > 8) return;
let customLabel = document.createElement('span');
customLabel.className = 'smk-customLabel';
customLabel.textContent = ` ${idx + 1}`;
customLabel.setAttribute('smk-key', `smk-${idx + 1}`);
btn.setAttribute('smk-key', `smk-${idx + 1}`);
btn.append(customLabel);
});
};
// ========
// FEATURES
// ========
const feature_keybinds = () => {
[...document.querySelectorAll(buttonSelector)].forEach((button, idx) => {
let temp = button.value.toString().toLowerCase();
let el = document.getElementById(button.id);
el.value = btnReplacePairs[temp];
});
document.body.addEventListener('keyup', (e) => {
switch (e.keyCode) {
case 27:
{
// Escape
buttonHandlers.escapeCurrentWindow();
}
break;
case 89:
{
// Y
buttonHandlers.handleModalBtn('fine');
buttonHandlers.handleModalBtn('yes');
}
break;
case 78:
{
// N
buttonHandlers.handleModalBtn('waive fine');
buttonHandlers.handleModalBtn('no');
}
break;
case 13:
{
// Enter
buttonHandlers.handleModalBtn('search');
buttonHandlers.handleModalBtn('close');
buttonHandlers.handleModalBtn('save');
}
break;
default:
{
}
break;
}
});
};
const feature_user_wm_fix = () => {
let userMenu = document?.querySelector('.userinfo') || false;
// To prevent infinite re-renders, this checks to see if the patch has already been run.
if (userMenu && userMenu.innerHTML.toString().includes('Welcome, ')) {
let children = [...userMenu.children];
userMenu.textContent = '';
children.forEach((child, idx) => {
userMenu.appendChild(child);
});
let userButton = userMenu.querySelector('#LB_LoginDetails');
children = [...userButton.children];
userButton.textContent = 'Welcome ';
children.forEach((child, idx) => {
userButton.appendChild(child);
});
}
};
const feature_navbar_fixes = () => {
linkOverrides.forEach((override, idx) => {
let el = document?.querySelector(override.selector) || false;
if (el) el.href = override.link;
});
};
const feature_styling_fixes = () => {
let leftContent =
document?.querySelector('.detailsleftdiv-container-circ') ||
document.querySelector('.detailsleftdiv');
let rightContent =
document?.querySelector('.right-section-container.rsc-details') ||
document.querySelector('.right-section-container');
let contentDiv = document.createElement('div');
contentDiv.setAttribute('class', 'smk-contentContainer');
document.querySelector('#form1').appendChild(contentDiv);
if (leftContent) contentDiv.appendChild(leftContent);
if (rightContent) contentDiv.appendChild(rightContent);
};
const feature_modal_click_outside = () => {
let modalBackdrops = [...document.querySelectorAll('.modal-backdrop')];
modalBackdrops.forEach((el, idx) => {
el.addEventListener('click', (e) => {
buttonHandlers.escapeCurrentWindow();
});
});
};
const feature_second_keybinds = () => {
document.body.addEventListener('keydown', (e) => {
if (
[49, 50, 51, 52, 53, 54, 55, 56, 57].includes(e.keyCode) &&
e.altKey
) {
e.preventDefault();
// This is kinda silly, but it works
buttonHandlers.handleSecondaryMenuBtn(e.keyCode - 48);
}
});
};
const feature_second_tooltips = () => {
document.body.addEventListener('keydown', (e) => {
if (e.altKey) {
document.documentElement.style.setProperty('--custom-label-opacity', 1);
}
});
document.body.addEventListener('keyup', (e) => {
if (e.keyCode == 18) {
e.preventDefault();
document.documentElement.style.setProperty('--custom-label-opacity', 0);
}
});
};
const feature_favicon = () => {
helperFunctions.setFavicon(
'https://cdn.owenrossikeen.com/public/image/evolveextensions/favicon.ico'
);
};
if (second_keybinds || second_tooltips) prereq_second_keybinds_tooltips();
// Conditionally initialize single-run functions
if (navbar_fixes) feature_navbar_fixes();
if (modal_click_outside) feature_modal_click_outside();
if (favicon) feature_favicon();
if (second_keybinds) feature_second_keybinds();
if (second_tooltips) feature_second_tooltips();
// This one is mandatory;
feature_styling_fixes();
new MutationObserver(() => {
// Conditionally initialize DOM-reactive functions
if (keybinds) feature_keybinds();
if (user_wm_fix) feature_user_wm_fix();
}).observe(document.body, {
attributes: true,
childList: true,
characterData: true,
subtree: true,
});
};
Start();