Faction Last Active

description

当前为 2018-07-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Faction Last Active
  3. // @namespace namespace
  4. // @version 0.5.1
  5. // @description description
  6. // @author tos
  7. // @match *.torn.com/factions.php*
  8. // @grant GM_addStyle
  9. // ==/UserScript==
  10.  
  11. const apiKey = 'APIKEY'
  12.  
  13. GM_addStyle(`
  14. .last_action_icon {
  15. cursor: pointer;
  16. vertical-align: middle;
  17. display: inline-block;
  18. background-image: url(/images/v2/sidebar_icons_desktop_2017.png);
  19. background-repeat: no-repeat;
  20. background-position-y: -785px;
  21. width: 34px;
  22. height: 30px;
  23. }
  24.  
  25. .member_active {
  26. color: green;
  27. }
  28. .member_away {
  29. color: red;
  30. }
  31. `)
  32.  
  33. let faction = window.location.search.split('ID=')[1] || ''
  34.  
  35. const get_api = async (fac = faction, key = apiKey) => {
  36. const response = await fetch(`https://api.torn.com/faction/${faction}$?selections=basic&key=${key}`)
  37. return await response.json()
  38. }
  39.  
  40. const toggleLastAction = (iconsTitle, memberUL) => {
  41. if (iconsTitle.innerText === 'Icons') {
  42. iconsTitle.childNodes[0].nodeValue = 'Last Action'
  43. get_api().then((res) => {
  44. for (const li of memberUL.children) {
  45. const lastActionDIV = li.querySelector('.last-action')
  46. const memberID = lastActionDIV.getAttribute('data-member-ID')
  47. const lastAction = res.members[memberID].last_action
  48. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  49. lastActionDIV.innerText = lastAction
  50. if (lastAction.includes('minutes') && parseInt(lastAction.split(' ')[0]) < 10) lastActionDIV.classList.add('member_active')
  51. if (lastAction.includes('day')) lastActionDIV.classList.add('member_away')
  52. lastActionDIV.classList.toggle('hide')
  53. }
  54. })
  55. }
  56. else {
  57. iconsTitle.childNodes[0].nodeValue = 'Icons'
  58. for (const li of memberUL.children) {
  59. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  60. li.querySelector('.last-action').classList.toggle('hide')
  61. }
  62. }
  63. }
  64.  
  65. const add_toggle = (node) => {
  66. const iconsTitle = node.querySelector('.title .member-icons')
  67. const memberUL = node.querySelector('.member-list')
  68. iconsTitle.insertAdjacentHTML('beforeend', `<i class="last_action_icon right"></i>`)
  69. node.querySelector('.last_action_icon').addEventListener('click', () => { toggleLastAction(iconsTitle, memberUL) })
  70. for (const li of memberUL.children) {
  71. const memberID = li.querySelector('.kick-yes').getAttribute('data-id')
  72. li.querySelector('.member-icons #iconTray').insertAdjacentHTML('afterend', `<div class="last-action hide" data-member-id="${memberID}"></div>`)
  73. }
  74. }
  75.  
  76. const observer = new MutationObserver((mutations) => {
  77. for (const mutation of mutations) {
  78. for (const node of mutation.addedNodes) {
  79. if (node.className && node.querySelector('.f-war-list')) {
  80. add_toggle(node)
  81. }
  82. }
  83. }
  84. })
  85.  
  86. const otherFactionUL = document.querySelector('.f-war-list')
  87. if (otherFactionUL) {
  88. add_toggle(otherFactionUL)
  89. }
  90. else {
  91. const wrapper = document.querySelector('#factions')
  92. observer.observe(wrapper, { subtree: true, childList: true })
  93. }
  94.  
  95.