TornAPI Quick Fill

Prefill API key and check 'pretty' radio buttons.

  1. // ==UserScript==
  2. // @name TornAPI Quick Fill
  3. // @namespace TornTos
  4. // @version 2.3
  5. // @description Prefill API key and check 'pretty' radio buttons.
  6. // Makes available fields clickable and fires try it button when clicked.
  7. // Ctrl-Click to add multiple selections, try-it button will need to be click manually with this method.
  8. // @author tos
  9. // @match *.torn.com/api.html*
  10. // @grant GM_addStyle
  11. // ==/UserScript==
  12.  
  13. const keyup = new Event('keyup')
  14. let APIkey = localStorage.getItem('x_apikey') || ''
  15.  
  16. GM_addStyle(`
  17. span.click_select {
  18. cursor: pointer;
  19. }
  20. #save_api_key {
  21. margin: 0em 0.5em;
  22. }
  23. `)
  24.  
  25. const sections = {
  26. u: `https://api.torn.com/user/?selections=lookup&key=`,
  27. p: `https://api.torn.com/property/?selections=lookup&key=`,
  28. f: `https://api.torn.com/faction/?selections=lookup&key=`,
  29. c: `https://api.torn.com/company/?selections=lookup&key=`,
  30. i: `https://api.torn.com/market/?selections=lookup&key=`,
  31. t: `https://api.torn.com/torn/?selections=lookup&key=`
  32. }
  33.  
  34. const fill_selections = async () => {
  35. for (const s in sections) {
  36. const res = await fetch(sections[s]+APIkey).then(r => r.json())
  37. document.querySelector(`p.${s}_fields`).innerHTML = `<small><strong>Available fields: </strong><span class="click_select">${res.selections.join('</span>, <span class="click_select">')}</span></small>`
  38. }
  39. document.querySelectorAll('span.click_select').forEach((span) => {
  40. span.addEventListener('click', (e) => {
  41. const panel = e.target.closest('div.panel-group')
  42. const selections_input = panel.querySelector('input[id*=selections]')
  43. if (e.ctrlKey) {
  44. if (selections_input.value === '') selections_input.value = e.target.innerText
  45. else selections_input.value += ','+e.target.innerText
  46. }
  47. else {
  48. selections_input.value = e.target.innerText
  49. panel.querySelector('BUTTON').click()
  50. }
  51. selections_input.dispatchEvent(keyup)
  52. })
  53. })
  54. }
  55.  
  56.  
  57. document.getElementById('documentation').style.display = 'none'
  58. document.getElementById('demo').style.display = 'block'
  59. $('#api_key').unbind('focusout')
  60. $('.updateURL').unbind('keyup')
  61. const api_key_input = document.getElementById('api_key')
  62. api_key_input.value = APIkey
  63. api_key_input.insertAdjacentHTML('afterend', `<button id="save_api_key">Save</button>`)
  64. const save_button = document.querySelector('#save_api_key')
  65. save_button.addEventListener('click', (e) => {
  66. APIkey = api_key_input.value
  67. localStorage.setItem('x_apikey', APIkey)
  68. })
  69. document.querySelectorAll('input[type=radio][value=pretty]').forEach((radio) => {radio.checked = true})
  70. document.querySelectorAll('input[id*=id],input[id*=selections]').forEach((input) => {
  71. input.addEventListener('keyup', (e)=> {
  72. const panel = e.target.closest('div.panel-group')
  73. const url_shown = document.querySelector(`#${e.target.getAttribute('data-field')}url`)
  74. const url_split = url_shown.innerText.split('/')
  75. url_split[4] = `${panel.querySelector('input[id*=id]').value}?selections=${panel.querySelector('input[id*=selections]').value}&key=`
  76. url_shown.innerText = url_split.join('/')
  77. })
  78. })
  79.  
  80. document.querySelectorAll('BUTTON.btn').forEach(try_it_button => try_it_button.addEventListener('click', (e) => {
  81. e.target.parentElement.querySelector('div[class$=_result]').innerHTML = '' //fix for site adding a new panel every request
  82. }))
  83.  
  84. fill_selections()