Codeforces Friends Online Status

Show the online status of friends in the friends table

当前为 2022-12-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Codeforces Friends Online Status
  3. // @namespace https://github.com/ItsTHEAvro
  4. // @version 0.2
  5. // @description Show the online status of friends in the friends table
  6. // @author ItsTHEAvro
  7. // @license MIT
  8. // @supportURL https://github.com/ItsTHEAvro/GreasyForkScripts/CodeforcesFriendsOnlineStatus
  9. // @match https://codeforces.com/friends
  10. // @grant GM_xmlhttpRequest
  11. // @connect codeforces.com
  12. // ==/UserScript==
  13.  
  14. // Get the list of friend usernames from the table
  15. let friendsTable = document.querySelectorAll('table')[5];
  16. let headerRow = friendsTable.rows[0];
  17. let friends = [];
  18. for (let row of friendsTable.rows) {
  19. if (row === headerRow) continue;
  20. let friend = row.cells[1].innerText; // Get the username from the 6th column of the table
  21. friends.push(friend);
  22. let newCell = row.insertCell(-1); // Insert a new cell at the end of the row
  23. let previousCell = row.cells[row.cells.length - 2];
  24. let computedStyle = getComputedStyle(previousCell);
  25. for (let property of computedStyle) {
  26. newCell.style.setProperty(property, computedStyle.getPropertyValue(property), computedStyle.getPropertyPriority(property));
  27. }
  28. newCell.style.borderLeft = '1px solid #e1e1e1';
  29. }
  30.  
  31. let newHeaderCell = headerRow.insertCell(-1);
  32. newHeaderCell.innerText = 'Last Online';
  33.  
  34. let previousHeaderCell = headerRow.cells[headerRow.cells.length - 2];
  35. let computedHeaderStyle = getComputedStyle(previousHeaderCell);
  36. for (let property of computedHeaderStyle) {
  37. newHeaderCell.style.setProperty(property, computedHeaderStyle.getPropertyValue(property), computedHeaderStyle.getPropertyPriority(property));
  38. }
  39. newHeaderCell.style.borderLeft = '1px solid #e1e1e1';
  40.  
  41. friends.forEach((friend, index) => {
  42. setTimeout(() => {
  43. GM_xmlhttpRequest({
  44. method: 'GET',
  45. url: `https://codeforces.com/api/user.info?handles=${friend}`,
  46. onload: function(response) {
  47. let data;
  48. try {
  49. data = JSON.parse(response.responseText);
  50. } catch (error) {
  51. console.error('Error parsing JSON data:');
  52. return;
  53. }
  54. let lastOnlineSeconds = data.result[0].lastOnlineTimeSeconds;
  55. let currentTimeSeconds = Math.floor(Date.now() / 1000);
  56. let elapsedTimeSeconds = currentTimeSeconds - lastOnlineSeconds;
  57. let years = Math.floor(elapsedTimeSeconds / 31536000);
  58. let months = Math.floor((elapsedTimeSeconds % 31536000) / 2592000);
  59. let weeks = Math.floor((elapsedTimeSeconds % 2592000) / 604800);
  60. let days = Math.floor((elapsedTimeSeconds % 604800) / 86400);
  61. let hours = Math.floor((elapsedTimeSeconds % 86400) / 3600);
  62. let minutes = Math.floor((elapsedTimeSeconds % 3600) / 60);
  63. let onlineStatus;
  64. if (years > 0) {
  65. let yearsText = years === 1 ? 'year' : 'years';
  66. onlineStatus = `${years} ${yearsText} ago `;
  67. } else if (months > 0) {
  68. let monthsText = months === 1 ? 'month' : 'months';
  69. onlineStatus = `${months} ${monthsText} ago `;
  70. } else if (weeks > 0) {
  71. let weeksText = weeks === 1 ? 'week' : 'weeks';
  72. onlineStatus = `${weeks} ${weeksText} ago `;
  73. } else if (days > 0) {
  74. let daysText = days === 1 ? 'day' : 'days';
  75. onlineStatus = `${days} ${daysText} ago `;
  76. } else if (hours > 0) {
  77. let hoursText = hours === 1 ? 'hour' : 'hours';
  78. let minutesText = minutes === 1 ? 'minute' : 'minutes';
  79. onlineStatus = `${hours} ${hoursText} and ${minutes} ${minutesText} ago `;
  80. } else {
  81. let minutesText = minutes === 1 ? 'minute' : 'minutes';
  82. onlineStatus = `${minutes} ${minutesText} ago `;
  83. }
  84.  
  85. let row = friendsTable.rows[index + 1];
  86. let newCell = row.cells[row.cells.length - 1];
  87.  
  88. newCell.innerText = onlineStatus;
  89. }
  90. });
  91. }, 1000 * index);
  92. });