SG Game Tags

Shows some tags of the game in Steamgifts.

当前为 2016-03-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name SG Game Tags
  3. // @namespace http://steamcommunity.com/id/Ruphine/
  4. // @version 0.2
  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. </style>';
  37.  
  38. $("head").append(myCSS);
  39.  
  40.  
  41. /* Constant Variables */
  42. const linkCard = "http://www.steamcardexchange.net/index.php?inventorygame-appid-";
  43. const linkBundle = "http://www.steamgifts.com/bundle-games/search?q=";
  44.  
  45. const linkGameAPI = "http://store.steampowered.com/api/appdetails?filters=categories&appids=";
  46. const linkPackAPI = "http://store.steampowered.com/api/packagedetails?filters=categories&packageids=";
  47.  
  48. const ClassCard = "tags tags-green";
  49. const TitleCard = "This game has trading cards";
  50. const TextCard = "Cards";
  51.  
  52. const ClassBundle = "tags tags-red";
  53. const TitleBundle = "This game is considered as bundled by Steamgifts";
  54. const TextBundle = "Bundled";
  55.  
  56. main();
  57.  
  58. function main()
  59. {
  60. //shows trading card tag in featured game (header)
  61. var currLoc = window.location.href.split("/");
  62. if(currLoc[currLoc.length-2] != "user") //exclude user page for getting featured appID
  63. {
  64. var ID = getAppIDfromImg($(".global__image-outer-wrap img")[0].src);
  65. if (ID != null) { //if game doesn't have appID e.g Humble Indie Bundle
  66. var Name = $(".featured__heading__medium").text();
  67. var target = $(".featured__heading");
  68.  
  69. var tagCard = createTag(ClassCard, TitleCard, TextCard, linkCard+ID, target);
  70. var tagBundle = createTag(ClassBundle, TitleBundle, TextBundle, linkBundle+Name, target);
  71.  
  72. getTradingCardStatus(tagCard, ID);
  73. getBundleStatus(tagBundle, ID, Name);
  74. }
  75. }
  76.  
  77. //looping for each games shown
  78. $(".giveaway__row-inner-wrap").each(function(index, element)
  79. {
  80. var ID = getAppIDfromLink($("a.giveaway__icon")[index].href);
  81. if (ID != null) {
  82. var Name = $($(".giveaway__heading__name")[index]).text();
  83. var target = $(element).find(".giveaway__heading");
  84.  
  85. var tagCard = createTag(ClassCard, TitleCard, TextCard, linkCard+ID, target);
  86. var tagBundle = createTag(ClassBundle, TitleBundle, TextBundle, linkBundle+Name, target);
  87.  
  88. //I assume app will have zero at the end ID, and package is non zero.Need more research.
  89. if(ID.charAt(ID.length-1) == "0")
  90. getTradingCardStatus(tagCard, ID);
  91. else
  92. getTradingCardStatusFromPackage(tagCard, ID);
  93. getBundleStatus(tagBundle, ID, Name);
  94. }
  95. });
  96. }
  97.  
  98. function createTag(_class, title, text, href, divTarget)
  99. {
  100. var tag = document.createElement("a");
  101. tag.setAttribute("id", "tags");
  102. tag.setAttribute("target", "_blank");
  103. tag.setAttribute("class", _class);
  104. tag.setAttribute("title", title);
  105. tag.setAttribute("href", href);
  106. tag.innerHTML = text;
  107.  
  108. divTarget.append(tag);
  109.  
  110. return tag;
  111. }
  112.  
  113. function getAppIDfromImg(link)
  114. {
  115. // http://cdn.akamai.steamstatic.com/steam/apps/269270/header_292x136.jpg
  116. var url = link.split("/");
  117. return url[url.length-2];
  118. }
  119.  
  120. function getAppIDfromLink(link)
  121. {
  122. // http://store.steampowered.com/app/403570/
  123. var url = link.split("/");
  124. return url[url.length-2];
  125. }
  126.  
  127. function getTradingCardStatus(elems, appID)
  128. {
  129. //TODO: Check if the game is saved, if no then request to steam
  130. GM_xmlhttpRequest({
  131. method: "GET",
  132. timeout: 10000,
  133. url: linkGameAPI+appID,
  134. onload: function(data)
  135. {
  136. var obj = JSON.parse(data.responseText)[appID].data.categories;
  137. for(i=0; i<obj.length; i++)
  138. {
  139. if(obj[i].id == "29")
  140. {
  141. //TODO : Save appID + true ke local cache
  142. $(elems).css("display", "block");
  143. break;
  144. }
  145. }
  146. }
  147. });
  148. }
  149.  
  150. function getBundleStatus(elems, appID, appName)
  151. {
  152. //TODO: Check if the game is saved, if no then request to steam
  153. $.get( linkBundle+appName, function( data ) {
  154. var gamesfound = $(data).find(".table__column__secondary-link");
  155. for(i=0; i<$(gamesfound).length; i++)
  156. {
  157. var url = $(gamesfound)[i].href;
  158. var ID = getAppIDfromLink(url);
  159.  
  160. if(appID == ID)
  161. {
  162. //TODO : Save appID + true ke local cache
  163. $(elems).css("display", "block");
  164. break;
  165. }
  166. }
  167. //TODO : Save appID + false + expire time ke local cache
  168. });
  169. }
  170.  
  171. function getTradingCardStatusFromPackage(elems, appID) //Need more research
  172. {
  173. //TODO: Check if the game is saved, if no then request to steam
  174. GM_xmlhttpRequest({
  175. method: "GET",
  176. timeout: 10000,
  177. url: linkGameAPI+appID,
  178. onload: function(data)
  179. {
  180. var ID = JSON.parse(data.responseText)[appID].data.apps[0].id;
  181. getTradingCardStatus(elems, ID);
  182. //TODO : Save appID + false + expire time ke local cache
  183. }
  184. });
  185. }