SWH_Helper

从steamdb.keylol.com获取愿望单数据

当前为 2020-12-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name SWH_Helper
  3. // @namespace https://blog.chrxw.com
  4. // @version 1.0
  5. // @description 从steamdb.keylol.com获取愿望单数据
  6. // @author Chr_
  7. // @match https://swh.chrxw.com/
  8. // @match http://localhost/
  9. // @match https://steamdb.keylol.com/*
  10. // @connect steamdb.keylol.com
  11. // @connect swh.chrxw.com
  12. // @connect swh.chrxw.cn
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // @grant GM_xmlhttpRequest
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20. var serverdic = {
  21. 'ali': ["阿里云1', 'swh.chrxw.cn:20443"],
  22. 'ali2': ["阿里云2', 'swh2.chrxw.cn:20443"],
  23. }
  24. var review = ["评测不足', '差评如潮', '特别差评', '差评', '多半差评', '褒贬不一', '多半好评', '好评', '特别好评', '好评如潮', '好评如潮"];
  25. var infodict;
  26. var wishlist;
  27. var wishstr;
  28. var server;
  29. var tmp;
  30.  
  31. if (window.location.host == 'steamdb.keylol.com') {
  32. wishstr = window.localStorage.getItem('wish');
  33. GM_setValue('wish', wishstr);
  34. var nav = document.getElementsByClassName('nav')[0];
  35. var button = document.createElement('li');
  36. button.setAttribute('class', 'nav-swh');
  37. button.setAttribute('class', 'nav-swh');
  38. var link = document.createElement('a');
  39. link.setAttribute('href', 'https://swh.chrxw.com')
  40. link.text = '愿望单助手';
  41. button.appendChild(link);
  42. nav.appendChild(button);
  43. } else {
  44. wishstr = GM_getValue('wish');
  45. if (!wishstr) {
  46. wishstr = '[]'
  47. }
  48. wishlist = JSON.parse(wishstr);
  49. infodict = GM_getValue('infodict');
  50. if (!infodict) {
  51. infodict = {};
  52. }
  53. document.getElementById('install').remove();
  54. document.getElementById('noserver').remove();
  55. var sbox = document.getElementById('server');
  56. var opt;
  57. for (var key in serverdic) {
  58. opt = document.createElement('option');
  59. opt.value = key;
  60. opt.text = serverdic[key][0];
  61. sbox.appendChild(opt);
  62. }
  63. var bts = document.getElementById('buttons');
  64. var btn = document.createElement('button');
  65. btn.type = 'button';
  66. btn.className = 'layui-btn layui-btn-sm';
  67. btn.textContent = '刷新愿望单数据';
  68. btn.addEventListener('click', flashLocalData, false);
  69. bts.appendChild(btn);
  70.  
  71. if (infodict) {
  72. renderTable();
  73. document.getElementById('status').textContent = '已显示缓存数据'
  74. }
  75. }
  76.  
  77.  
  78. function getSelectServer() {
  79. var choose = document.getElementById('server').value;
  80. for (var key in serverdic) {
  81. if (choose == key) {
  82. server = 'https://' + serverdic[key][1];
  83. }
  84. }
  85. }
  86.  
  87. function flashLocalData() {
  88. getSelectServer();
  89. var process = document.getElementById('process');
  90. var status = document.getElementById('status');
  91. var count = 0;
  92. var total = wishlist.length;
  93. var tmp;
  94.  
  95. tmp = setInterval(
  96. function () {
  97. getFromRemote(wishlist[count]);
  98. }
  99. , 500)
  100.  
  101. while (count >= total) {
  102. clearInterval(tmp);
  103. }
  104.  
  105. function getFromRemote(appid) {
  106. try {
  107. GM_xmlhttpRequest({
  108. method: "GET",
  109. headers: {
  110. "User-Agent": "SWH_Helper 1.0",
  111. "Accept": "application/json"
  112. },
  113. url: server + '/games/' + appid.toString(),
  114. onload: function (response) {
  115. count++;
  116. status.textContent = '进度:' + count.toString() + '/' + total.toString()+',刷新即可终止操作(已有数据会保留)';
  117. process.style.width = getPercent(count, total)
  118. if (response.status == 200) {
  119. var t = JSON.parse(response.responseText);
  120. t["lowest"] = t["pcurrent"] <= t["plowest"];
  121. infodict[appid] = t;
  122. GM_setValue('infodict', infodict);
  123. renderTable();
  124. } else {
  125. console.log('未查到信息,appid:', appid);
  126. }
  127. if (count >= total) {
  128. console.log('处理完毕');
  129. }
  130. }
  131. });
  132. } catch (e) {
  133. console.log(e);
  134. }
  135. }
  136.  
  137. }
  138.  
  139. function cardb2s(b) {
  140. return b["card"] ? "√" : ""
  141. }
  142. function limitb2s(b) {
  143. return b["limit"] ? "√" : ""
  144. }
  145. function adultb2s(b) {
  146. return b["adult"] ? "√" : ""
  147. }
  148. function freeb2s(b) {
  149. return b["free"] ? "√" : ""
  150. }
  151. function releaseb2s(b) {
  152. return b["release"] ? "√" : ""
  153. }
  154. function lowestb2s(b) {
  155. return b["lowest"] ? "√" : ""
  156. }
  157. function genLink(b) {
  158. return '<a style="color: #01AAED;" href=https://store.steampowered.com/app/' + b["appid"].toString() + '>' + b["appid"].toString() + '</a>'
  159.  
  160. }
  161. function review2text(b) {
  162. var score = b["rscore"];
  163. if (score > 9) { score = 9; }
  164. if (score < 0) { score = 0; }
  165. return review[b["rscore"]];
  166. }
  167.  
  168. function renderTable() {
  169. var data = [];
  170. for (var key in infodict) {
  171. data.push(infodict[key]);
  172. }
  173. layui.use('table', function () {
  174. var table = layui.table;
  175. table.render({
  176. elem: '#test'
  177. , cols: [[
  178. { field: 'appid', title: 'ID', width: 100, sort: true, fixed: 'left', align: "center", templet: genLink }
  179. , { field: 'name_cn', title: '名称', width: 200, sort: true, align: "center" }
  180. , { field: 'card', title: '卡牌', minWidth: 50, sort: true, align: "center", templet: cardb2s }
  181. , { field: 'limit', title: '受限', minWidth: 50, sort: true, align: "center", templet: limitb2s }
  182. , { field: 'adult', title: '软锁', width: 50, sort: true, align: "center", templet: adultb2s }
  183. , { field: 'free', title: '免费', width: 50, sort: true, align: "center", templet: freeb2s }
  184. , { field: 'release', title: '发售', width: 50, sort: true, align: "center", templet: releaseb2s }
  185. , { field: 'lowest', title: '史低', width: 50, sort: true, align: "center", templet: lowestb2s }
  186.  
  187. , { field: 'rscore', title: '评价', width: 120, sort: true, align: "center", templet: review2text }
  188. , { field: 'rtotal', title: '总评', width: 80, sort: true, align: "center" }
  189. , { field: 'rpercent', title: '好评', width: 80, sort: true, align: "center" }
  190.  
  191. , { field: 'pcurrent', title: '现价', width: 80, sort: true, align: "center" }
  192. , { field: 'porigin', title: '原价', width: 80, sort: true, align: "center" }
  193. , { field: 'plowest', title: '史低价', width: 100, sort: true, align: "center" }
  194. , { field: 'pcut', title: '当前折扣', width: 100, sort: true, align: "center" }
  195. , { field: 'plowestcut', title: '史低折扣', width: 100, sort: true, align: "center" }
  196.  
  197. , { field: 'tag', title: '标签', width: 150, align: "center" }
  198. , { field: 'developer', title: '开发商', width: 150, align: "center" }
  199. , { field: 'publisher', title: '发行商', width: 150, align: "center" }
  200.  
  201. , { field: 'tlowest', title: '史低时间', width: 120, sort: true, align: "center" }
  202. , { field: 'trelease', title: '发售时间', width: 120, sort: true, align: "center" }
  203. , { field: 'tmodify', title: '更新时间', width: 120, sort: true, align: "center" }
  204.  
  205. , { field: 'gtype', title: '类型', width: 50, sort: true, align: "center" }
  206. , { field: 'source', title: '来源', width: 50, sort: true, align: "center" }
  207. ]]
  208. , data: data
  209. , skin: 'row' //表格风格
  210. , even: true
  211. , page: true //是否显示分页
  212. , limits: [15, 30, 60, 100, 150, 300, 600, 1000]
  213. , limit: 60
  214. , cellMinWidth: 60
  215. , height: 'full-215'
  216. });
  217. });
  218. }
  219. function getPercent(num, total) {
  220. num = parseFloat(num);
  221. total = parseFloat(total);
  222. if (isNaN(num) || isNaN(total)) {
  223. return "0%";
  224. }
  225. return total <= 0 ? "0%" : (Math.round(num / total * 100)) + "%";
  226. }
  227. })();