· 6 years ago · Nov 06, 2019, 10:54 PM
1// ==UserScript==
2// @name Faction Last Active
3// @namespace namespace
4// @version 0.0.5
5// @description Faction Last Active script written by tos (branch off of 0.8. Source: https://greasyfork.org/en/scripts/370102-faction-last-active)
6// @author Helcostr [1934501] (maintainer), tos [1976582], LordBusiness [2052465]
7// @connect api.torn.com
8// @match *.torn.com/factions.php*
9// @run-at document-end
10// @grant GM_addStyle
11// @grant GM_xmlhttpRequest
12// ==/UserScript==
13
14const apiKey = 'hUDHtnW65qypjp07'
15
16GM_addStyle(`
17 .last_action_icon {
18 cursor: pointer;
19 vertical-align: middle;
20 display: inline-block;
21 background-image: url(/images/v2/sidebar_icons_desktop_2017.png);
22 background-repeat: no-repeat;
23 background-position-y: -785px;
24 width: 34px;
25 height: 30px;
26 }
27
28 .member-list.hide-icons > li .member-icons > #iconTray,
29 .member-list:not(.hide-icons) > li .member-icons > .last-action {
30 display: none !important;
31 }
32`)
33
34const factionInfoWrap = document.querySelector('.faction-info'),
35 factionID = factionInfoWrap ? factionInfoWrap.getAttribute('data-faction') : ''
36
37const myFetch = url => {
38 return new Promise((resolve, reject) => {
39 let ret = GM_xmlhttpRequest({
40 method: 'GET',
41 url: url,
42 responseType: 'json',
43 onload: response => resolve(response.response),
44 onerror: err => reject(err)
45 })
46 })
47}
48
49const get_api = async (fac = factionID, key = apiKey) => {
50 return await myFetch(`https://api.torn.com/faction/${fac}?selections=basic&key=${key}`)
51}
52
53const toggleLastAction = (iconsTitle, memberUL) => {
54 if (iconsTitle.innerText === 'Icons') {
55 iconsTitle.childNodes[0].nodeValue = 'Last Action'
56 get_api().then(res => {
57 if (res.error && res.error.code === 2) alert(`Invalid API key found in ${GM.info.script.name} script. Please update it on line 14.`)
58 for (const li of memberUL.children) {
59 const lastActionDIV = li.querySelector('.last-action'),
60 memberID = lastActionDIV.getAttribute('data-member-ID'),
61 lastAction = res.members[memberID].last_action
62 lastActionDIV.innerText = lastAction
63 if (lastAction.includes('minute')) lastActionDIV.classList.add('ftGreen')
64 else if (lastAction.includes('hour')) lastActionDIV.classList.add(parseInt(lastAction.split(' ')[0]) < 12 ? 't-green' : 'ftGold')
65 else if (lastAction.includes('day')) lastActionDIV.classList.add(parseInt(lastAction.split(' ')[0]) < 7 ? 'ftDarkGold' : 't-red')
66 }
67 })
68 .catch(err => console.log(err))
69 } else {
70 iconsTitle.childNodes[0].nodeValue = 'Icons'
71 }
72 memberUL.classList.toggle('hide-icons')
73}
74
75const add_toggle = node => {
76 const iconsTitle = node.querySelector('.title .member-icons'),
77 memberUL = node.querySelector('.member-list')
78 iconsTitle.insertAdjacentHTML('beforeend', `<i class="last_action_icon right"></i>`)
79 node.querySelector('.last_action_icon').addEventListener('click', () => { toggleLastAction(iconsTitle, memberUL) })
80 for (const li of memberUL.children) {
81 const memberID = /XID=(\d+)/.exec(li.querySelector('.user.name').getAttribute('href'))[1]
82 li.querySelector('.member-icons #iconTray').insertAdjacentHTML('afterend', `<div class="last-action" data-member-id="${memberID}"></div>`)
83 }
84}
85
86const observer = new MutationObserver(mutations => {
87 for (const mutation of mutations) {
88 for (const node of mutation.addedNodes) {
89 if (node.className && node.querySelector('.f-war-list')) {
90 add_toggle(node)
91 }
92 }
93 }
94})
95
96const otherFactionUL = document.querySelector('.f-war-list')
97if (otherFactionUL) {
98 add_toggle(otherFactionUL)
99} else {
100 const wrapper = document.getElementById('factions')
101 observer.observe(wrapper, { subtree: true, childList: true })
102}