Skip to content

Commit de190df

Browse files
authored
[FORUMS] Fix deprecated patching method (#119)
* chore: prettify * fix: migrate from DOMNodeInserted to MutationObserver * Prettified Code! --------- Co-authored-by: blankdvth <blankdvth@users.noreply.github.com>
1 parent 37b2847 commit de190df

File tree

2 files changed

+69
-51
lines changed

2 files changed

+69
-51
lines changed

src/EGO Forum Enhancement.ts

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name EdgeGamers Forum Enhancement%RELEASE_TYPE%
33
// @namespace https://github.com/blankdvth/eGOScripts/blob/master/src/EGO%20Forum%20Enhancement.ts
4-
// @version 4.11.4
4+
// @version 4.11.5
55
// @description Add various enhancements & QOL additions to the EdgeGamers Forums that are beneficial for Leadership members.
66
// @author blank_dvth, Skle, MSWS, PixeL
77
// @match https://www.edgegamers.com/*
@@ -629,12 +629,19 @@ function setupForumsConfig() {
629629
});
630630

631631
const profileMenu = document.querySelector("div.js-visitorMenuBody");
632-
if (profileMenu)
633-
profileMenu.addEventListener(
634-
"DOMNodeInserted",
635-
handleProfileDropdown,
636-
false,
637-
);
632+
if (profileMenu) {
633+
const profileMenuObserver = new MutationObserver((mutations) => {
634+
mutations.every((mutation) => {
635+
mutation.addedNodes.forEach((node) => {
636+
handleProfileDropdown(node as HTMLElement);
637+
});
638+
});
639+
});
640+
profileMenuObserver.observe(profileMenu, {
641+
childList: true,
642+
subtree: true,
643+
});
644+
}
638645
}
639646

640647
/**
@@ -1623,10 +1630,8 @@ function generateResponseText(response: string) {
16231630
* @param {HTMLElementEventMap} event
16241631
* @returns void
16251632
*/
1626-
function tooltipMAULListener(event: Event) {
1633+
function tooltipMAULListener(target: HTMLElement) {
16271634
// Make sure this specific event is the node we want
1628-
if (!event.target) return;
1629-
const target = event.target as HTMLElement;
16301635
if (
16311636
target.nodeName != "DIV" ||
16321637
!target.classList.contains("tooltip-content-inner")
@@ -1697,11 +1702,10 @@ function handleThreadMovePage(hash: string) {
16971702
).checked = false;
16981703
(
16991704
checkArr
1700-
.find(
1701-
(el) =>
1702-
el.textContent?.startsWith(
1703-
"Notify thread starter of this action.",
1704-
),
1705+
.find((el) =>
1706+
el.textContent?.startsWith(
1707+
"Notify thread starter of this action.",
1708+
),
17051709
)
17061710
?.querySelector("label > input") as HTMLInputElement
17071711
).checked = false;
@@ -1918,9 +1922,7 @@ function handleBanAppealReport(report: boolean = false) {
19181922
* @param {HTMLElementEventMap} event
19191923
* @returns void
19201924
*/
1921-
function handleOnHold(event: Event) {
1922-
if (!event.target) return;
1923-
const target = event.target as HTMLElement;
1925+
function handleOnHold(target: HTMLElement) {
19241926
if (
19251927
target.nodeName != "DIV" ||
19261928
!target.classList.contains("overlay-container") ||
@@ -1976,9 +1978,7 @@ function handleOnHold(event: Event) {
19761978
* @param {HTMLElementEventMap} event
19771979
* @returns void
19781980
*/
1979-
function handleProfileDropdown(event: Event) {
1980-
if (!event.target) return;
1981-
const target = event.target as HTMLElement;
1981+
function handleProfileDropdown(target: HTMLElement) {
19821982
if (target.nodeName != "UL" || !target.classList.contains("tabPanes"))
19831983
return;
19841984
const btn = document.createElement("a");
@@ -2313,21 +2313,25 @@ function blockSignatures() {
23132313
const signature = post.querySelector(
23142314
"aside.message-signature > div",
23152315
) as HTMLDivElement;
2316-
// iframe's are added after page load, using a DOMNodeInserted event to work around that
2317-
function signatureEvent(event: Event) {
2318-
if (!event.target) return;
2319-
if (!((event.target as HTMLElement).nodeName === "IFRAME"))
2320-
return;
2321-
(event.target as HTMLIFrameElement).dataset.src = (
2322-
event.target as HTMLIFrameElement
2316+
// iframe's are added after page load, using a MutationObserver to work around that
2317+
function signatureEvent(target: HTMLElement) {
2318+
if (!(target.nodeName === "IFRAME")) return;
2319+
(target as HTMLIFrameElement).dataset.src = (
2320+
target as HTMLIFrameElement
23232321
).src;
2324-
(event.target as HTMLIFrameElement).src = "about:blank";
2322+
(target as HTMLIFrameElement).src = "about:blank";
23252323
}
2326-
signature.addEventListener(
2327-
"DOMNodeInserted",
2328-
signatureEvent,
2329-
false,
2330-
);
2324+
const signatureObserver = new MutationObserver((mutations) => {
2325+
mutations.every((mutation) => {
2326+
mutation.addedNodes.forEach((node) => {
2327+
signatureEvent(node as HTMLElement);
2328+
});
2329+
});
2330+
});
2331+
signatureObserver.observe(signature, {
2332+
childList: true,
2333+
subtree: true,
2334+
});
23312335
// Set the SRC of content to nothing (data:,), empty string is not used as it may cause additional requests to the page
23322336
// Issue originated back in 2009, unsure if it is still a problem but best to lean on the safe side.
23332337
// Was fixed in FireFox a while ago, not sure about Chrome
@@ -2388,11 +2392,7 @@ function blockSignatures() {
23882392
source.src = source.dataset.src as string;
23892393
delete source.dataset.src;
23902394
});
2391-
signature.removeEventListener(
2392-
"DOMNodeInserted",
2393-
signatureEvent,
2394-
false,
2395-
);
2395+
signatureObserver.disconnect();
23962396
btn.remove();
23972397
};
23982398
btn.innerHTML = "Load Signature";
@@ -2420,12 +2420,29 @@ function blockSignatures() {
24202420
const url = window.location.href;
24212421
const hash = window.location.hash;
24222422

2423-
document.body.addEventListener(
2424-
"DOMNodeInserted",
2425-
tooltipMAULListener,
2426-
false,
2427-
);
2428-
document.body.addEventListener("DOMNodeInserted", handleOnHold, false);
2423+
const tooltipObserver = new MutationObserver((mutations) => {
2424+
mutations.every((mutation) => {
2425+
mutation.addedNodes.forEach((node) => {
2426+
tooltipMAULListener(node as HTMLElement);
2427+
});
2428+
});
2429+
});
2430+
tooltipObserver.observe(document.body, {
2431+
childList: true,
2432+
subtree: true,
2433+
});
2434+
2435+
const onHoldObserver = new MutationObserver((mutations) => {
2436+
mutations.every((mutation) => {
2437+
mutation.addedNodes.forEach((node) => {
2438+
handleOnHold(node as HTMLElement);
2439+
});
2440+
});
2441+
});
2442+
onHoldObserver.observe(document.body, {
2443+
childList: true,
2444+
subtree: true,
2445+
});
24292446

24302447
// Add Helpful Links to the Navigation Bar
24312448
const nav_list = document.querySelector(".p-nav-list") as HTMLUListElement;

src/EGO MAUL Enhancement.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,13 @@ function handleAddBan(hash: string = "") {
649649
(document.getElementById("handle") as HTMLInputElement).value =
650650
data.name;
651651
if (data.threadId)
652-
(
653-
document.getElementById("notes") as HTMLTextAreaElement
654-
).value = `https://edgegamers.com/threads/${data.threadId}/\n\n`;
652+
(document.getElementById("notes") as HTMLTextAreaElement).value =
653+
`https://edgegamers.com/threads/${data.threadId}/\n\n`;
655654
(document.getElementById("gameId") as HTMLInputElement).value = data.id;
656655
(
657656
document.querySelector("input[name='redirect']") as HTMLInputElement
658-
).value = `https://maul.edgegamers.com/index.php?page=bans&qType=gameId&q=${data.id}`;
657+
).value =
658+
`https://maul.edgegamers.com/index.php?page=bans&qType=gameId&q=${data.id}`;
659659
}
660660
}
661661

@@ -706,8 +706,9 @@ function handleEditBan() {
706706
}
707707

708708
// Steam ID buttons
709-
const idGroup = document.querySelector(".control-label[for=gameId]")
710-
?.parentElement;
709+
const idGroup = document.querySelector(
710+
".control-label[for=gameId]",
711+
)?.parentElement;
711712
const id = idGroup?.querySelector("p")?.innerText;
712713
if (id) {
713714
const idDiv = document.createElement("div");

0 commit comments

Comments
 (0)