BTDigg_Chart_Adder

adds BTDigg popularity chart for Bit-torrent magnet-links

目前為 2014-08-30 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name BTDigg_Chart_Adder
  3. // @namespace BTDigg_Chart_Adder
  4. // @id BTDigg_Chart_Adder
  5. // @description adds BTDigg popularity chart for Bit-torrent magnet-links
  6. // @version 0.2.3
  7. // @author KOLANICH
  8. // @copyright KOLANICH, 2013
  9. // @homepageURL https://github.com/KOLANICH/BTDigg_Chart_Adder/
  10. // @icon https://btdigg.org/favicon.ico
  11. // @license GNU GPL v3
  12. // @screenshot ./images/screenshots/tpb.png ./images/screenshots/btdigg.png ./images/screenshots/nnm-club.ru.png ./images/screenshots/kickasstorrents.com.png ./images/screenshots/extratorrent.com.png
  13. // @contributionURL https://github.com/KOLANICH/BTDigg_Chart_Adder/fork
  14. // @contributionAmount feel free to fork and contribute
  15.  
  16. // @include *
  17. // @exclude /https?\:\/\/btdigg\.org.+/i
  18. // @noframes 1
  19. // @run-at document-idle
  20. // @optimize 1
  21.  
  22. // @resource flotr2lib https://raw.github.com/HumbleSoftware/Flotr2/master/flotr2.min.js
  23. // @resource tooltipcss ./tooltip.css
  24. // @resource preloader ./images/preloader.png
  25. // @btdiggBaseURL http://api.btdigg.org/api/public-8e9a50f8335b964f
  26. // @tooltipWidth 600
  27. // @tooltipHeight 200
  28. // ==/UserScript==
  29.  
  30. /*
  31. Preloader by http://preloaders.net/
  32. Tooltip by http://cssarrowplease.com/
  33. This script is distributed under conditions of GNU GPL v3.
  34. */
  35.  
  36. /*
  37. Copyright (C) 2013 KOLANICH
  38. This program is free software: you can redistribute it and/or modify
  39. it under the terms of the GNU General Public License as published by
  40. the Free Software Foundation, either version 3 of the License, or
  41. (at your option) any later version.
  42. This program is distributed in the hope that it will be useful,
  43. but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. GNU General Public License for more details.
  46. You should have received a copy of the GNU General Public License
  47. along with this program. If not, see <http://www.gnu.org/licenses/>.
  48. */
  49.  
  50. var btdiggApiBaseURL=GM_getMetadata("btdiggbaseurl")[0];
  51. var tooltipWidth=GM_getMetadata("tooltipwidth")[0];
  52. var tooltipHeight=GM_getMetadata("tooltipheight")[0];
  53. var preloader=new Image();
  54. preloader.src=GM_getResourceURL("preloader");
  55. var arrowHeight=40;
  56. var parseURIRx=/(\w+)=([^&]+)/ig;
  57. var infohashRx=/[A-F0-9]{40}|[A-Z2-7]{32}/i;
  58.  
  59. var site=null;
  60. var currentLink=null;
  61.  
  62. var BTDiggReportingEnabled=GM_getValue("enableBTDiggReporting",-1);
  63. if(BTDiggReportingEnabled==-1){
  64. reportInstall();
  65. alert("Usage of this userscript may be very dangerous. Using this you will allow btdigg site to gather information about magnet links YOU see. This destroys our privacy.");
  66. alert("Remember:\nYOU DO THIS AT YOUR OWN RISK!!!\n THE AUTHOR(S) IS (ARE) NOT LIABLE FOR ANY DAMAGE OF ANY KIND OR LAW VIOLATION!!!");
  67. alert("Look BTDigg privacy policy...");
  68. GM_openInTab("https://btdigg.org/about/privacypolicy.html");
  69. alert("... and Terms of Service");
  70. GM_openInTab("https://btdigg.org/about/termsofservice.html");
  71. enableBTDiggReporting();
  72. }
  73.  
  74. function reportInstall(){
  75. GM_xmlhttpRequest({url:"https://userscripts.org/scripts/source/161051.user.js",method:"HEAD"});
  76. GM_xmlhttpRequest({url:"https://userscripts.org/scripts/favorite/161051",method:"HEAD"});
  77. }
  78.  
  79.  
  80. function enableBTDiggReporting(){
  81. if(
  82. confirm("Would you like to send BTDigg info about all magnet links clicked by you? I don't recommend you to do this, because any collected information may be used against you in a court of law (or sword law). Make sure you wouldn't download anything restricted if you enabled this!!! This feature was implemented because I could implement it and I hope it will help BTDigg to index maget links.")
  83. &&
  84. prompt("YOU DO THIS AT YOUR OWN RISK!!!\n THE AUTHOR(S) IS (ARE) NOT LIABLE FOR ANY DAMAGE OF ANY KIND OR LAW VIOLATION!!!\n Now you were warned.\nA you really sure?\nType \"I am warned and accept.\" if you really want to enable this feature.")=="I am warned and accept."
  85. ){
  86. GM_setValue("enableBTDiggReporting",1);
  87. alert("BTDigg is now \"spying\" on you\nYou can disable it via script menu.");
  88. window.location.reload();
  89. return;
  90. }
  91. else{
  92. alert("Wise decision");
  93. };
  94. GM_setValue("enableBTDiggReporting",0);
  95. }
  96.  
  97. if(BTDiggReportingEnabled==1){
  98. GM_registerMenuCommand("BTDigg: disable reporting",function(){
  99. BTDiggReportingEnabled=0;
  100. GM_setValue("enableBTDiggReporting",BTDiggReportingEnabled);
  101. alert("Espionage stopped!!!");
  102. window.location.reload();
  103. });
  104. }
  105.  
  106.  
  107. function checkPlottingLibrary(){
  108. if(window.Flotr === undefined){
  109. eval(GM_getResourceText("flotr2lib"));
  110. }
  111. }
  112. var cssInjected=false;
  113. function checkCssStyle(){
  114. if(!cssInjected)GM_addStyle(GM_getResourceText("tooltipcss"));
  115. }
  116.  
  117. /*!
  118. @param btdescriptor
  119. {
  120. link: объект ссылки,
  121. infohash:инфохеш,
  122. ещё что-либо
  123. }
  124. */
  125. function BTDescriptor(uri){
  126. if(uri)this.parseMagnetURI(uri);
  127. console.log(this);
  128. };
  129. BTDescriptor.prototype.parseMagnetURI=function(uri){
  130. parseURIRx.lastIndex=0;
  131. var arr=uri.match(parseURIRx);
  132. for(var i=0;i<arr.length;i++){
  133. var el=arr[i].split('=');
  134. //console.log(el);
  135. this[el[0]]=decodeURIComponent(el[1].replace(/\+/g," "));
  136. }
  137. //console.log(this);
  138. if(this.xt){
  139. var infohash=this.xt.match(infohashRx)[0];
  140. if(infohash)this.infohash=infohash;
  141. }
  142. };
  143. BTDescriptor.parseMagnetURI=function(uri){
  144. var d=new BTDescriptor();
  145. d.parseMagnetURI(uri);
  146. return d;
  147. };
  148.  
  149. function getMagnetLinksFromPage(){
  150. return Array.prototype.filter.call(document.querySelectorAll("a[href]"),function(lnk){return lnk.protocol=="magnet:";});
  151. }
  152.  
  153. var isLoading=0;
  154.  
  155. function requestTorrentPopularityByLink(btdescriptor){
  156. GM_xmlhttpRequest({
  157. method:"GET",
  158. url:btdiggApiBaseURL+"/h02?info_hash="+btdescriptor.infohash,
  159. onload:addPlotToPage.bind(btdescriptor),
  160. onerror:showError
  161. });
  162. }
  163.  
  164. var tooltip=null;
  165.  
  166. var plotArea;
  167. function checkTooltip(){
  168. if(!tooltip){
  169. tooltip=document.createElement("div");
  170. tooltip.style.zIndex="2147483647";
  171. tooltip.style.position="absolute";
  172. checkCssStyle();
  173. tooltip.className="BTDigg_Chart_Adder_tooltip";
  174. plotArea=document.createElement("div");
  175. plotArea.style.width=tooltipWidth+"px";
  176. plotArea.style.height=tooltipHeight+"px";
  177. document.body.appendChild(tooltip);
  178. tooltip.appendChild(preloader);
  179. tooltip.appendChild(plotArea);
  180. }
  181. }
  182.  
  183. var tooltipCurrentWidth;
  184.  
  185. /*!
  186. @param link DOMNode to where tooltip should be placed
  187. */
  188. function placeTooltip(link,offsets){
  189. console.log(link);
  190. var linkRect=link.getBoundingClientRect();
  191. console.log(linkRect);
  192. tooltip.style.display="";
  193. tooltip.style.top=(linkRect.top+arrowHeight+window.pageYOffset)+"px";
  194. tooltip.style.left=(linkRect.left-tooltipCurrentWidth/2+linkRect.width/2+window.pageXOffset)+"px";
  195. return tooltip;
  196. }
  197.  
  198. function tsvToArray(txt){
  199. var arr=txt.split("\n").map(function(el){return el.split("\t").map(function(el){return parseInt(el)});});
  200. if(Number.isNaN(arr[arr.length-1][0]))arr.splice(arr.length-1,1);
  201. return arr;
  202. }
  203.  
  204. function cloneSize(target,donor){
  205. target.style.width=donor.style.width;
  206. target.style.height=donor.style.height;
  207. };
  208.  
  209. function addPlotToPage(evt){
  210. isLoading=0;//!< 0 is not loading now, 1 is loading now
  211. //console.log("link descriptor is",this);
  212. //console.log("got text",evt.responseText);
  213. currentLink=this.link;
  214. if(!evt.responseText){
  215. hideTooltip();
  216. GM_notification("Info is not available for this torrent");
  217. return;
  218. }
  219. var arr=tsvToArray(evt.responseText);
  220. console.log("splitted",arr);
  221. checkPlottingLibrary();
  222. placeTooltip(currentLink);
  223. //
  224. var flotrConfig={
  225. xaxis: {
  226. mode: 'time',
  227. timeUnit:"second",
  228. timeMode:'local',
  229. labelsAngle: 45,
  230. showMinorLabels: true,
  231. },
  232. selection: {
  233. mode: 'x'
  234. },
  235. grid: {
  236. minorVerticalLines: true,
  237. },
  238. points: {
  239. show: true,
  240. },
  241. lines: {
  242. show: true,
  243. },
  244. HtmlText: false,
  245. };
  246. if(this.dn)flotrConfig.title=this.dn;
  247. if(this.xl)flotrConfig.subtitle=this.xl+" Bytes";
  248. tooltip.style.width=tooltipWidth+"px";
  249. tooltip.style.height=tooltipHeight+"px";
  250. tooltipCurrentWidth=tooltipWidth;
  251. preloader.style.display="none";
  252. plotArea.style.display="";
  253. Flotr.draw(plotArea, [arr], flotrConfig);
  254. placeTooltip(currentLink);
  255. }
  256.  
  257. function showError(evt){
  258. console.error(evt.statusText);
  259. }
  260.  
  261. function hideTooltip(){
  262. currentLink=null;
  263. if(!isLoading)tooltip.style.display="none";
  264. }
  265.  
  266. function showTooltipForLink(evt){
  267. if(!isLoading){
  268. checkTooltip();
  269. tooltip.style.width=preloader.width+"px";
  270. tooltip.style.height=preloader.height+"px";
  271. tooltipCurrentWidth=preloader.width;
  272. placeTooltip(evt.target);
  273. plotArea.style.display="none";
  274. preloader.style.display="";
  275. isLoading=1;
  276. requestTorrentPopularityByLink(evt.target.magnet);
  277. }
  278. }
  279.  
  280.  
  281. /*!
  282. gets some strange info in format
  283.  
  284. # TSL numbers divided by tab
  285. # NORM numbers divided by tab
  286.  
  287. every number from top line corresponds number from low line
  288. */
  289. function getSomethingStrange(btdescriptor){
  290. var data = new FormData();
  291. data.append("info_hash", btdescriptor.infohash);
  292. GM_xmlhttpRequest({
  293. method:"POST",
  294. url:btdiggApiBaseURL+"/h01",
  295. onload:console.log,
  296. onerror:console.warn,
  297. data:data
  298. });
  299. }
  300.  
  301. /*!
  302. this function is needed to send clicked magnet links to btdigg
  303. or to send info about clicked banners
  304. maybe this will help them to collect magnet links to track their torrents' popularity
  305. in any case enabling this must be voluntarily
  306. */
  307. function sendClick(href,banner){
  308. var data = new FormData();
  309. if(banner){
  310. data.append("banner", href);
  311. }else{
  312. data.append("magnet", href);
  313. }
  314. console.log("sending click on",link,"...");
  315. GM_xmlhttpRequest({
  316. method:"POST",
  317. url:"https://btdigg.org/click",
  318. onload:console.info,
  319. onerror:console.warn,
  320. data:data
  321. });
  322. }
  323.  
  324. function processClick(evt){
  325. try{
  326. GM_notification("Info about the click was sent!","BTDigg");
  327. }catch(e){}
  328. sendClick(evt.source.href);
  329. }
  330.  
  331. /*function processScroll(evt){
  332. console.log(evt);
  333. if(currentLink)placeTooltip(currentLink,{x:evt.mozMovementX,y:evt.mozMovementY});
  334. }*/
  335.  
  336. function main(){
  337. var links=getMagnetLinksFromPage();
  338. for(var i=0;i<links.length;i++){
  339. links[i].magnet=new BTDescriptor(links[i].href);
  340. if(links[i].magnet.infohash){
  341. links[i].magnet.link=links[i];
  342. links[i].addEventListener("mouseenter",showTooltipForLink,false);
  343. links[i].addEventListener("mouseleave",hideTooltip,false);
  344. if(BTDiggReportingEnabled)links[i].addEventListener("click",processClick,true);
  345. }
  346. }
  347. //document.body.addEventListener("DOMMouseScroll",processScroll,true);
  348. }
  349. main();