BLAEO API

Useful library for communicating with the BLAEO API.

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/406057/823678/BLAEO%20API.js

  1. // ==UserScript==
  2. // @name BLAEO API
  3. // @namespace https://rafaelgssa.gitlab.io/monkey-scripts
  4. // @version 2.1.5
  5. // @author rafaelgssa
  6. // @description Useful library for communicating with the BLAEO API.
  7. // @match *://*/*
  8. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  9. // @require https://gf.qytechs.cn/scripts/405813-monkey-utils/code/Monkey%20Utils.js
  10. // @require https://gf.qytechs.cn/scripts/405802-monkey-dom/code/Monkey%20DOM.js
  11. // @require https://gf.qytechs.cn/scripts/405822-monkey-requests/code/Monkey%20Requests.js
  12. // @grant GM.xmlHttpRequest
  13. // @grant GM_xmlhttpRequest
  14. // ==/UserScript==
  15.  
  16. /* global Requests */
  17.  
  18. /**
  19. * @typedef {Object} UserIdentification
  20. * @property {string} [steamId]
  21. * @property {string} [username]
  22. *
  23. * @typedef {BlaeoUser} GetUserResponse
  24. *
  25. * @typedef {Object} BlaeoUser
  26. * @property {string} id
  27. * @property {string} display_name
  28. * @property {string} steamgifts_name
  29. * @property {string} steam_name
  30. * @property {string} steam_id
  31. * @property {BlaeoUserStatistics} statistics
  32. *
  33. * @typedef {Object} BlaeoUserStatistics
  34. * @property {number} games
  35. * @property {number} uncategorized
  36. * @property {number} never_played
  37. * @property {number} unfinished
  38. * @property {number} beaten
  39. * @property {number} completed
  40. * @property {number} wont_play
  41. * @property {number} categorized
  42. *
  43. * @typedef {Object} GetGameResponse
  44. * @property {string} owner
  45. * @property {Omit<BlaeoGame, 'id'>} game
  46. *
  47. * @typedef {Object} GetGamesResponse
  48. * @property {BlaeoGame[]} games
  49. *
  50. * @typedef {Object} BlaeoGame
  51. * @property {string} id
  52. * @property {number} steam_id
  53. * @property {string} name
  54. * @property {number} playtime
  55. * @property {BlaeoGameProgress} [progress]
  56. * @property {BlaeoGameAchievements} [achievements]
  57. *
  58. * @typedef {'uncategorized' | 'never played' | 'unfinished' | 'beaten' | 'completed' | 'wont play'} BlaeoGameProgress
  59. *
  60. * @typedef {Object} BlaeoGameAchievements
  61. * @property {number} unlocked
  62. * @property {number} total
  63. *
  64. * @typedef {Object} GetRecentlyPlayedResponse
  65. * @property {BlaeoRecentlyPlayed[]} playtimes
  66. *
  67. * @typedef {Object} BlaeoRecentlyPlayed
  68. * @property {number} steam_id
  69. * @property {string} name
  70. * @property {number} minutes
  71. * @property {BlaeoGameProgress} progress_before
  72. * @property {BlaeoGameProgress} progress_now
  73. */
  74.  
  75. // eslint-disable-next-line
  76. const BlaeoApi = (() => {
  77. const BLAEO_URL = 'https://www.backlog-assassins.net';
  78.  
  79. let _xcsrfToken = '';
  80. let _authenticityToken = '';
  81. let _utf8 = '';
  82.  
  83. /**
  84. * Initializes the API.
  85. */
  86. const init = () => {
  87. const xcsrfTokenField = /** @type {HTMLMetaElement | null} */ (document.querySelector(
  88. '[name="csrf-token"]'
  89. ));
  90. const authenticityTokenField = /** @type {HTMLInputElement | null} */ (document.querySelector(
  91. '[name="authenticity_token"]'
  92. ));
  93. const utf8Field = /** @type {HTMLInputElement | null} */ (document.querySelector(
  94. '[name="utf8"]'
  95. ));
  96. if (xcsrfTokenField) {
  97. _xcsrfToken = xcsrfTokenField.content;
  98. }
  99. if (authenticityTokenField) {
  100. _authenticityToken = authenticityTokenField.value;
  101. }
  102. if (utf8Field) {
  103. _utf8 = utf8Field.value;
  104. }
  105. };
  106.  
  107. /**
  108. * Retrieves information about a user.
  109. * @param {UserIdentification} identification The identification of the user.
  110. * @returns {Promise<BlaeoUser | undefined>} The information about the user, if successful.
  111. */
  112. const getUser = async (identification) => {
  113. const userPath = _getUserPath(identification);
  114. const response = /** @type {ExtendedResponse<GetUserResponse>} */ (await Requests.GET(
  115. `${BLAEO_URL}/users/${userPath}.json`
  116. ));
  117. return response.json;
  118. };
  119.  
  120. /**
  121. * Retrieves information about a user's game.
  122. * @param {UserIdentification} identification The identification of the user.
  123. * @param {number} id The Steam ID of the game.
  124. * @returns {Promise<Omit<BlaeoGame, 'id'> | undefined>} The information about the game, if successful.
  125. */
  126. const getGame = async (identification, id) => {
  127. const userPath = _getUserPath(identification);
  128. const response = /** @type {ExtendedResponse<GetGameResponse>} */ (await Requests.GET(
  129. `${BLAEO_URL}/users/${userPath}/games/${id}.json`,
  130. {
  131. headers: {
  132. Accept: 'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8',
  133. },
  134. }
  135. ));
  136. return response.json && response.json.game;
  137. };
  138.  
  139. /**
  140. * Retrieves a user's games.
  141. * @param {UserIdentification} identification The identification of the user.
  142. * @returns {Promise<BlaeoGame[] | undefined>} The user's games, if successful.
  143. */
  144. const getGames = async (identification) => {
  145. const userPath = _getUserPath(identification);
  146. const response = /** @type {ExtendedResponse<GetGamesResponse>} */ (await Requests.GET(
  147. `${BLAEO_URL}/users/${userPath}/games.json`
  148. ));
  149. return response.json && response.json.games;
  150. };
  151.  
  152. /**
  153. * Retrieves a user's recently played games.
  154. * @param {UserIdentification} identification The identification of the user.
  155. * @returns {Promise<BlaeoRecentlyPlayed[] | undefined>} The user's recently played games, if successful.
  156. */
  157. const getRecentlyPlayed = async (identification) => {
  158. const userPath = _getUserPath(identification);
  159. const response = /** @type {ExtendedResponse<GetRecentlyPlayedResponse>} */ (await Requests.GET(
  160. `${BLAEO_URL}/users/${userPath}/played.json`
  161. ));
  162. return response.json && response.json.playtimes;
  163. };
  164.  
  165. /**
  166. * Searches for games in a user's library.
  167. * @param {UserIdentification} identification The identification of the user.
  168. * @param {string} query The query string to search for.
  169. * @returns {Promise<HTMLUListElement | undefined>} An element containing the list of matches, if successful.
  170. */
  171. const searchGames = async (identification, query) => {
  172. const userPath = _getUserPath(identification);
  173. const response = await Requests.GET(`${BLAEO_URL}/users/${userPath}/games/filter?q=${query}`);
  174. if (!response.dom) {
  175. return;
  176. }
  177. const listEl = /** @type {HTMLUListElement | null} */ (response.dom.querySelector('.games'));
  178. return listEl || undefined;
  179. };
  180.  
  181. /**
  182. * Previews a markdown post for a user.
  183. * @param {UserIdentification} identification The identification of the user.
  184. * @param {string} markdown The markdown string of the post.
  185. * @returns {Promise<HTMLDivElement | undefined>} An element containing the preview, if successful.
  186. */
  187. const previewPost = async (identification, markdown) => {
  188. const userPath = _getUserPath(identification);
  189. const response = await Requests.POST(`${BLAEO_URL}/posts/preview.${userPath}`, {
  190. headers: {
  191. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  192. 'X-CSRF-Token': _xcsrfToken,
  193. },
  194. body: Requests.convertToQuery({
  195. authenticity_token: _authenticityToken,
  196. utf8: _utf8,
  197. 'post[text]': encodeURIComponent(markdown),
  198. }),
  199. });
  200. if (!response.dom) {
  201. return;
  202. }
  203. const previewEl = /** @type {HTMLDivElement | null} */ (response.dom.querySelector(
  204. '.markdown'
  205. ));
  206. return previewEl || undefined;
  207. };
  208.  
  209. /**
  210. * Returns the path for a user based on the identification method.
  211. * @param {UserIdentification} identification The identification of the user.
  212. * @returns {string} The path for the user.
  213. */
  214. const _getUserPath = ({ steamId, username }) => {
  215. return steamId ? `+${steamId}` : username;
  216. };
  217.  
  218. return {
  219. BLAEO_URL,
  220. init,
  221. getUser,
  222. getGame,
  223. getGames,
  224. getRecentlyPlayed,
  225. searchGames,
  226. previewPost,
  227. };
  228. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址