This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btdigg.user.js
406 lines (355 loc) · 11.9 KB
/
btdigg.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// ==UserScript==
// @name BTDigg_Chart_Adder
// @namespace BTDigg_Chart_Adder
// @id BTDigg_Chart_Adder
// @description adds BTDigg popularity chart for Bit-torrent magnet-links
// @version 0.2.4
// @author KOLANICH
// @copyright KOLANICH, 2014
// @homepageURL https://github.com/KOLANICH/BTDigg_Chart_Adder/
// @icon https://btdigg.org/favicon.ico
// @license GNU GPL v3
// @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
// @contributionURL https://github.com/KOLANICH/BTDigg_Chart_Adder/fork
// @contributionAmount feel free to fork and contribute
// @include *
// @exclude /https?\:\/\/btdigg\.org.+/i
// @noframes 1
// @run-at document-idle
// @optimize 1
// @resource flotr2lib https://raw.github.com/HumbleSoftware/Flotr2/master/flotr2.min.js
// @resource tooltipcss ./tooltip.css
// @resource preloader ./images/preloader.png
// @btdiggBaseURL http://api.btdigg.org/api/public-8e9a50f8335b964f
// @tooltipWidth 600
// @tooltipHeight 200
// @installationReportIteration 1
// @scriptId 4706
// ==/UserScript==
/*
Preloader by http://preloaders.net/
Tooltip by http://cssarrowplease.com/
This script is distributed under conditions of GNU GPL v3.
*/
/*
Copyright (C) 2013-2014 KOLANICH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var btdiggApiBaseURL=GM_getMetadata("btdiggbaseurl")[0];
var tooltipWidth=GM_getMetadata("tooltipwidth")[0];
var tooltipHeight=GM_getMetadata("tooltipheight")[0];
var preloader=new Image();
preloader.src=GM_getResourceURL("preloader");
var arrowHeight=40;
var parseURIRx=/(\w+)=([^&]+)/ig;
var infohashRx=/[A-F0-9]{40}|[A-Z2-7]{32}/i;
var site=null;
var currentLink=null;
var BTDiggReportingEnabled=GM_getValue("enableBTDiggReporting",-1);
if(BTDiggReportingEnabled==-1){
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. We know nothing about BTDigg true intentions, they may be working for copyrust.\nAnother risk is that BTDigg have ssl certificate only for www.btdigg.com and btdigg.com, but noot for api.btdigg.com, so we have to USE UNENCRYPTED PROTOCOL, WHICH MAKES ANYONE WHO CANE EAVESDROP YOUR INTERNET CONNECTION, INCLUDING GOVERNMENT AGENCIES, KNOW WHAT LINKS DO YOU REQUEST STATISTICS FOR");
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!!!");
alert("Look BTDigg privacy policy...");
GM_openInTab("https://btdigg.org/about/privacypolicy.html");
alert("... and Terms of Service");
GM_openInTab("https://btdigg.org/about/termsofservice.html");
enableBTDiggReporting();
}
reportInstallationIfNeeded();
function GM_xhrPr(obj){
return function(resolve, reject) {
obj.onload=resolve;
obj.onerror=reject;
GM_xmlhttpRequest(obj);
};
}
function reportInstallationIfNeeded(){
const instReptItKey="installationReportIteration";
var instReptIt=GM_getMetadata(instReptItKey.toLowerCase())[0];
if(GM_getValue(instReptItKey,0)<instReptIt){
const scriptId=GM_getMetadata("scriptid")[0];
const greaseForkScripts="https://greasyfork.org/scripts/";
let token="";
let pr=new Promise(GM_xhrPr({url:greaseForkScripts+scriptId, method:"GET", ignoreCache:true}))
.then((xhr)=>findCSRFToken(xhr.responseText))
.then((token)=>{
new Promise(GM_xhrPr({
url:"https://greasyfork.org/script_sets/add_to_set",method:"POST",
ignoreCache:true,
data:"utf8=%E2%9C%93&authenticity_token="+encodeURIComponent(token)+"&action-set=ai-336&script_id="+scriptId,
headers:{"Content-Type":"application/x-www-form-urlencoded"}
}))
.then((xhr)=>findCSRFToken(xhr.responseText))
.then((token)=>{
new Promise(GM_xhrPr({
url:greaseForkScripts+scriptId+"/install-ping?authenticity_token="+token,
method:"POST", ignoreCache:true
}))
.then((xhr)=>GM_setValue(instReptItKey,instReptIt));
});
});
}
}
function findCSRFToken(source){
const tokenFieldRx=/<meta[^<>]*name=(['"])csrf-token\1[^<>]*\/>/i
const valueRx=/content=(["'])([A-Z0-9+=]*)\1/i;
let m=source.match(tokenFieldRx);
if(m[0]){
m=m[0].match(valueRx);
if(m[2])return m[2];
}
return false;
}
function enableBTDiggReporting(){
if(
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 (sword) law. Make sure you wouldn't download anything illegal or copyrighted when you enabled this!!! This feature was implemented because I could implement it and I hope it will help BTDigg to index maget links.")
&&
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."
){
GM_setValue("enableBTDiggReporting",1);
alert("BTDigg is now \"spying\" on you\nYou can disable it via script menu.");
window.location.reload();
return;
}
else{
alert("Wise decision");
};
GM_setValue("enableBTDiggReporting",0);
}
if(BTDiggReportingEnabled==1){
GM_registerMenuCommand("BTDigg: disable reporting",function(){
BTDiggReportingEnabled=0;
GM_setValue("enableBTDiggReporting",BTDiggReportingEnabled);
alert("Espionage stopped!!!");
window.location.reload();
});
}
function checkPlottingLibrary(){
if(window.Flotr === undefined){
eval(GM_getResourceText("flotr2lib"));
}
}
var cssInjected=false;
function checkCssStyle(){
if(!cssInjected)GM_addStyle(GM_getResourceText("tooltipcss"));
}
/*!
@param btdescriptor
{
link: объект ссылки,
infohash:инфохеш,
ещё что-либо
}
*/
function BTDescriptor(uri){
if(uri)this.parseMagnetURI(uri);
console.log(this);
};
BTDescriptor.prototype.parseMagnetURI=function(uri){
parseURIRx.lastIndex=0;
var arr=uri.match(parseURIRx);
for(var i=0;i<arr.length;i++){
var el=arr[i].split('=');
//console.log(el);
this[el[0]]=decodeURIComponent(el[1].replace(/\+/g," "));
}
//console.log(this);
if(this.xt){
var infohash=this.xt.match(infohashRx)[0];
if(infohash)this.infohash=infohash;
}
};
BTDescriptor.parseMagnetURI=function(uri){
var d=new BTDescriptor();
d.parseMagnetURI(uri);
return d;
};
function getMagnetLinksFromPage(){
return Array.prototype.filter.call(document.querySelectorAll("a[href]"),function(lnk){return lnk.protocol=="magnet:";});
}
var isLoading=0;
function requestTorrentPopularityByLink(btdescriptor){
return new Promise(GM_xhrPr({
method:"GET",
url:btdiggApiBaseURL+"/h02?info_hash="+btdescriptor.infohash,
})).then(addPlotToPage.bind(btdescriptor),showError);
}
var tooltip=null;
var plotArea;
function checkTooltip(){
if(!tooltip){
tooltip=document.createElement("div");
tooltip.style.zIndex="2147483647";
tooltip.style.position="absolute";
checkCssStyle();
tooltip.className="BTDigg_Chart_Adder_tooltip";
plotArea=document.createElement("div");
plotArea.style.width=tooltipWidth+"px";
plotArea.style.height=tooltipHeight+"px";
document.body.appendChild(tooltip);
tooltip.appendChild(preloader);
tooltip.appendChild(plotArea);
}
}
var tooltipCurrentWidth;
/*!
@param link DOMNode to where tooltip should be placed
*/
function placeTooltip(link,offsets){
console.log(link);
var linkRect=link.getBoundingClientRect();
console.log(linkRect);
tooltip.style.display="";
tooltip.style.top=(linkRect.top+arrowHeight+window.pageYOffset)+"px";
tooltip.style.left=(linkRect.left-tooltipCurrentWidth/2+linkRect.width/2+window.pageXOffset)+"px";
return tooltip;
}
function tsvToArray(txt){
var arr=txt.split("\n").map(function(el){return el.split("\t").map(function(el){return parseInt(el)});});
if(Number.isNaN(arr[arr.length-1][0]))arr.splice(arr.length-1,1);
return arr;
}
function cloneSize(target,donor){
target.style.width=donor.style.width;
target.style.height=donor.style.height;
};
function addPlotToPage(evt){
isLoading=0;//!< 0 is not loading now, 1 is loading now
//console.log("link descriptor is",this);
//console.log("got text",evt.responseText);
currentLink=this.link;
if(!evt.responseText){
hideTooltip();
GM_notification("Info is not available for this torrent");
return;
}
var arr=tsvToArray(evt.responseText);
console.log("splitted",arr);
checkPlottingLibrary();
placeTooltip(currentLink);
//
var flotrConfig={
xaxis: {
mode: 'time',
timeUnit:"second",
timeMode:'local',
labelsAngle: 45,
showMinorLabels: true,
},
selection: {
mode: 'x'
},
grid: {
minorVerticalLines: true,
},
points: {
show: true,
},
lines: {
show: true,
},
HtmlText: false,
};
if(this.dn)flotrConfig.title=this.dn;
if(this.xl)flotrConfig.subtitle=this.xl+" Bytes";
tooltip.style.width=tooltipWidth+"px";
tooltip.style.height=tooltipHeight+"px";
tooltipCurrentWidth=tooltipWidth;
preloader.style.display="none";
plotArea.style.display="";
console.log(flotrConfig);
Flotr.draw(plotArea, [arr], flotrConfig);
placeTooltip(currentLink);
}
function showError(evt){
console.error(evt.statusText);
}
function hideTooltip(){
currentLink=null;
if(!isLoading)tooltip.style.display="none";
}
function showTooltipForLink(evt){
if(!isLoading){
checkTooltip();
tooltip.style.width=preloader.width+"px";
tooltip.style.height=preloader.height+"px";
tooltipCurrentWidth=preloader.width;
placeTooltip(evt.target);
plotArea.style.display="none";
preloader.style.display="";
isLoading=1;
requestTorrentPopularityByLink(evt.target.magnet);
}
}
/*!
gets some strange info in format
# TSL numbers divided by tab
# NORM numbers divided by tab
every number from top line corresponds number from low line
*/
function getSomethingStrange(btdescriptor){
var data = new FormData();
data.append("info_hash", btdescriptor.infohash);
GM_xmlhttpRequest({
method:"POST",
url:btdiggApiBaseURL+"/h01",
onload:console.log,
onerror:console.warn,
data:data
});
}
/*!
this function is needed to send clicked magnet links to btdigg
or to send info about clicked banners
maybe this will help them to collect magnet links to track their torrents' popularity
in any case enabling this must be voluntarily
*/
function sendClick(href,banner){
var data = new FormData();
if(banner){
data.append("banner", href);
}else{
data.append("magnet", href);
}
console.log("sending click on",link,"...");
GM_xmlhttpRequest({
method:"POST",
url:"https://btdigg.org/click",
onload:console.info,
onerror:console.warn,
data:data
});
}
function processClick(evt){
try{
GM_notification("Info about the click was sent!","BTDigg");
}catch(e){}
sendClick(evt.source.href);
}
/*function processScroll(evt){
console.log(evt);
if(currentLink)placeTooltip(currentLink,{x:evt.mozMovementX,y:evt.mozMovementY});
}*/
function main(){
var links=getMagnetLinksFromPage();
for(var i=0;i<links.length;i++){
links[i].magnet=new BTDescriptor(links[i].href);
if(links[i].magnet.infohash){
links[i].magnet.link=links[i];
links[i].addEventListener("mouseenter",showTooltipForLink,false);
links[i].addEventListener("mouseleave",hideTooltip,false);
if(BTDiggReportingEnabled)links[i].addEventListener("click",processClick,true);
}
}
//document.body.addEventListener("DOMMouseScroll",processScroll,true);
}
main();