Greasy Fork 还支持 简体中文。

SG Game Tags

Shows some tags of the game in Steamgifts.

目前為 2016-03-19 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name SG Game Tags
  3. // @namespace http://steamcommunity.com/id/Ruphine/
  4. // @version 1.0
  5. // @description Shows some tags of the game in Steamgifts.
  6. // @author Ruphine
  7.  
  8. // @match http://www.steamgifts.com/*
  9.  
  10. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
  11. // @grant GM_deleteValue
  12. // @grant GM_getValue
  13. // @grant GM_listValues
  14. // @grant GM_setValue
  15. // @grant GM_xmlhttpRequest
  16. // ==/UserScript==
  17.  
  18. /* CSS */
  19. var myCSS;
  20. myCSS = '<style> \
  21. .tags { \
  22. color: #FFFFFF; \
  23. text-decoration: none; \
  24. border-radius: 20px; \
  25. padding-top: 2px; \
  26. padding-bottom: 2px; \
  27. padding-left: 5px; \
  28. padding-right: 5px; \
  29. font-size: 8pt; \
  30. margin-left: 3px; \
  31. text-shadow: none; \
  32. display: none; \
  33. } \
  34. .tags-green { background-color: #3AA435; } \
  35. .tags-red { background-color: #f44336; } \
  36. .tags-blue { background-color: #305AC9; } \
  37. </style>';
  38.  
  39. $("head").append(myCSS);
  40.  
  41.  
  42. /* Constant Variables */
  43. const linkCard = "http://www.steamcardexchange.net/index.php?inventorygame-appid-";
  44. const linkBundle = "http://www.steamgifts.com/bundle-games/search?q=";
  45.  
  46. const linkGameAPI = "http://store.steampowered.com/api/appdetails?filters=categories&appids=";
  47. const linkPackAPI = "http://store.steampowered.com/api/packagedetails?filters=categories&packageids=";
  48.  
  49. const ClassCard = "tags tags-green";
  50. const TitleCard = "This game has trading cards";
  51. const TextCard = "Cards";
  52.  
  53. const ClassBundle = "tags tags-red";
  54. const TitleBundle = "This game is considered as bundled by Steamgifts";
  55. const TextBundle = "Bundled";
  56.  
  57. main();
  58.  
  59. function main()
  60. {
  61. // shows trading card tag in featured game (header)
  62. var currLoc = window.location.href.split("/");
  63.  
  64. if($(".featured__inner-wrap").length == 1) //exclude page without featured inner wrap
  65. {
  66. var url;
  67. if(currLoc[3] == "giveaway")
  68. url = $(".featured__inner-wrap a")[0].href;
  69. else if(currLoc[3] != "user")
  70. url = $(".featured__inner-wrap a img")[0].src;
  71.  
  72. if (url != null) //if game doesn't have appID e.g Humble Indie Bundle
  73. {
  74. var ID = getAppIDfromImg(url);
  75.  
  76. var Name = $(".featured__heading__medium").text();
  77. var target = $(".featured__heading");
  78.  
  79. var tagCard = createTag(ClassCard, TitleCard, TextCard, linkCard+ID, target);
  80. var tagBundle = createTag(ClassBundle, TitleBundle, TextBundle, linkBundle+Name, target);
  81.  
  82. if(isAppOrPackage(url))
  83. getTradingCardStatus(tagCard, ID);
  84. else
  85. getTradingCardStatusFromPackage(tagCard, ID);
  86.  
  87. getBundleStatus(tagBundle, ID, Name);
  88. }
  89. }
  90.  
  91. //looping for each games shown
  92. $(".giveaway__row-inner-wrap").each(function(index, element)
  93. {
  94. var url = $(element).find("a.giveaway__icon").attr("href");
  95. if(url == null)
  96. console.log();
  97. else
  98. {
  99. var ID = getAppIDfromLink(url);
  100. var Name = $(element).find(".giveaway__heading__name").text();
  101. var target = $(element).find(".giveaway__heading");
  102.  
  103. var tagCard = createTag(ClassCard, TitleCard, TextCard, linkCard+ID, target);
  104. var tagBundle = createTag(ClassBundle, TitleBundle, TextBundle, linkBundle+Name, target);
  105.  
  106. if(isAppOrPackage(url))
  107. getTradingCardStatus(tagCard, ID);
  108. else
  109. getTradingCardStatusFromPackage(tagCard, ID);
  110.  
  111. getBundleStatus(tagBundle, ID, Name);
  112. }
  113. });
  114. }
  115.  
  116. function createTag(_class, title, text, href, divTarget)
  117. {
  118. var tag = document.createElement("a");
  119. tag.setAttribute("id", "tags");
  120. tag.setAttribute("target", "_blank");
  121. tag.setAttribute("class", _class);
  122. tag.setAttribute("title", title);
  123. tag.setAttribute("href", href);
  124. tag.innerHTML = text;
  125.  
  126. divTarget.append(tag);
  127. return tag;
  128. }
  129.  
  130. function getTradingCardStatus(elems, appID)
  131. {
  132. var jsonCards = GM_getValue("cards-" + appID, "");
  133. if(!needRequest(jsonCards))
  134. {
  135. if(JSON.parse(jsonCards).val)
  136. $(elems).css("display", "block");
  137. }
  138. else
  139. {
  140. console.log("request card " + appID);
  141. GM_xmlhttpRequest({
  142. method: "GET",
  143. timeout: 10000,
  144. url: linkGameAPI+appID,
  145. onload: function(data)
  146. {
  147. var obj = JSON.parse(data.responseText)[appID].data;
  148. if(obj == null)
  149. {
  150. console.log("apps " + appID + " does not have store page or does not exist");
  151. saveData("cards-" + appID, false);
  152. }
  153. else
  154. {
  155. obj =obj.categories;
  156. for(i=0; i<obj.length; i++)
  157. {
  158. if(obj[i].id == "29")
  159. {
  160. $(elems).css("display", "block");
  161. saveData("cards-" + appID, true);
  162. return true; //exit function
  163. }
  164. }
  165. saveData("cards-" + appID, false);
  166. }
  167. }
  168. });
  169. }
  170. }
  171.  
  172. function getBundleStatus(elems, appID, appName)
  173. {
  174. var jsonBundle = GM_getValue("bundled-" + appID, "");
  175. if(!needRequest(jsonBundle))
  176. {
  177. if(JSON.parse(jsonBundle).val)
  178. $(elems).css("display", "block");
  179. }
  180. else
  181. {
  182. console.log("request bundle " + appID);
  183. $.get( linkBundle+appName, function(data) {
  184. var gamesfound = $(data).find(".table__column__secondary-link");
  185. for(i=0; i<$(gamesfound).length; i++)
  186. {
  187. var url = $(gamesfound)[i].href;
  188. var ID = getAppIDfromLink(url);
  189.  
  190. if(appID == ID)
  191. {
  192. //TODO : Save appID + true ke local cache
  193. $(elems).css("display", "block");
  194. saveData("bundled-" + appID, true);
  195. return true; //exit function
  196. }
  197. }
  198. saveData("bundled-" + appID, false);
  199. });
  200. }
  201. }
  202.  
  203. function getTradingCardStatusFromPackage(elems, appID) //Need more research
  204. {
  205. //TODO: Check if the game is saved, if no then request to steam
  206. GM_xmlhttpRequest({
  207. method: "GET",
  208. timeout: 10000,
  209. url: linkPackAPI+appID,
  210. onload: function(data)
  211. {
  212. var IDs = JSON.parse(data.responseText)[appID].data;
  213. if(IDs == null) console.log("package " + appID + " does not exist");
  214. else
  215. {
  216. IDs = IDs.apps;
  217. $.each(IDs, function(index)
  218. {
  219. getTradingCardStatus(elems, IDs[index].id);
  220. //TODO : Save appID + false + expire time ke local cache
  221. });
  222. }
  223. }
  224. });
  225. }
  226.  
  227. function getAppIDfromImg(link)
  228. {
  229. // http://cdn.akamai.steamstatic.com/steam/apps/269270/header_292x136.jpg
  230. var url = link.split("/");
  231. return url[url.length-2];
  232. }
  233.  
  234. function getAppIDfromLink(link)
  235. {
  236. // http://store.steampowered.com/app/403570/
  237. var url = link.split("/");
  238. return url[url.length-2];
  239. }
  240.  
  241. function isAppOrPackage(link)
  242. {
  243. // store.steampowered.com/app/403570/
  244. var pattern = /\/app\/|\/apps\//;
  245. return pattern.test(link);
  246. }
  247.  
  248. function saveData(name, val)
  249. {
  250. var today = new Date().toJSON().slice(0,10);
  251. var data = {val:val, savedDate:today};
  252. GM_setValue(name, JSON.stringify(data));
  253. }
  254.  
  255. function needRequest(json)
  256. {
  257. if(json == "")
  258. return true;
  259. else
  260. {
  261. var obj = JSON.parse(json);
  262. if(obj.val)
  263. return false;
  264. else
  265. {
  266. var today = new Date().toJSON().slice(0,10);
  267. if(obj.savedDate == today)
  268. return false;
  269. else
  270. return true;
  271. }
  272. }
  273. }