SideTrackedStats

Adds your SideTracked stats badge onto your profile page and SideTracked cache pages on geocaching.com.

目前为 2018-04-08 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name SideTrackedStats
  3. // @namespace http://www.cryotest.com/
  4. // @description Adds your SideTracked stats badge onto your profile page and SideTracked cache pages on geocaching.com.
  5. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  6. // @copyright 2015-2018, Cryo99
  7. // @attribution SideTracked stats provided by Chris AKA Bus.Stop (http://www.sidetrackedseries.info/)
  8. // @attribution Icon image extracted from the SideTracked banner by Chris AKA Bus.Stop
  9. // @icon https://raw.githubusercontent.com/Cryo99/SideTrackedStats/master/icon48.png
  10. // @icon64 https://raw.githubusercontent.com/Cryo99/SideTrackedStats/master/icon64.png
  11. // @include /^https?://www\.geocaching\.com/(account|my|default|geocache|profile|seek/cache_details|p)/
  12. // @exclude /^https?://www\.geocaching\.com/(login|about|articles|myfriends)/
  13. // @version 0.1.0
  14. // @supportURL https://github.com/Cryo99/SideTrackedStats
  15. // @grant GM_xmlhttpRequest
  16. // ==/UserScript==
  17.  
  18.  
  19. (function (){
  20. "use strict";
  21. var cacheName = document.getElementById("ctl00_ContentBody_CacheName"),
  22. stsCSS = document.createElement("style"),
  23. // ST images can be wider when level names are long. overflow: hidden; on sts-container prevents images from overlaying the div border.
  24. css = 'div.sts-container { border: 1px solid #b0b0b0; margin-top: 1.5em; padding: 0; text-align: center; overflow: hidden;} .WidgetBody div.sts-container { border: none; } #ctl00_ContentBody_ProfilePanel1_pnlProfile div.sts-container { border: none; text-align: inherit;} a.sts-badge { background-color: white;} #ctl00_ContentBody_ProfilePanel1_pnlProfile div.sts-container {float: left}',
  25. currentPage,
  26. profileNameOld = document.getElementById("ctl00_ContentBody_ProfilePanel1_lblMemberName"),
  27. profileName = document.getElementById("ctl00_ProfileHead_ProfileHeader_lblMemberName"),
  28. userFieldOld = document.getElementsByClassName("li-user-info"),
  29. userField = document.getElementsByClassName("user-name"),
  30. userName = "",
  31. userNames = [],
  32. stats = [];
  33.  
  34. function displayStats(stats, page){
  35. function getHtml(uname, level, award, finds){
  36. return "<a class='sts-badge' href='http://www.sidetrackedseries.info' title='SideTracked stats.'><img src='http://img.sidetrackedseries.info/awards/st_F_award.php?name=" + uname + "&brand=jobs' /></a>";
  37. }
  38. var stsWidget = document.createElement("div"),
  39. html = "",
  40. i,
  41. target;
  42.  
  43. for(i = 0; i < stats.length; i++){
  44. var name = (stats[i].name + "")
  45. .replace(/;/g, ",")
  46. .replace(/'/g, "&apos;")
  47. .replace(/"/g, "&quot;");
  48. if(i === 0 || stats[i].name !== stats[0].name){
  49. html += getHtml(name);
  50. }
  51. }
  52.  
  53. switch(page){
  54. case "my":
  55. target = document.getElementById("ctl00_ContentBody_lnkProfile");
  56. break;
  57. case "account":
  58. target = document.getElementsByClassName('sidebar-right')[0];
  59. break;
  60. case "cache":
  61. target = document.getElementsByClassName('sidebar')[0];
  62. break;
  63. case "profile":
  64. target = document.getElementById("ctl00_ContentBody_ProfilePanel1_lblProfile");
  65. if(target){
  66. target = target.parentNode;
  67. }
  68. break;
  69. }
  70.  
  71. if(!target){
  72. console.warn("SideTracked Stats: Aborted - couldn't find where to insert widget. You might not be logged in.");
  73. return;
  74. }
  75.  
  76. if(html){
  77. stsWidget.className = "sts-container";
  78. stsWidget.innerHTML = html;
  79. switch(page){
  80. case "my":
  81. case "profile":
  82. target.parentNode.insertBefore(stsWidget, target.nextSibling);
  83. break;
  84. default:
  85. target.insertBefore(stsWidget, target.firstChild.nextSibling.nextSibling);
  86. break;
  87. }
  88. }else{
  89. console.warn("SideTracked Stats: didn't generate an award badge.");
  90. }
  91. }
  92. function getHiderName(){
  93. var i,
  94. links = document.getElementsByTagName("a"),
  95. pos;
  96. if(links){
  97. for(i = 0; i < links.length; i++){
  98. pos = links[i].href.indexOf("/seek/nearest.aspx?u=");
  99. if(pos !== -1){
  100. return decodeURIComponent(links[i].href.substr(pos + 21).replace(/\+/g, '%20'));
  101. }
  102. }
  103. }
  104. }
  105.  
  106. function parseNames(names){
  107. // Filter out null or undefined entries, convert commas to semicolons, then convert to a comma-separated string.
  108. return encodeURIComponent(names
  109. .filter(function (n){
  110. return n !== undefined;
  111. })
  112. .map(function (n){
  113. return (n + "").replace(/,/g, ";");
  114. })
  115. .join());
  116. }
  117.  
  118. // Don't run on frames or iframes
  119. if(window.top !== window.self){
  120. return false;
  121. }
  122.  
  123. if(/\/my\//.test(location.pathname)){
  124. // On a My Profile page
  125. currentPage = "my";
  126. }else if(/\/account\//.test(location.pathname)){
  127. // On a Profile page
  128. currentPage = "account";
  129. }else{
  130. if(cacheName){
  131. // On a Geocache page...
  132. if(!/SideTracked/i.test(cacheName.innerHTML) && !/side tracked/i.test(cacheName.innerHTML)){
  133. // ...but not a SideTracked cache
  134. return;
  135. }
  136. currentPage = "cache";
  137. }else{
  138. currentPage = "profile";
  139. }
  140. }
  141.  
  142. var hider;
  143. switch(currentPage){
  144. case "profile":
  145. if(profileName){
  146. userNames = [profileName.textContent.trim()];
  147. }else if(profileNameOld){
  148. userNames = [profileNameOld.textContent.trim()];
  149. }
  150. break;
  151. default:
  152. if(userField.length > 0){
  153. userNames.push(userField[0].innerHTML.trim());
  154. }
  155. hider = getHiderName();
  156. if(typeof hider !== 'undefined'){
  157. userNames.push(hider);
  158. }
  159. break;
  160. }
  161.  
  162. for(var i = 0; i < userNames.length; i++){
  163. stats[i] = {name: userNames[i]};
  164. }
  165.  
  166. userName = parseNames(userNames);
  167. if(!userName){
  168. console.error("SideTracked Stats: Aborted - couldn't work out user name");
  169. return;
  170. }
  171.  
  172. // Inject widget styling
  173. stsCSS.type = 'text/css';
  174. if(stsCSS.styleSheet){
  175. stsCSS.styleSheet.cssText = css;
  176. }else{
  177. stsCSS.appendChild(document.createTextNode(css));
  178. }
  179. document.head.appendChild(stsCSS);
  180. displayStats(stats.reverse(), currentPage);
  181. }());