Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Faction Last Active
  3. // @namespace namespace
  4. // @version 0.7
  5. // @description description
  6. // @author tos
  7. // @match *.torn.com/factions.php*
  8. // @run-at document-end
  9. // @grant GM_addStyle
  10. // ==/UserScript==
  11.  
  12. const apiKey = 'PUT YOUR API KEY HERE dont remove the apostrophes'
  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. .member_active {
  27. color: green;
  28. }
  29. .member_idle {
  30. color: #ff7c23;
  31. }
  32. .member_away {
  33. color: red;
  34. font-weight: bold;
  35. }
  36. `)
  37.  
  38. let faction = window.location.search.split('ID=')[1] || ''
  39.  
  40. const get_api = async (fac = faction, key = apiKey) => {
  41. const response = await fetch(`https://api.torn.com/faction/${faction}$?selections=basic&key=${key}`)
  42. return await response.json()
  43. }
  44.  
  45. const toggleLastAction = (iconsTitle, memberUL) => {
  46. if (iconsTitle.innerText === 'Icons') {
  47. iconsTitle.childNodes[0].nodeValue = 'Last Action'
  48. get_api().then((res) => {
  49. if (res.error && res.error.code === 2) alert('Invalid API key in Faction Last Action script. Please update in line 11.')
  50. for (const li of memberUL.children) {
  51. const lastActionDIV = li.querySelector('.last-action')
  52. const memberID = lastActionDIV.getAttribute('data-member-ID')
  53. const lastAction = res.members[memberID].last_action
  54. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  55. lastActionDIV.innerText = lastAction
  56. if (lastAction.includes('minute') && parseInt(lastAction.split(' ')[0]) <= 10) lastActionDIV.classList.add('member_active')
  57. if (lastAction.includes('day') && parseInt(lastAction.split(' ')[0]) < 7) lastActionDIV.classList.add('member_idle')
  58. if (lastAction.includes('day') && parseInt(lastAction.split(' ')[0]) >= 7) lastActionDIV.classList.add('member_away')
  59. lastActionDIV.classList.toggle('hide')
  60. }
  61. })
  62. }
  63. else {
  64. iconsTitle.childNodes[0].nodeValue = 'Icons'
  65. for (const li of memberUL.children) {
  66. li.querySelector('.member-icons #iconTray').classList.toggle('hide')
  67. li.querySelector('.last-action').classList.toggle('hide')
  68. }
  69. }
  70. }
  71.  
  72. const add_toggle = (node) => {
  73. const iconsTitle = node.querySelector('.title .member-icons')
  74. const memberUL = node.querySelector('.member-list')
  75. iconsTitle.insertAdjacentHTML('beforeend', `<i class="last_action_icon right"></i>`)
  76. node.querySelector('.last_action_icon').addEventListener('click', () => { toggleLastAction(iconsTitle, memberUL) })
  77. for (const li of memberUL.children) {
  78. const memberID = li.querySelector('.kick-yes').getAttribute('data-id')
  79. li.querySelector('.member-icons #iconTray').insertAdjacentHTML('afterend', `<div class="last-action hide" data-member-id="${memberID}"></div>`)
  80. }
  81. }
  82.  
  83. const observer = new MutationObserver((mutations) => {
  84. for (const mutation of mutations) {
  85. for (const node of mutation.addedNodes) {
  86. if (node.className && node.querySelector('.f-war-list')) {
  87. add_toggle(node)
  88. }
  89. }
  90. }
  91. })
  92.  
  93. const otherFactionUL = document.querySelector('.f-war-list')
  94. if (otherFactionUL) {
  95. add_toggle(otherFactionUL)
  96. }
  97. else {
  98. const wrapper = document.querySelector('#factions')
  99. observer.observe(wrapper, { subtree: true, childList: true })
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement