🏠 Home 

Search_Steam_Store

搜索Steam商店,返回[{ appID, isBundle, appName, appPrice, appUrl, appImg }]

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/431430/964232/Search_Steam_Store.js

  1. // ==UserScript==
  2. // @name Search_Steam_Store
  3. // @namespace https://blog.chrxw.com
  4. // @version 1.1
  5. // @description 搜索Steam商店,返回[{ appID, isBundle, appName, appPrice, appUrl, appImg }]
  6. // @author Chr_
  7. // ==/UserScript==
  8. //异步检索steam商店
  9. function searchStore(keywords, cc = 'CN') {
  10. return new Promise((resolve, reject) => {
  11. $http.getText(`https://store.steampowered.com/search/suggest?term=${keywords}&f=games&cc=${cc}&realm=1&l=schinese`)
  12. .then((text) => {
  13. const vdom = document.createElement('div');
  14. vdom.style.display = 'none';
  15. vdom.innerHTML = text;
  16. const r###lt = [];
  17. vdom.querySelectorAll('a').forEach((ele) => {
  18. const isBundle = ele.hasAttribute('data-ds-bundleid');
  19. const appID = parseInt(ele.getAttribute(isBundle ? 'data-ds-bundleid' : 'data-ds-appid'));
  20. const appUrl = ele.getAttribute('href');
  21. const appName = ele.querySelector('div.match_name').textContent;
  22. const appImg = ele.querySelector('div.match_img>img').getAttribute('src');
  23. const appPrice = ele.querySelector('div.match_price').textContent;
  24. r###lt.push({ appID, isBundle, appName, appPrice, appUrl, appImg });
  25. });
  26. resolve(r###lt);
  27. })
  28. .catch((reason) => {
  29. console.error(reason);
  30. reject(reason);
  31. });
  32. });
  33. }
  34. //==============================================================
  35. class Request {
  36. constructor(timeout = 3000) {
  37. this.timeout = timeout;
  38. }
  39. get(url, opt = {}) {
  40. return this.baseRequest(url, 'GET', opt, 'json');
  41. }
  42. getHtml(url, opt = {}) {
  43. return this.baseRequest(url, 'GET', opt, '');
  44. }
  45. getText(url, opt = {}) {
  46. return this.baseRequest(url, 'GET', opt, 'text');
  47. }
  48. post(url, data, opt = {}) {
  49. opt.data = JSON.stringify(data);
  50. return this.baseRequest(url, 'POST', opt, 'json');
  51. }
  52. baseRequest(url, method = 'GET', opt = {}, responseType = 'json') {
  53. Object.assign(opt, {
  54. url, method, responseType, timeout: this.timeout
  55. });
  56. return new Promise((resolve, reject) => {
  57. opt.ontimeout = opt.onerror = reject;
  58. opt.onload = ({ readyState, status, response, responseText }) => {
  59. if (readyState === 4 && status === 200) {
  60. if (responseType == 'json') {
  61. resolve(response);
  62. } else if (responseType == 'text') {
  63. resolve(responseText);
  64. }
  65. } else {
  66. console.error('网络错误');
  67. console.log(readyState);
  68. console.log(status);
  69. console.log(response);
  70. reject('解析出错');
  71. }
  72. }
  73. GM_xmlhttpRequest(opt);
  74. });
  75. }
  76. }
  77. const $http = new Request();