Faction Last Active

description

目前为 2018-07-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Faction Last Active
  3. // @namespace namespace
  4. // @version 0.6
  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_idle {
  29. color: #ff7c23;
  30. }
  31. .member_away {
  32. color: red;
  33. font-weight: bold;
  34. }
  35. `)
  36.  
  37. let faction = window.location.search.split('ID=')[1] || ''
  38.  
  39. const get_api = async (fac = faction, key = apiKey) => {
  40. const response = await fetch(`https://api.torn.com/faction/${faction}$?selections=basic&key=${key}`)
  41. return await response.json()
  42. }
  43.  
  44. const toggleLastAction = (iconsTitle, memberUL) => {
  45. if (iconsTitle.innerText === 'Icons') {
  46. iconsTitle.childNodes[0].nodeValue = 'Last Action'
  47. get_api().then((res) => {
  48. if (res.error && res.error.code === 2) alert('Invalid API key in Faction Last Action script. Please update in line 11.')
  49. for (const li of memberUL.children) {
  50. const lastActionDIV = li.querySelector('.last-action')
  51. const memberID = lastActionDIV.getAttribute('data-member-ID')
  52. const lastAction = res.members[memberID].last_action
  53. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  54. lastActionDIV.innerText = lastAction
  55. if (lastAction.includes('minute') && parseInt(lastAction.split(' ')[0]) <= 10) lastActionDIV.classList.add('member_active')
  56. if (lastAction.includes('day') && parseInt(lastAction.split(' ')[0]) < 7) lastActionDIV.classList.add('member_idle')
  57. if (lastAction.includes('day') && parseInt(lastAction.split(' ')[0]) >= 7) lastActionDIV.classList.add('member_away')
  58. lastActionDIV.classList.toggle('hide')
  59. }
  60. })
  61. }
  62. else {
  63. iconsTitle.childNodes[0].nodeValue = 'Icons'
  64. for (const li of memberUL.children) {
  65. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  66. li.querySelector('.last-action').classList.toggle('hide')
  67. }
  68. }
  69. }
  70.  
  71. const add_toggle = (node) => {
  72. const iconsTitle = node.querySelector('.title .member-icons')
  73. const memberUL = node.querySelector('.member-list')
  74. iconsTitle.insertAdjacentHTML('beforeend', `<i class="last_action_icon right"></i>`)
  75. node.querySelector('.last_action_icon').addEventListener('click', () => { toggleLastAction(iconsTitle, memberUL) })
  76. for (const li of memberUL.children) {
  77. const memberID = li.querySelector('.kick-yes').getAttribute('data-id')
  78. li.querySelector('.member-icons #iconTray').insertAdjacentHTML('afterend', `<div class="last-action hide" data-member-id="${memberID}"></div>`)
  79. }
  80. }
  81.  
  82. const observer = new MutationObserver((mutations) => {
  83. for (const mutation of mutations) {
  84. for (const node of mutation.addedNodes) {
  85. if (node.className && node.querySelector('.f-war-list')) {
  86. add_toggle(node)
  87. }
  88. }
  89. }
  90. })
  91.  
  92. const otherFactionUL = document.querySelector('.f-war-list')
  93. if (otherFactionUL) {
  94. add_toggle(otherFactionUL)
  95. }
  96. else {
  97. const wrapper = document.querySelector('#factions')
  98. observer.observe(wrapper, { subtree: true, childList: true })
  99. }
  100.  
  101.