Hikari_Field入库检测

Hikari_Field入库游戏检测

当前为 2021-12-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name:zh-CN Hikari_Field入库检测
  3. // @name Hikari_Field_Helper
  4. // @namespace https://blog.chrxw.com
  5. // @supportURL https://blog.chrxw.com/scripts.html
  6. // @contributionURL https://afdian.net/@chr233
  7. // @version 2.1
  8. // @description Hikari_Field入库游戏检测
  9. // @description:zh-CN Hikari_Field入库游戏检测
  10. // @author Chr_
  11. // @include https://keylol.com/*
  12. // @include https://store.hikarifield.co.jp/libraries
  13. // @license AGPL-3.0
  14. // @icon https://blog.chrxw.com/favicon.ico
  15. // @resource data https://gitee.com/chr_a1/gm_scripts/raw/master/Keylol/Data/Hikari_Field_Helper.json
  16. // @grant GM_getResourceText
  17. // @grant GM_setValue
  18. // @grant GM_getValue
  19. // @grant GM_addStyle
  20. // @grant GM_registerMenuCommand
  21. // ==/UserScript==
  22.  
  23.  
  24. (() => {
  25. "use strict";
  26.  
  27. const HFSHOP = "https://store.hikarifield.co.jp/shop/";
  28. const HFLIBARY = "https://store.hikarifield.co.jp/libraries";
  29.  
  30. GM_registerMenuCommand("导入Hikari Field游戏", () => {
  31. window.location.href = HFLIBARY;
  32. });
  33.  
  34. const { INFO, DESC } = JSON.parse(GM_getResourceText("data"));
  35. const host = window.location.host;
  36.  
  37. if (host === "store.hikarifield.co.jp") {
  38. //更新库存
  39. const myGames = document.querySelectorAll(".game-cover>a");
  40. const ownedGames = [625760]; //魔卡魅恋(免费)
  41. for (const ele of myGames) {
  42. const key = ele.href?.replace(HFSHOP, "");
  43. if (key !== undefined) {
  44. let [gameName, appID, _, __] = INFO[key] ?? [null, null, null];
  45. if (appID !== null) {
  46. ownedGames.push(appID);
  47. console.log(`已拥有 ${gameName} ${appID}`);
  48. }
  49. } else {
  50. console.error(`${ele.href} 无效`);
  51. }
  52. }
  53. //储存列表
  54. GM_setValue("ownedGames", ownedGames);
  55. GM_setValue("refreshTime", new Date().toISOString());
  56. swal({
  57. position: "top-end",
  58. text: "导入游戏列表成功",
  59. icon: "success",
  60. button: false,
  61. timer: 1200
  62. });
  63.  
  64. } else if (host.endsWith('keylol.com')) {
  65. //其乐
  66. if (document.title.search('Keylol') === -1) { return; } //跳过iframe
  67. const ownedGames = new Set(GM_getValue("ownedGames") ?? []);
  68. const refreshTime = GM_getValue("refreshTime") ?? null;
  69.  
  70. if (ownedGames.size === 0) {
  71. if (confirm("是否导入游戏列表?")) {
  72. window.location.href = HFLIBARY;
  73. } else {
  74. showError("【可以在油猴菜单中进行同步】");
  75. GM_setValue("ownedGames", [0]);
  76. }
  77. }
  78.  
  79. setTimeout(() => {
  80. const steamLinks = document.querySelectorAll("a[href^='https://store.steampowered.com/'],a[href^='https://steamdb.info/app/']");
  81. const HFLinks = document.querySelectorAll("a[href^='https://store.hikarifield.co.jp/shop/']");
  82. let flag = HFLinks.length > 0;
  83. const grubAppid = RegExp(/app\/(\d+)\/?/);
  84. for (const ele of steamLinks) {
  85. const href = ele.href;
  86. if (href !== undefined) {
  87. const appID = parseInt(grubAppid.exec(href)?.[1] ?? 0);
  88. if (appID > 0) {
  89. if (ownedGames.has(appID)) {
  90. ele.classList.add("steam-info-link");
  91. ele.classList.add("steam-info-own");
  92. flag = true;
  93. }
  94. }
  95. }
  96. }
  97. if (!flag) { return; } //未匹配到游戏,结束运行
  98. for (const ele of HFLinks) {
  99. const key = ele.href?.replace(HFSHOP, "");
  100. if (key !== undefined) {
  101. let [gameName, appID, steam, hf] = INFO[key] ?? [null, null, null, null];
  102. if (appID !== null) {
  103. if (ownedGames.has(appID)) {
  104. ele.classList.add("steam-info-link");
  105. ele.classList.add("steam-info-own");
  106. }
  107. ele.setAttribute("data-hf", key);
  108. ele.addEventListener("mouseenter", showDiag);
  109. }
  110. } else {
  111. console.log(ele);
  112. }
  113. }
  114. }, 1000);
  115.  
  116. const diagObjs = {}; //小部件DOM对象
  117. //创建弹窗小部件
  118. function initDiag() {
  119. const newDiv = (cls) => { const d = document.createElement("div"); if (cls) { d.className = cls; } return d; };
  120.  
  121. const hfBox = newDiv("hf-box");
  122.  
  123. let lastRefresh;
  124. if (refreshTime !== null) {
  125. try {
  126. const t = new Date(refreshTime);
  127. lastRefresh = `账号同步于 ${t.toLocaleString()} 点击刷新`;
  128. } catch (e) {
  129. console.error(e);
  130. lastRefresh = `读取同步时间出错, 点击刷新`;
  131. }
  132. } else {
  133. lastRefresh = "账号未同步, 点击刷新";
  134. }
  135.  
  136. hfBox.style.display="none";
  137.  
  138. hfBox.innerHTML = `
  139. <div class="hf-head">
  140. <span title="">占位</span><a title="隐藏">❌</a>
  141. </div>
  142. <div class="hf-body">
  143. <img src="https://cdn.cloudflare.steamstatic.com/steam/apps/1662840/header.jpg">
  144. </div>
  145. <div class="hf-foot">
  146. <div class="hf-describe">
  147. <span title="">...</span>
  148. </div>
  149. <div class="hf-line"></div>
  150. <div class="hf-detail">
  151. <div class="hf-line"></div>
  152. <div><a href="${HFLIBARY}" target="_blank">${lastRefresh}</a></div>
  153. <div class="hf-line"></div>
  154. <p class="hf-hf"><b>HF商店:</b><span class="hf-unknown">占位</span><a href="#" target="_blank" class="hf-link">前往商店</a></p>
  155. <div class="hf-line"></div>
  156. <p class="hf-steam"><b>Steam: </b><span class="hf-unknown">占位</span> <a href="#" target="_blank" class="hf-link steam-info-loaded">前往商店</a> (<a href="#" target="_blank">SteamDB</a>)</p></div>
  157. <div class="hf-line"></div>
  158. </div>
  159. </div>
  160. `
  161. document.body.appendChild(hfBox);
  162.  
  163. const eleTitle = hfBox.querySelector("div.hf-head>span");
  164. const eleClose = hfBox.querySelector("div.hf-head>a");
  165. const eleImg = hfBox.querySelector("div.hf-body>img");
  166. const eleDesc = hfBox.querySelector("div.hf-describe>span");
  167. const eleHfState = hfBox.querySelector("p.hf-hf>span");
  168. const eleHfLink = hfBox.querySelector("p.hf-hf>a");
  169. const eleSteamState = hfBox.querySelector("p.hf-steam>span");
  170. const eleSteamLink = hfBox.querySelector("p.hf-steam>a:first-of-type");
  171. const eleSteamDBLink = hfBox.querySelector("p.hf-steam>a:last-of-type");
  172.  
  173. eleClose.addEventListener('click', hideDiag);
  174.  
  175. Object.assign(diagObjs, { hfBox, eleTitle, eleImg, eleDesc, eleHfState, eleHfLink, eleSteamState, eleSteamLink, eleSteamDBLink });
  176. }
  177.  
  178. initDiag();
  179.  
  180. const { script: { version } } = GM_info;
  181. const Tail = ` - Hikari_Field_Helper v${version} by Chr_』`;
  182.  
  183. //更新小部件显示
  184. function showDiag(event) {
  185. const ele = event.target;
  186. const key = ele.getAttribute("data-hf");
  187.  
  188. const { hfBox, eleTitle, eleImg, eleDesc, eleHfState, eleHfLink, eleSteamState, eleSteamLink, eleSteamDBLink } = diagObjs;
  189.  
  190. const [gameName, appID, steamState, hfState] = INFO[key] ?? [null, null, null, null];
  191. const describe = DESC[key] ?? "";
  192.  
  193. if (!gameName) { return; }
  194.  
  195. eleTitle.title = gameName + Tail;
  196. eleTitle.textContent = gameName;
  197. eleImg.src = `https://cdn.cloudflare.steamstatic.com/steam/apps/${appID}/header.jpg`;
  198.  
  199. eleDesc.textContent = describe.substr(0, 72) + "...";
  200. eleDesc.title = describe;
  201.  
  202. switch (hfState) {
  203. case -1:
  204. eleHfState.textContent = "已下架";
  205. eleHfState.className = "hf-unavailable";
  206. break;
  207. case 1:
  208. eleHfState.textContent = "可购买";
  209. eleHfState.className = "hf-available";
  210. break;
  211. default:
  212. eleHfState.textContent = "未发售";
  213. eleHfState.className = "hf-unknown";
  214. break;
  215. }
  216. eleHfLink.href = HFSHOP + key;
  217.  
  218. switch (steamState) {
  219. case -1:
  220. eleSteamState.textContent = "已下架";
  221. eleSteamState.className = "hf-unavailable";
  222. eleSteamLink.classList.add("hf-disabled");
  223. break;
  224. case 1:
  225. eleSteamState.textContent = "可购买";
  226. eleSteamState.className = "hf-available";
  227. eleSteamLink.classList.remove("hf-disabled");
  228. break;
  229. default:
  230. eleSteamState.textContent = "未发售";
  231. eleSteamState.className = "hf-unknown";
  232. eleSteamLink.classList.remove("hf-disabled");
  233. break;
  234. }
  235. eleSteamLink.href = `https://store.steampowered.com/app/${appID}/`;
  236. eleSteamDBLink.href = `https://steamdb.info/app/${appID}/`;
  237.  
  238. const { top, right } = ele.getBoundingClientRect();
  239. hfBox.style.left = `${right + window.scrollX + 2}px`;
  240. hfBox.style.top = `${top + window.scrollY}px`;
  241. hfBox.style.opacity = 1;
  242. hfBox.style.display = "";
  243. hfBox.removeAttribute("hf-fd");
  244. }
  245. //隐藏小部件
  246. function hideDiag(event) {
  247. console.log('call hideDiag');
  248. const { hfBox } = diagObjs;
  249. if (!hfBox.hasAttribute("hf-fd")) {
  250. hfBox.setAttribute("hf-fd", "");
  251. hfBox.style.opacity = 0;
  252. setTimeout(() => {
  253. hfBox.style.cssText = "display:none; opacity: 0;";
  254. }, 200);
  255. }
  256. }
  257. }
  258.  
  259. })();
  260.  
  261. GM_addStyle(`.hf-line {
  262. margin-top: 0.5em;
  263. }
  264. .hf-available {
  265. color: #6c3;
  266. padding: 0 0.3em;
  267. }
  268. .hf-unavailable {
  269. color: #e60;
  270. padding: 0 0.3em;
  271. }
  272. .hf-unknown {
  273. color: #ccc;
  274. padding: 0 0.3em;
  275. }
  276. .hf-available::before {
  277. content: "☑";
  278. padding-right: 0.3em;
  279. }
  280. .hf-unavailable::before {
  281. content: "☒";
  282. padding-right: 0.3em;
  283. }
  284. .hf-unknown::before {
  285. content: "☐";
  286. padding-right: 0.3em;
  287. }
  288. .hf-disabled {
  289. pointer-events: none;
  290. cursor: default;
  291. text-decoration: line-through !important;
  292. }
  293. .hf-detail > div {
  294. text-align: center;
  295. }
  296. .hf-box {
  297. width: 300px;
  298. position: absolute;
  299. left: 0;
  300. top: 0;
  301. z-index: 100;
  302. background-color: #343a40;
  303. color: #ccc;
  304. border-radius: 5px;
  305. transition: all 0.2s;
  306. }
  307. .hf-box * {
  308. max-width: 100%;
  309. }
  310. .hf-head {
  311. overflow: hidden;
  312. white-space: nowrap;
  313. margin: 4px 0;
  314. }
  315. .hf-head > span {
  316. font-size: 12px;
  317. font-weight: bold;
  318. padding: 5px 16px;
  319. }
  320. .hf-head > a {
  321. position: absolute;
  322. right: 5px;
  323. cursor: pointer;
  324. }
  325. .hf-body {
  326. height: 140px;
  327. }
  328. .hf-foot {
  329. margin: 5px;
  330. }
  331. .hf-foot b {
  332. color: #8a959c;
  333. font-weight: normal;
  334. }
  335. .hf-foot a {
  336. color: #fff;
  337. text-decoration: none;
  338. }
  339.  
  340. `);