diff --git a/extension/changelog.json b/extension/changelog.json index d3b7f7cff..c3c3dcf09 100644 --- a/extension/changelog.json +++ b/extension/changelog.json @@ -9,7 +9,8 @@ { "message": "Blackjack strategy for new page URL.", "contributor": "TheFoxMan" }, { "message": "Flowers/Plushies Missing Set uses on page data instead of Torn API data.", "contributor": "TheFoxMan" }, { "message": "Change the element from where Quick Items takes XIDs.", "contributor": "TheFoxMan" }, - { "message": "Fix Block Zalgo, Chat Autocomplete, Chat Highlight, Colored Chats, Hide Chats, Search Chats, User Alias, Chat Font Size for Chats 2.0.", "contributor": "TheFoxMan" } + { "message": "Fix Block Zalgo, Chat Autocomplete, Chat Highlight, Colored Chats, Hide Chats, Search Chats, User Alias, Chat Font Size for Chats 2.0.", "contributor": "TheFoxMan" }, + { "message": "Trade Chat Timer for Chats 2.0.", "contributor": "TheFoxMan" } ], "changes": [ { diff --git a/extension/scripts/content/global/ttGlobal.js b/extension/scripts/content/global/ttGlobal.js index b8ea7489e..130937e3f 100644 --- a/extension/scripts/content/global/ttGlobal.js +++ b/extension/scripts/content/global/ttGlobal.js @@ -86,7 +86,7 @@ if (countdown.dataset.end) { seconds = parseInt((parseInt(countdown.dataset.end) - now) / 1000); } else { - seconds = parseInt(countdown.dataset.seconds) - 1; + seconds = parseInt(countdown.dataset.seconds ?? "0") - 1; } if (seconds <= 0) { diff --git a/extension/scripts/features/trade-timer/ttTradeTimer.js b/extension/scripts/features/trade-timer/ttTradeTimer.js index f7cb1a7f2..a9dee8eb7 100644 --- a/extension/scripts/features/trade-timer/ttTradeTimer.js +++ b/extension/scripts/features/trade-timer/ttTradeTimer.js @@ -16,27 +16,24 @@ function initialise() { CUSTOM_LISTENERS[EVENT_CHANNELS.CHAT_OPENED].push(({ chat }) => { - if (!chat.classList.contains("^=_trade_")) return; + if (chat.find("[class*='chat-box-header__info__']").textContent !== "Trade") return; - triggerTrade(chat); + listenTradeChatInput(chat); }); } + let timer; async function detectChat() { await requireChatsLoaded(); - const chat = document.find("#chatRoot [class*='_chat-box_'][class*='_trade_'][class*='_chat-active_']"); - if (!chat) return; - - triggerTrade(chat); - } - - function triggerTrade(chat) { - const input = chat.find("[class*='_chat-box-input_']"); + const tradeChatButton = document.find("#chatRoot [class*='minimized-menu-item__'][title='Trade']"); + if (!tradeChatButton) { + console.error("TornTools - Trade Chat Button not found."); + return; + } - let timer; - if (input.find("#tt-trade-timer")) timer = input.find("#tt-trade-timer"); - else { + tradeChatButton.find("svg").classList.add("tt-hidden"); + if (!timer) { timer = document.newElement({ type: "div", id: "tt-trade-timer", @@ -45,71 +42,72 @@ timeSettings: JSON.stringify({ type: "wordTimer", extraShort: true }), }, }); - input.insertBefore(document.newElement({ type: "div", children: [timer] }), input.firstElementChild); - - input.find("textarea").addEventListener("keypress", onKeyPress); - input.classList.add("tt-modified"); } + if (!countdownTimers.includes(timer)) countdownTimers.push(timer); const now = Date.now(); if (localdata.tradeMessage > now) { timer.textContent = formatTime({ milliseconds: localdata.tradeMessage - now }, { type: "wordTimer", extraShort: true }); timer.dataset.seconds = ((localdata.tradeMessage - now) / TO_MILLIS.SECONDS).dropDecimals().toString(); - countdownTimers.push(timer); } else { timer.textContent = "OK"; } + tradeChatButton.appendChild(timer); - const search = input.find(".tt-chat-filter"); - if (search) timer.parentElement.appendChild(search); + listenTradeChatInput(); } - async function onKeyPress(event) { + function getTradeChat() { + const openChats = document.findAll("#chatRoot [class^='chat-box__']"); + if (!openChats.length) return; + + return [...openChats].filter((chat) => chat.find("[class*='chat-box-header__info__']").textContent === "Trade")?.[0]; + } + + function listenTradeChatInput(tradeChat = null) { + if (!tradeChat) + tradeChat = getTradeChat(); + if (!tradeChat) return; + + tradeChat.find("[class*='chat-box-footer__textarea__']").addEventListener("keyup", onKeyUp); + } + + async function onKeyUp(event) { if (event.key !== "Enter") return; - if (!event.target.value) return; + // if (!event.target.value) return; - const chat = findParent(event.target, { class: "^=_chat-box_" }); - const overview = chat.find("[class*='_overview_']"); + const tradeChat = event.target.closest("[class^='chat-box__']"); + const chatBody = tradeChat.find("[class*='chat-box-body___']"); const message = await new Promise((resolve) => { new MutationObserver((mutations, observer) => { - const mutation = mutations.find((mutation) => mutation.addedNodes.length); + const mutation = mutations.filter((mutation) => mutation.addedNodes.length).last(); if (!mutation) return; const node = mutation.addedNodes[0]; observer.disconnect(); resolve(node); - }).observe(overview, { childList: true }); + }).observe(chatBody, { childList: true }); }); if (event.target.value) return; - if (message.classList.contains("^=error_")) return; + if (message.className.includes("chat-box-body__block-message-wrapper__") && message.textContent === "Trade rooms allows one message per 60 seconds") return; await ttStorage.change({ localdata: { tradeMessage: Date.now() + TO_MILLIS.SECONDS * 61 } }); } function cleanup() { - const chat = document.find("#chatRoot [class*='_chat-box_'][class*='_trade_'][class*='_chat-active_']"); - if (!chat) return; - - const timer = chat.find("#tt-trade-timer"); - if (timer) timer.remove(); + timer.remove(); + timer = null; - const input = chat.find("[class*='_chat-box-input_']"); + const tradeChatButton = document.find("#chatRoot [class*='minimized-menu-item__'][title='Trade']"); + if (tradeChatButton) + tradeChatButton.find("svg").classList.remove("tt-hidden"); - if (!input.find(".tt-chat-filter")) input.classList.remove("tt-modified"); + const tradeChat = getTradeChat(); + if (!tradeChat) return; - input.find("textarea").removeEventListener("keypress", onKeyPress); + tradeChat.find("textarea").removeEventListener("keypress", onKeyUp); } - - // function moveSearch() { - // const timer = document.find("#tt-trade-timer"); - // if (!timer) return; - // - // const search = timer.parentElement.parentElement.find(".tt-chat-filter"); - // if (!search) return; - // - // timer.parentElement.appendChild(search); - // } })(); diff --git a/extension/scripts/global/featureManager.js b/extension/scripts/global/featureManager.js index baff9438a..887c5bc05 100644 --- a/extension/scripts/global/featureManager.js +++ b/extension/scripts/global/featureManager.js @@ -175,7 +175,7 @@ class FeatureManager { this.showResult(feature, "disabled"); } } catch (error) { - await this.executeFunction(feature.cleanup).catch((error) => this.logError(`Failed to cleanup in a failed start of "${name}".`, error)); + await this.executeFunction(feature.cleanup).catch((error) => this.logError(`Failed to cleanup in a failed start of "${feature.name}".`, error)); this.showResult(feature, "failed"); this.logError(`Failed to start "${feature.name}".`, error).then(() => {});