|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +(async () => { |
| 4 | + const { hasSidebar } = await checkDevice(); |
| 5 | + if (!hasSidebar) return "Not supported on mobiles or tablets!"; |
| 6 | + |
| 7 | + featureManager.registerFeature( |
| 8 | + "OC2 Time", |
| 9 | + "sidebar", |
| 10 | + () => settings.pages.sidebar.oc2Timer && (userdata?.faction?.faction_id > 0 ?? false), |
| 11 | + null, |
| 12 | + showTimer, |
| 13 | + removeTimer, |
| 14 | + { |
| 15 | + storage: ["settings.pages.sidebar.oc2Timer", "settings.pages.sidebar.oc2TimerPosition", "userdata.organizedCrime"], |
| 16 | + }, |
| 17 | + () => { |
| 18 | + if (!hasAPIData()) return "No API access."; |
| 19 | + else if (!userdata.organizedCrime) return "No OC data (still on OC 1?)."; |
| 20 | + } |
| 21 | + ); |
| 22 | + |
| 23 | + async function showTimer() { |
| 24 | + await requireSidebar(); |
| 25 | + |
| 26 | + removeTimer(); |
| 27 | + addInformationSection(); |
| 28 | + showInformationSection(); |
| 29 | + |
| 30 | + const elements = []; |
| 31 | + |
| 32 | + const inCrime = ["Recruiting", "Planning"].includes(userdata.organizedCrime.status); |
| 33 | + if (inCrime) { |
| 34 | + elements.push(buildTimeLeftElement()); |
| 35 | + if (settings.pages.sidebar.oc2TimerPosition) { |
| 36 | + elements.push(document.newElement({ type: "span", class: "countdown", text: " - " })); |
| 37 | + elements.push(buildPositionElement()); |
| 38 | + } |
| 39 | + } else { |
| 40 | + elements.push(document.newElement({ type: "span", class: "countdown", text: "No crime joined." })); |
| 41 | + } |
| 42 | + |
| 43 | + document.find(".tt-sidebar-information").appendChild( |
| 44 | + document.newElement({ |
| 45 | + type: "section", |
| 46 | + id: "oc2Timer", |
| 47 | + children: [document.newElement({ type: "a", class: "title", text: "OC: ", href: LINKS.organizedCrimes }), ...elements], |
| 48 | + }) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + function buildTimeLeftElement() { |
| 53 | + const timeLeftElement = document.newElement({ type: "span", class: "countdown" }); |
| 54 | + |
| 55 | + const readyAt = userdata.organizedCrime.ready_at * 1000; |
| 56 | + const timeLeft = readyAt - Date.now(); |
| 57 | + |
| 58 | + if (timeLeft <= TO_MILLIS.HOURS * 8) timeLeftElement.classList.add("short"); |
| 59 | + else if (timeLeft <= TO_MILLIS.HOURS * 12) timeLeftElement.classList.add("medium"); |
| 60 | + |
| 61 | + if (timeLeft > 0) { |
| 62 | + const formatOptions = { type: "wordTimer", extraShort: true, showDays: true, truncateSeconds: true }; |
| 63 | + timeLeftElement.textContent = formatTime({ milliseconds: timeLeft }, formatOptions); |
| 64 | + |
| 65 | + timeLeftElement.dataset.end = readyAt; |
| 66 | + timeLeftElement.dataset.timeSettings = JSON.stringify(formatOptions); |
| 67 | + countdownTimers.push(timeLeftElement); |
| 68 | + } else { |
| 69 | + timeLeftElement.textContent = `Ready ${userdata.organizedCrime.status}`; |
| 70 | + } |
| 71 | + |
| 72 | + return timeLeftElement; |
| 73 | + } |
| 74 | + |
| 75 | + function buildPositionElement() { |
| 76 | + // const position = userdata.organizedCrime.slots.find(({ user_id }) => user_id === userdata.user_id)?.position ?? "???"; |
| 77 | + const position = userdata.organizedCrime.slots.find(({ user_id }) => user_id === 1043377)?.position ?? "???"; |
| 78 | + const name = userdata.organizedCrime.name; |
| 79 | + const level = userdata.organizedCrime.difficulty; |
| 80 | + |
| 81 | + return document.newElement({ type: "span", class: "position", text: `${position} in ${name} (Lvl ${level})` }); |
| 82 | + } |
| 83 | + |
| 84 | + function removeTimer() { |
| 85 | + document.find("#oc2Timer")?.remove(); |
| 86 | + } |
| 87 | + |
| 88 | + function addInformationSection() { |
| 89 | + if (document.find(".tt-sidebar-information")) return; |
| 90 | + |
| 91 | + const parent = document.find("#sidebarroot div[class*='user-information_'] div[class*='toggle-content_'] div[class*='content_']"); |
| 92 | + |
| 93 | + parent.appendChild(document.newElement({ type: "hr", class: "tt-sidebar-information-divider tt-delimiter tt-hidden" })); |
| 94 | + parent.appendChild(document.newElement({ type: "div", class: "tt-sidebar-information tt-hidden" })); |
| 95 | + } |
| 96 | + |
| 97 | + function showInformationSection() { |
| 98 | + document.find(".tt-sidebar-information-divider").classList.remove("tt-hidden"); |
| 99 | + document.find(".tt-sidebar-information").classList.remove("tt-hidden"); |
| 100 | + } |
| 101 | +})(); |
0 commit comments