TornAPI Quick Fill

Prefill API key and check 'pretty' radio buttons.

当前为 2019-03-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TornAPI Quick Fill
  3. // @namespace TornTos
  4. // @version 2.0
  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 *.api.torn.com/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. const keyup = new Event('keyup')
  14.  
  15. GM_addStyle(`
  16. span.click_select {
  17. cursor: pointer;
  18. }
  19. `)
  20.  
  21. const sections = {
  22. u: `https://api.torn.com/user/?selections=lookup&key=`,
  23. p: `https://api.torn.com/property/?selections=lookup&key=`,
  24. f: `https://api.torn.com/faction/?selections=lookup&key=`,
  25. c: `https://api.torn.com/company/?selections=lookup&key=`,
  26. i: `https://api.torn.com/market/?selections=lookup&key=`,
  27. t: `https://api.torn.com/torn/?selections=lookup&key=`
  28. }
  29.  
  30. const fill_selections = async () => {
  31. for (const s in sections) {
  32. const res = await fetch(sections[s]+APIkey).then(r => r.json())
  33. 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>`
  34. }
  35. document.querySelectorAll('span.click_select').forEach((span) => {
  36. span.addEventListener('click', (e) => {
  37. const panel = e.target.closest('div.panel-group')
  38. const selections_input = panel.querySelector('input[id*=selections]')
  39. if (e.ctrlKey) {
  40. if (selections_input.value === '') selections_input.value = e.target.innerText
  41. else selections_input.value += ','+e.target.innerText
  42. }
  43. else {
  44. selections_input.value = e.target.innerText
  45. panel.querySelector('BUTTON').click()
  46. }
  47. selections_input.dispatchEvent(keyup)
  48. })
  49. })
  50. }
  51.  
  52. (()=>{
  53. document.getElementById('documentation').style.display = 'none'
  54. document.getElementById('demo').style.display = 'block'
  55. $('#api_key').unbind('focusout')
  56. $('.updateURL').unbind('keyup')
  57. const api_key_input = document.getElementById('api_key')
  58. api_key_input.value = localStorage.getItem('x_apikey') || ''
  59. api_key_input.insertAdjacentHTML('afterend', `<button id="save_api_key">Save</button>`)
  60. const save_button = document.querySelector('#save_api_key')
  61. save_button.style.margin = '0em 0.5em'
  62. save_button.addEventListener('click', (e) => {
  63. const apikey = api_key_input.value
  64. localStorage.setItem('x_apikey', apikey)
  65. })
  66. document.querySelectorAll('input[type=radio][value=pretty]').forEach((radio) => {radio.checked = true})
  67. document.querySelectorAll('input[id*=id],input[id*=selections]').forEach((input) => {
  68. input.addEventListener('keyup', (e)=> {
  69. const panel = e.target.closest('div.panel-group')
  70. const url_shown = document.querySelector(`#${e.target.getAttribute('data-field')}url`)
  71. const url_split = url_shown.innerText.split('/')
  72. url_split[4] = `${panel.querySelector('input[id*=id]').value}?selections=${panel.querySelector('input[id*=selections]').value}&key=`
  73. url_shown.innerText = url_split.join('/')
  74. })
  75. })
  76. fill_selections()
  77. })();