Faction Last Active

description

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

  1. // ==UserScript==
  2. // @name Faction Last Active
  3. // @namespace namespace
  4. // @version 0.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.  
  14. GM_addStyle(`
  15. .last_action_icon {
  16. cursor: pointer;
  17. vertical-align: middle;
  18. display: inline-block;
  19. background-image: url(/images/v2/sidebar_icons_desktop_2017.png);
  20. background-repeat: no-repeat;
  21. background-position-y: -785px;
  22. width: 34px;
  23. height: 30px;
  24. }
  25. `)
  26.  
  27. const get_api = async (key = apiKey) => {
  28. const response = await fetch(`https://api.torn.com/faction/$?selections=basic&key=${key}`)
  29. return await response.json()
  30. }
  31.  
  32. const toggleLastAction = (iconsTitle, memberUL) => {
  33. if (iconsTitle.innerText === 'Icons') {
  34. iconsTitle.childNodes[0].nodeValue = 'Last Action'
  35. get_api().then((res) => {
  36. for (const li of memberUL.children) {
  37. const lastActionDIV = li.querySelector('.last-action')
  38. const memberID = lastActionDIV.getAttribute('data-member-ID')
  39. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  40. lastActionDIV.innerText = res.members[memberID].last_action
  41. lastActionDIV.classList.toggle('hide')
  42. }
  43. })
  44. }
  45. else {
  46. iconsTitle.childNodes[0].nodeValue = 'Icons'
  47. for (const li of memberUL.children) {
  48. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  49. li.querySelector('.last-action').classList.toggle('hide')
  50. }
  51. }
  52. }
  53.  
  54. const observer = new MutationObserver((mutations) => {
  55. for (const mutation of mutations) {
  56. for (const node of mutation.addedNodes) {
  57. if (node.className && node.className === 'faction-info-wrap another-faction') {
  58. const iconsTitle = node.querySelector('.title .member-icons')
  59. const memberUL = node.querySelector('.member-list')
  60. iconsTitle.insertAdjacentHTML('beforeend', `<i class="last_action_icon right"></i>`)
  61. node.querySelector('.last_action_icon').addEventListener('click', () => { toggleLastAction(iconsTitle, memberUL) })
  62. for (const li of memberUL.children) {
  63. const memberID = li.querySelector('.kick-yes').getAttribute('data-id')
  64. li.querySelector('.member-icons #iconTray').insertAdjacentHTML('afterend', `<div class="last-action hide" data-member-id="${memberID}"></div>`)
  65. }
  66. }
  67. }
  68. }
  69. })
  70.  
  71. const wrapper = document.querySelector('#factions')
  72. observer.observe(wrapper, { subtree: true, childList: true })