FF Progs

Adds a Button to view logs in xivanalysis also some minor improvements.

当前为 2023-05-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name FF Progs
  3. // @name:en FF Progs
  4. // @description Adds a Button to view logs in xivanalysis also some minor improvements.
  5. // @description:en Adds a Button to view logs in xivanalysis also some minor improvements.
  6. // @version 1.0.6
  7. // @namespace k_fizzel
  8. // @author Chad Bradly
  9. // @website https://www.fflogs.com/character/id/12781922
  10. // @icon https://assets.rpglogs.com/img/ff/favicon.png?v=2
  11. // @match https://*.fflogs.com/*
  12. // @require https://code.jquery.com/jquery-3.2.0.min.js
  13. // @grant unsafeWindow
  14. // @grant GM_addStyle
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_deleteValue
  18. // @license MIT License
  19. // ==/UserScript==
  20.  
  21. (function () {
  22. "use strict";
  23.  
  24. const JOB_ORDER = [
  25. // Tanks
  26. "Paladin",
  27. "Warrior",
  28. "DarkKnight",
  29. "Gunbreaker",
  30. // Healers
  31. "WhiteMage",
  32. "Scholar",
  33. "Astrologian",
  34. "Sage",
  35. // Melee
  36. "Monk",
  37. "Dragoon",
  38. "Ninja",
  39. "Samurai",
  40. "Reaper",
  41. // Physical Ranged
  42. "Bard",
  43. "Machinist",
  44. "Dancer",
  45. // Magical Ranged
  46. "BlackMage",
  47. "Summoner",
  48. "RedMage",
  49. ];
  50. const ABILITY_TYPES = {
  51. 0: "None",
  52. 1: "Buff",
  53. 2: "Unknown",
  54. 4: "Unknown",
  55. 8: "Heal",
  56. 16: "Unknown",
  57. 32: "True",
  58. 64: "DOT",
  59. 124: "Darkness",
  60. 125: "Darkness",
  61. 126: "Darkness",
  62. 127: "Darkness",
  63. 128: "Physical",
  64. 256: "Magical",
  65. 512: "Unknown",
  66. 1024: "Magical",
  67. };
  68. const PASSIVE_LB_GAIN = [
  69. ["75"], // one bar
  70. ["180"], // two bars
  71. ["220", "170", "160", "154", "144", "140"], // three bars
  72. ];
  73. // this code was made in 1 day so its not the best but it works :D
  74. const LB_PIN = `2$Main$#ffff14$script$let l;pinMatchesFightEvent=(e,f)=>{switch(e.type){case"limitbreakupdate":return l&&l===e.timestamp||(l=e.timestamp),!0;case"calculateddamage":if("Player"===e.target.type&&e.timestamp===l)return!0;break;case"heal":if(!e.isTick&&e.timestamp===l)return!0}return!1};`;
  75. const REPORTS_PATH_REGEX = /\/reports\/.+/;
  76. const ZONE_RANKINGS_PATH_REGEX = /\/zone\/rankings\/.+/;
  77. const CHARACTER_PATH_REGEX = /\/character\/.+/;
  78. const PROFILE_PATH_REGEX = /\/profile/;
  79. const LB_REGEX = /The limit break gauge updated to (\d+). There are (\d+) total bars./;
  80.  
  81. const apiKey = GM_getValue("apiKey");
  82.  
  83. const getHashParams = () => {
  84. const hash = window.location.hash.substring(1);
  85. const params = {};
  86.  
  87. hash.split("&").forEach((pair) => {
  88. const [key, value] = pair.split("=");
  89. params[key] = decodeURIComponent(value);
  90. });
  91.  
  92. return params;
  93. };
  94.  
  95. const changeHashParams = (defaultParams) => {
  96. const hashParams = getHashParams();
  97. const newParams = {
  98. ...hashParams,
  99. ...defaultParams,
  100. };
  101.  
  102. location.hash = Object.entries(newParams)
  103. .filter(([_key, value]) => !["undefined", "null", "", null, undefined].includes(value))
  104. .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
  105. .join("&");
  106. };
  107.  
  108. const characterAllStar = (rank, outOf, rDPS, rankOneRDPS) => {
  109. return Math.min(Math.max(100 * (rDPS / rankOneRDPS), 100 - (rank / outOf) * 100) + 20 * (rDPS / rankOneRDPS), 120);
  110. };
  111.  
  112. // AdBlock
  113. $("#top-banner, .side-rail-ads, #bottom-banner, #subscription-message-tile-container, #playwire-video-container, #right-ad-box, #right-vertical-banner").remove();
  114. $("#table-container").css("margin", "0 0 0 0");
  115.  
  116. // Reports Page
  117. if (REPORTS_PATH_REGEX.test(location.pathname)) {
  118. // Add XIV Analysis Button
  119. $("#filter-analyze-tab").before(
  120. `<a target="_blank" class="big-tab view-type-tab" id="xivanalysis-tab"><span class="zmdi zmdi-time-interval"></span> <span class="big-tab-text"><br>xivanalysis</span></a>`
  121. );
  122. $("#xivanalysis-tab").click(() => {
  123. $("#xivanalysis-tab").attr("href", `https://xivanalysis.com/report-redirect/${location.href}`);
  124. });
  125.  
  126. $("#filter-type-tabs").css("cursor", "default");
  127. // add new tab 1 before last element
  128. $("#filter-type-tabs").find("a:nth-last-child(2)").after(`<a href="#" class="filter-type-tab drop" id="filter-lb-tab">LB</a>`);
  129. $("#filter-lb-tab").click(() => {
  130. changeHashParams({
  131. type: "summary",
  132. view: "events",
  133. pins: LB_PIN,
  134. });
  135. return false;
  136. });
  137.  
  138. let jobs;
  139. const rankOnes = {};
  140.  
  141. const onTableChange = () => {
  142. const hashParams = getHashParams();
  143. let lastLbGain;
  144. let lastTimeDiff;
  145. // Rankings Tab
  146. if (hashParams.view === "rankings") {
  147. if (!GM_getValue("apiKey")) return;
  148. const rows = [];
  149. if (!jobs) {
  150. fetch(`https://www.fflogs.com/v1/classes?api_key=${GM_getValue("apiKey")}`)
  151. .then((res) => res.json())
  152. .then((data) => {
  153. jobs = data[0].specs;
  154. rows.forEach((row) => {
  155. updatePoints(row);
  156. });
  157. })
  158. .catch((err) => console.error(err));
  159. } else {
  160. setTimeout(() => {
  161. rows.forEach((row) => {
  162. updatePoints(row);
  163. });
  164. }, 0);
  165. }
  166.  
  167. const updatePoints = async (row) => {
  168. const hashParams = getHashParams();
  169. const rank = Number($(row).find("td:nth-child(2)").text().replace("~", ""));
  170. const outOf = Number($(row).find("td:nth-child(3)").text().replace(",", ""));
  171. const dps = Number($(row).find("td:nth-child(6)").text().replace(",", ""));
  172. const jobName = $(row).find("td:nth-child(5) > a").attr("class") || "";
  173. const jobName2 = $(row).find("td:nth-child(5) > a:nth-last-child(1)").attr("class") || "";
  174. const playerMetric = hashParams.playermetric || "rdps";
  175.  
  176. if (jobName2 !== "players-table-realm") {
  177. $(row)
  178. .find("td:nth-child(7)")
  179. .html(`<center><img src="https://cdn.7tv.app/emote/62523dbbbab59cfd1b8b889d/1x.webp" title="No api v1 endpoint for combined damage." style="height: 15px;"></center>`);
  180. return;
  181. }
  182.  
  183. const updateCharecterAllStar = async () => {
  184. $(row).find("td:nth-child(7)").html(characterAllStar(rank, outOf, dps, rankOnes[jobName][playerMetric]).toFixed(2));
  185. };
  186.  
  187. if (!rankOnes[jobName]) {
  188. rankOnes[jobName] = {};
  189. }
  190.  
  191. if (!rankOnes[jobName][playerMetric]) {
  192. const url = `https://www.fflogs.com/v1/rankings/encounter/${reportsCache.filterFightBoss}?metric=${playerMetric}&spec=${
  193. jobs.find((job) => job.name.replace(" ", "") === jobName)?.id
  194. }&api_key=${GM_getValue("apiKey")}`;
  195. fetch(url)
  196. .then((res) => res.json())
  197. .then((data) => {
  198. rankOnes[jobName][playerMetric] = Number(data.rankings[0].total.toFixed(1));
  199. updateCharecterAllStar();
  200. })
  201. .catch((err) => console.error(err));
  202. } else {
  203. updateCharecterAllStar();
  204. }
  205. };
  206.  
  207. $(".player-table").each((_i, table) => {
  208. $(table)
  209. .find("thead tr th:nth-child(6)")
  210. .after(
  211. `<th class="sorting ui-state-default" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Patch: activate to sort column ascending"><div class="DataTables_sort_wrapper">Points<span class="DataTables_sort_icon css_right ui-icon ui-icon-caret-2-n-s"></span></div></th>`
  212. );
  213. $(table)
  214. .find("tbody tr")
  215. .each((_i, row) => {
  216. $(row)
  217. .find("td:nth-child(6)")
  218. .after(`<td class="rank-per-second primary main-table-number"><center><span class="zmdi zmdi-spinner zmdi-hc-spin" style="color:white font-size:24px"></center></span></td>`);
  219. rows.push(row);
  220. });
  221. });
  222. }
  223.  
  224. // Events Tab
  225. if (hashParams.view === "events") {
  226. $(".events-table")
  227. .find("thead tr th:nth-child(1)")
  228. .before(`<th class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper">Diff<span class="DataTables_sort_icon"></span></div></th>`);
  229.  
  230. $(".main-table-number").each((_i, cell) => {
  231. if (lastTimeDiff) {
  232. const time = moment($(cell).text(), "m:ss.SSS");
  233. const diff = (time.diff(lastTimeDiff) / 1000).toFixed(3);
  234. $(cell).before(`<td style="width: 2em; text-align: right;">${diff.padStart(5, "0")}</td>`);
  235. lastTimeDiff = time;
  236. } else {
  237. $(cell).before(`<td style="width: 2em; text-align: right;"> - </td>`);
  238. lastTimeDiff = moment($(cell).text(), "m:ss.SSS");
  239. }
  240. });
  241. }
  242.  
  243. // LB Tab
  244. if (hashParams.view === "events" && hashParams.type === "summary" && hashParams.pins === LB_PIN) {
  245. $(".filter-type-tab.selected").removeClass("selected");
  246. $("#filter-lb-tab").addClass("selected");
  247.  
  248. $(".events-table")
  249. .find("thead tr th:nth-last-child(3)")
  250. .after(`<th class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper">Active<span class="DataTables_sort_icon"></span></div></th>`);
  251. $(".events-table")
  252. .find("thead tr th:nth-last-child(2)")
  253. .after(`<th class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper">Bars<span class="DataTables_sort_icon"></span></div></th>`);
  254.  
  255. $(".event-description-cell").each((_i, cell) => {
  256. const text = $(cell).text();
  257. if (text === "Event") {
  258. $(cell).html(`<div class="DataTables_sort_wrapper">Limit Break Total<span class="DataTables_sort_icon"></span></div>`);
  259. return;
  260. }
  261.  
  262. if (!LB_REGEX.test(text)) {
  263. $(cell).before(`<td style="width: 2em; text-align: right; white-space: nowrap;"> * </td>`);
  264. $(cell).after(`<td style="width: 2em; text-align: right;"> * </td>`);
  265. return;
  266. }
  267.  
  268. const lb = text.match(LB_REGEX);
  269. const currentLb = Number(lb?.[1]);
  270. const currentBars = Number(lb?.[2]);
  271.  
  272. if (lb) {
  273. let diff;
  274. if (lastLbGain !== undefined) {
  275. diff = (currentLb - lastLbGain).toLocaleString();
  276. } else {
  277. diff = " - ";
  278. }
  279. lastLbGain = currentLb;
  280. let actualDiff = diff > 0 ? `+${diff}` : diff;
  281.  
  282. if (PASSIVE_LB_GAIN[currentBars - 1].includes(diff)) {
  283. // passive lb gain
  284. diff = " - ";
  285. } else {
  286. // active lb gain
  287. }
  288.  
  289. $(cell).before(`<td style="width: 2em; text-align: right; white-space: nowrap;">${diff}</td>`);
  290. $(cell).html(`${Number(currentLb).toLocaleString()} / ${(Number(currentBars) * 10000).toLocaleString()} <span style="float: right;">${actualDiff}</span>`);
  291. $(cell).after(`<td style="width: 2em; text-align: right;">${currentBars}</td>`);
  292. }
  293. });
  294. } else if (hashParams.pins === LB_PIN) {
  295. $("#filter-lb-tab").removeClass("selected");
  296. $(`#filter-${hashParams.type}-tab`).addClass("selected");
  297. changeHashParams({ pins: "" });
  298. }
  299. };
  300.  
  301. const tableContainer = document.querySelector("#table-container");
  302. if (tableContainer) {
  303. const observer = new MutationObserver(onTableChange);
  304. observer.observe(tableContainer, { attributes: true, characterData: true, childList: true });
  305. }
  306. }
  307.  
  308. // Zone Rankings Page
  309. if (ZONE_RANKINGS_PATH_REGEX.test(location.pathname)) {
  310. const onTableChange = () => {
  311. $(".main-table-name").each((_i, cell) => {
  312. if ($(cell).find(".main-table-realm").text().includes("(JP)")) {
  313. if ($(cell).find(".main-table-guild").attr("href").includes("translate=true")) return;
  314. $(cell)
  315. .find(".main-table-guild")
  316. .attr("href", `${$(cell).find(".main-table-guild").attr("href")}&translate=true`);
  317. }
  318. });
  319. };
  320.  
  321. onTableChange();
  322. const tableContainer = document.querySelector("#table-container");
  323. if (tableContainer) {
  324. const observer = new MutationObserver(onTableChange);
  325. observer.observe(tableContainer, { attributes: true, characterData: true, childList: true });
  326. }
  327. }
  328.  
  329. // Character Page
  330. if (CHARACTER_PATH_REGEX.test(location.pathname)) {
  331. // Chad Bradly's Profile Customization
  332. const CHAD_ID_REGEX = /\/character\/id\/12781922/;
  333. const CHAD_NAME_REGEX = /\/character\/na\/sargatanas\/chad%20bradly/;
  334. const CHAD_ICON_URL = "https://media.tenor.com/epNMHGvRyHcAAAAd/gigachad-chad.gif";
  335.  
  336. if (CHAD_ID_REGEX.test(location.pathname) || CHAD_NAME_REGEX.test(location.pathname)) {
  337. $("#character-portrait-image").attr("src", CHAD_ICON_URL);
  338. }
  339. }
  340.  
  341. // Profile Page
  342. if (PROFILE_PATH_REGEX.test(location.pathname)) {
  343. const $extension = $(`
  344. <div id="extension" class="dialog-block">
  345. <div id="extension-title" class="dialog-title">FF Progs</div>
  346. <div id="extension-content" style="margin:1em"></div>
  347. </div>
  348. `);
  349.  
  350. const $apiInputContainer = $(`
  351. <div id="api-input-container" style="margin:1em">
  352. <div>Enter your FFLogs API Key</div>
  353. <input type="text" id="api-key-input" style="margin-left: 10px" value="${apiKey || ""}">
  354. <input type="button" id="api-save-button" style="margin-left: 10px" value="${apiKey ? "Update API Key" : "Save API Key"}">
  355. </div>
  356. `);
  357.  
  358. const $apiStatus = $(`
  359. <div id="api-status" style="margin:1em; display: ${apiKey ? "block" : "none"}">
  360. <div>API Key ${apiKey ? "saved" : "not saved"}</div>
  361. <input type="button" id="api-remove-button" style="margin-left: 10px" value="Remove API Key">
  362. </div>
  363. `);
  364.  
  365. const saveApiKey = () => {
  366. const newApiKey = $("#api-key-input").val().trim();
  367. if (newApiKey) {
  368. GM_setValue("apiKey", newApiKey);
  369. $apiStatus.show().find("div").text("API Key saved");
  370. $apiInputContainer.hide();
  371. setTimeout(() => {
  372. $apiStatus.hide();
  373. $apiInputContainer.show();
  374. }, 2000);
  375. }
  376. };
  377.  
  378. const removeApiKey = () => {
  379. GM_deleteValue("apiKey");
  380. $apiStatus.show().find("div").text("API Key removed");
  381. $apiStatus.find("#api-remove-button").remove();
  382. $apiInputContainer.show();
  383. setTimeout(() => {
  384. $apiStatus.hide();
  385. }, 2000);
  386. };
  387.  
  388. $extension.insertAfter("#api");
  389. $apiInputContainer.appendTo("#extension-content");
  390. $apiStatus.appendTo("#extension-content");
  391.  
  392. $apiInputContainer.on("click", "#api-save-button", saveApiKey);
  393. $apiStatus.on("click", "#api-remove-button", removeApiKey);
  394. }
  395. })();