Faction Last Active

description

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

  1. // ==UserScript==
  2. // @name Faction Last Active
  3. // @namespace namespace
  4. // @version 0.2
  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.  
  26. const get_api = async (key = apiKey) => {
  27. const response = await fetch(`https://api.torn.com/faction/$?selections=basic&key=${key}`)
  28. return await response.json()
  29. }
  30.  
  31. const toggleLastAction = (iconsTitle, memberUL) => {
  32. if (iconsTitle.innerText === 'Icons') {
  33. iconsTitle.childNodes[0].nodeValue = 'Last Action'
  34. get_api().then((res) => {
  35. for (const li of memberUL.children) {
  36. const lastActionDIV = li.querySelector('.last-action')
  37. const memberID = lastActionDIV.getAttribute('data-member-ID')
  38. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  39. lastActionDIV.innerText = res.members[memberID].last_action
  40. lastActionDIV.classList.toggle('hide')
  41. }
  42. })
  43. }
  44. else {
  45. iconsTitle.childNodes[0].nodeValue = 'Icons'
  46. for (const li of memberUL.children) {
  47. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  48. li.querySelector('.last-action').classList.toggle('hide')
  49. }
  50. }
  51. }
  52.  
  53. const observer = new MutationObserver((mutations) => {
  54. for (const mutation of mutations) {
  55. for (const node of mutation.addedNodes) {
  56. if (node.className && node.querySelector('.f-war-list')) {
  57. const iconsTitle = node.querySelector('.title .member-icons')
  58. const memberUL = node.querySelector('.member-list')
  59. iconsTitle.insertAdjacentHTML('beforeend', `<i class="last_action_icon right"></i>`)
  60. node.querySelector('.last_action_icon').addEventListener('click', () => { toggleLastAction(iconsTitle, memberUL) })
  61. for (const li of memberUL.children) {
  62. const memberID = li.querySelector('.kick-yes').getAttribute('data-id')
  63. li.querySelector('.member-icons #iconTray').insertAdjacentHTML('afterend', `<div class="last-action hide" data-member-id="${memberID}"></div>`)
  64. }
  65. }
  66. }
  67. }
  68. })
  69.  
  70. const wrapper = document.querySelector('#factions')
  71. observer.observe(wrapper, { subtree: true, childList: true })