-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpixivblocker.user.js
684 lines (588 loc) · 24.2 KB
/
pixivblocker.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// ==UserScript==
// @name pixivblocker
// @namespace commies.pixivblocker
// @description nuke shit
// @include /.*\/\/.*pixiv\.net/.*/
// @require http://code.jquery.com/jquery-1.11.2.min.js
// @version 2.0.10
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_listValues
// ==/UserScript==
// the @grant is a workaround for jquery sandboxing or whatever
//utility functions
function arrayContains(needle, arrhaystack) {
return (arrhaystack.indexOf(needle) > - 1);
}
function nukeThumbs(username, nuke) {
$("li.image-item").each(function(v){
var usern = $(this).find($(".ui-profile-popup"));
if (usern.data("user_name") === username)
{
// Element exists with attribute. remove
if(nuke === true){
$(this).remove();
} else {
hideThumbs($(this));
}
}
});
}
function hideThumbs(node){
var target = $("#blockedthumbs");
if(target.length == 0){
var target = $("#wrapper .layout-body");
var temp = $(document.createElement("section"));
temp.attr("id", "blockedthumbscontainer")
.addClass("_unit")
.click(function(){
$("#blockedthumbs").removeClass("pixivblocker_hidden");
})
.append($(document.createElement("h2"))
.text("Blocked Thumbnails")
.addClass("column-title")
)
.append($(document.createElement("h3"))
.text("Click to see the thumbnails!")
)
.append($(document.createElement("div"))
.attr("id", "blockedthumbs")
.addClass("pixivblocker_hidden")
);
target.append(temp);
}
node.appendTo($("#blockedthumbs"));
}
function findAnimationURL(){
return unsafeWindow.pixiv.context.ugokuIllustFullscreenData.src || false;
}
function sescape(v){
return v.replace(/&/, "&").replace(/'/g, "'").replace(/"/g, """);
}
function rewriteImageURL(myurl){
if(typeof myurl != 'undefined'){
var myext = ".png";
if(myurl.indexOf("_m.png") !== -1 ){
myext = ".png";
} else if (myurl.indexOf("_m.jpg") !== -1 ) {
myext = ".jpg";
} else if (myurl.indexOf("_m.gif") !== -1 ) {
myext = ".gif";
} else if(myurl.indexOf("_master1200.jpg") !== -1){
var srcarr = myurl.split(/(\/.*\.net\/)(?:.*)(\/img\/(?:\d*\/)*\d*_)(.*)(?:_)(?:.*)(\.\S*$)/g);
return [srcarr[1] + "img-original" + srcarr[2] + srcarr[3] + srcarr[4], unsafeWindow.pixiv.context.illustId + "_" + srcarr[3] + srcarr[4]];
} else {
var srcarr = myurl.split(/(\/.*\.net\/.*)(?:_)(\S*)(\.\S*$)/g);
return [srcarr[1] + "_big_" + srcarr[2] + srcarr[3], unsafeWindow.pixiv.context.illustId + "_" + srcarr[2] + srcarr[3]];
}
return [myurl.substr(0, myurl.indexOf("_m"+myext)) + myext, "testtest"];
}
return myurl;
}
function checkIfImageExists(myurl){
var extsToCheck = [".jpg",".png"];
for(var i=0, found=false; i<extsToCheck.length || found; i++){
var newurl = myurl.replace(/\.[^/.]+$/, "") + extsToCheck[i];
var img = document.createElement('img');
img.src = newurl;
img.style = "visibility:hidden;"
document.body.appendChild(img);
if( img.height != 0 ){ //exists
document.body.removeChild(img);
return newurl;
}
document.body.removeChild(img);
}
return false;
}
//OOP config object
var configObj = function (prefix, settings, descriptions) {
this.settings = settings;
this.descriptions = descriptions;
this.prefix = prefix;
this.allowDelete = true;
this.allowEdit = true;
this.set = function(name, value) {
//just fucken stringify everything who cares!!!
value = JSON.stringify(value);
//console.log("Set the value of " + this.prefix + name + " to: " + value);
GM_setValue(this.prefix + name, value);
};
this.update = function(name, value){
//super hacky
this.set(name.substr(this.prefix.length), value);
};
this.get = function(name, def) {
if(arrayContains(this.prefix, name)){
name = name.substr(this.prefix.length);
}
var value = GM_getValue(this.prefix + name);
// nothing is set yet, so make sure to set our default for easy configuration
if(typeof value === 'undefined' || value == null) {
value = def || this.settings[name];
if(typeof(value) !== 'undefined') this.set(name, value);
return value;
}
//parse non-strings into an actual usable object
var tempvalue;
try{
var tempvalue = JSON.parse(value);
} catch (e){}
//if(name !== 'debug')
return tempvalue || value;
};
this.reset = function(name) {
if(confirm('Are you sure you want to RESET THIS SETTING???')){
var mypref = this.prefix + name;
GM_deleteValue(mypref);
console.log("Reset the value of " + mypref);
}
};
this.resetAll = function(){
//HACK: greasemonkey keeps having GM_ListValues() break and this is a bad workaround
//https://github.com/greasemonkey/greasemonkey/issues/2033
var keys = cloneInto(GM_listValues(), window);
var mythis = this; //hack
keys.map(function(key) {
if(key.indexOf(mythis.prefix) !== -1){
//console.log("Deleted key: ", key);
GM_deleteValue(key);
}
});
};
this.resetAllContains = function(search){
//HACK: greasemonkey keeps having GM_ListValues() break and this is a bad workaround
//https://github.com/greasemonkey/greasemonkey/issues/2033
var keys = cloneInto(GM_listValues(), window);
var mythis = this; //hack
//use a substr to check if that last section of it is identical or not
keys.map(function(key) {
if(key.indexOf(mythis.prefix) !== -1 && key.substr(key.length - search.length) === search){
console.log("Deleted key: ", key);
GM_deleteValue(key);
}
});
};
this.list = function(){
//HACK: greasemonkey keeps having GM_ListValues() break and this is a bad workaround
//https://github.com/greasemonkey/greasemonkey/issues/2033
//weird map thing also... idk this is really kind of dumb
var keys = cloneInto(GM_listValues(), window);
var mythis = this; //HACK: inline functions use wrong this :^)
for(var s in this.settings){
if(!arrayContains(this.prefix + s, keys)){
keys[keys.length] = this.prefix + s;
}
}
keys = keys.filter(function(key) {
if(key.indexOf(mythis.prefix) !== -1){
return true;
}
});
keys = keys.map(function(key) {
var t = {};
t[key] = mythis.get(key);
t[key] = JSON.parse(GM_getValue(key));
return t;
});
return keys;
};
this.prettyPrint = function(node, key, value){
//used in clicks
var mythis = this;
// because we automatically prefix keys
var prefixless = key.substr(this.prefix.length);
var myrow = $(document.createElement("tr"));
node.append(myrow);
myrow.append($(document.createElement("td"))
.text(this.descriptions[prefixless] || key)
.addClass("pixivblocker_config key")
);
if(this.allowEdit === true){
var mycell = $(document.createElement("td"))
.addClass("pixivblocker_config value " + typeof(value));
var myinput = $(document.createElement("input")).attr("value", value);
console.log(node, key, value);
switch(typeof value){
case "number":
myinput.attr("type", "number");
case "string":
default:
if(value.toString() === 'false' || value.toString() === 'true'){
myinput.attr("type", "checkbox").attr("checked", value.toString() === 'true')
.click(function(){ console.log(key, this.checked); mythis.update(key, this.checked); });
} else {
myinput.change(function(){
var myval = this.value;
try{ myval = JSON.parse(this.value); } catch(e) { /* this is mostly just for numbers anyways */ }
mythis.update(key, myval);
});
}
break;
}
myrow.append(mycell.append(myinput));
} else {
myrow.append($(document.createElement("td"))
.text(value)
.addClass("pixivblocker_config value " + typeof(value))
);
}
if(this.allowDelete === true){
myrow.prepend($(document.createElement("td"))
.text("X")
.click(function(){ mythis.reset(prefixless); $(this).parent().remove(); })
.css("cursor", "pointer")
.addClass("pixivblocker_config delete")
)
}
};
this.prettyPrintList = function(target){
var mytable = $(document.createElement("table"));
mytable.addClass("pixivblocker_config");
target.append($(document.createElement("h3"))
.addClass("blocksubhead")
.text(this.displayName || "Header")
).append($(document.createElement("div"))
.addClass("section")
.append(mytable)
);
var myrow = $(document.createElement("tr"));
mytable.append($(document.createElement("thead")).append(myrow));
myrow.append($(document.createElement("th"))
.text("Key")
.addClass("pixivblocker_config key")
)
.append($(document.createElement("th"))
.text("Value")
.addClass("pixivblocker_config value")
);
if(this.allowDelete === true){
myrow.prepend($(document.createElement("th"))
.text("Reset")
.addClass("pixivblocker_config delete")
);
}
var mylist = this.list();
for(var i = 0; i < mylist.length; i++){
var mykey = Object.keys(mylist[i])[0];
//console.log(i, mykey, mylist[i], mylist[i][mykey]);
this.prettyPrint(mytable, mykey, mylist[i][mykey]);
}
};
this.exportSetting = function (settvar){
return this.get(settvar);
};
this.importSetting = function (settvar, values){
var mythis = this;
try {
var wef = values;
if(wef){
wef = wef.replace(/([^,]+)/g, "\"$&\"");
wef = "[" + wef + "]";
wef = JSON.parse(wef);
mythis.set(settvar, wef);
}
} catch (e) {
console.log('Error: ', e);
alert("Error: ", e);
}
};
this.listManage = function(thelist, theuser, noadd){
var shitlist = this.get(thelist);
if(typeof(noadd) === 'undefined') noadd = false;
if(noadd === true){
alert("You need to specify a user to unblock!");
}else if (!theuser) {
theuser = prompt("Enter Username Here", "Type here");
}
if(arrayContains(theuser, shitlist)){
if(confirm('Are you sure you want to unblock this user?')){
shitlist.splice(shitlist.indexOf(theuser), 1);
this.set(thelist, shitlist );
}
return false;
}
//user wasn't in list and noadd is not defined AND false
if (noadd === false && confirm('Add to Filterlist?')){
shitlist[shitlist.length] = theuser;
this.set(thelist, shitlist);
return true;
}
return false;
};
};
//actually create our config objects with default parameters
var data = new configObj( "setting_",
{
'shitusers': [],
}
);
data.allowEdit = false;
data.allowDelete = false;
data.displayName = "Shit Users";
var config = new configObj( "config_",
{
'showlargeimages': true,
'showlargeimagesall': false,
'hideshitusers': true,
'nukeshitusers': true,
'blockads': false,
},
{
'showlargeimages': "Large Image Download (View Image Page)",
'showlargeimagesall': "Large Image Download (Everywhere else)",
'hideshitusers': "Allows you to hide shit users",
'nukeshitusers': "If hiding shit users, completely nukes the images",
'blockads': "Block the text ads aren't blocked by adblock like a douche :)",
}
);
config.displayName = "Configuration";
function populateCSS(){
var ourcss = "";
ourcss += "*::selection { background: #258FB8; color:#fff; }"; //pixiv has a really light selection color by default which is dumb
ourcss += "*::-moz-selection { background: #258FB8; color:#fff; }"; //pixiv has a really light selection color by default which is dumb
ourcss += ".image-item { height:auto !important; }"; //fixes heights being cut off
ourcss += ".popular-introduction { position:relative !important; top: 0px !important; left: 0px !important; }"; //fixes popular thing being in the way terribly
ourcss += ".pixivblocker_minus { padding:0px 4px; background: #77FF88; color:#FFF; cursor:pointer;}";
ourcss += ".pixivblocker_plus { padding:0px 4px; background: #FF7788; color:#FFF; cursor:pointer; margin-left:5px;}";
ourcss += ".pixivblocker_shitdiv {width:95%; margin:auto; margin-bottom: 15px; border-bottom: 2px dashed rgba(0,0,0,.2);}";
ourcss += ".pixivblocker_shitdiv ul {max-height: 0; transition: max-height 1s ease-out .4s; overflow: hidden; list-style: none; display:block;}";
ourcss += ".pixivblocker_shitdiv:hover ul {max-height:9999px; transition: max-height 1s ease-in .4s;}";
ourcss += ".pixivblocker_shitdiv h3 { font-weight: bolder; font-size: 110%; margin-bottom: 5px;}";
ourcss += ".pixivblocker_shitdiv h3 small { font-size: 70%;}";
ourcss += ".pixivblocker_shitdiv ul li { padding-left:3px; display:inline-block; background: rgba(0,0,0,.1); margin:5px;}";
//ourcss += ".pixivblocker_shitusers li:after { content: ', '}";
//ourcss += ".pixivblocker_shitusers li:last-child:after {content: ''; }";
ourcss += ".pixivblocker_footer h2 { font-size: 130%; font-weight: bold;}";
ourcss += ".pixivblocker_footer .container { padding: 10px; }";
ourcss += ".pixivblocker_hidden {display:none;}";
//now we actually add our CSS to the page
if (document.getElementById("PB_CSS") !== null){
document.getElementById("PB_CSS").innerHTML = ourcss;
} else {
var head = document.head;
if (!head) { return; }
var style = document.createElement('style');
style.type = 'text/css';
style.id = "PB_CSS";
style.innerHTML = ourcss;
head.appendChild(style);
}
}
var addedshitusers = false;
function addShitUserList(){
if(!addedshitusers){
var shitusers = data.get('shitusers');
var ourdiv = $('#pixivblocker_shitdiv');
var datalist = $(document.createElement('datalist'));
for(var v = 0; v < shitusers.length; v++){
datalist.append($(document.createElement('option'))
.attr("value", shitusers[v])
);
}
datalist.attr("id", "shitusers").appendTo(ourdiv);
}
addedshitusers = true;
}
function removeads(){
//generic
$(".ad, .ads_anchor, .multi-ads-area, .pay-button, .showcase-reminder, .require-premium").remove();
//homepage
$("aside.comic-container a.hover-item, aside.comic-container div.area_inside, .ui-layout-west .area_new:nth-last-child(2), .ui-layout-west .area_new:nth-last-child(1)").remove();
//prem offer below username
$("a[href=\"/premium.php?ref=premium_campaign\"]").remove();
//prem offer below section thumbnails
$("a[href=\"/premium.php?ref=popular_d_body\"]").remove();
}
function detectPageStuff(){
var animcontain = $("._ugoku-illust-player-container");
var imgcontain = $(".works_display div img");
var ismanga = $(".works_display a");
var mangacontain = $("#main .manga .item-container img");
if(animcontain.length > 0){
var coolbutton = document.createElement('a');
coolbutton.setAttribute("class", "_button");
coolbutton.setAttribute("href", this.findAnimationURL());
coolbutton.innerHTML += "Download Animation As .ZIP";
$("._work-detail-unit .action").first().prepend(coolbutton);
} else if(imgcontain.length && ismanga.length < 1 ){
var myurl = imgcontain.attr('src');
myurl = rewriteImageURL(myurl)[0];
myurl = checkIfImageExists(myurl);
var coolbutton = document.createElement('a');
coolbutton.setAttribute("class", "_button");
if(myurl) {
coolbutton.setAttribute("href", myurl);
coolbutton.innerHTML += "Download Full Size Image";
} else {
coolbutton.innerHTML += "Something went wrong!";
}
$("._work-detail-unit .action").first().prepend(coolbutton);
} else if(mangacontain.length > 0) {
var mangadiv = document.createElement('div');
mangadiv.id = "mangaimgdownload";
mangadiv.style = "max-width: 250px; margin: 10px; position: fixed; overflow-y:auto; max-height: calc(100% - 20px);";
mangadiv.innerHTML = "<strong>Download HI-RES Images</strong><p><small>DownThemAll regex: </small><input type=\"text/css\" value=\"/.*_p\\d*\\.jpg/\"></input></p>";
$("#main .manga").prepend(mangadiv);
mangacontain.each(function(v){
var src = $(this).data("src");
var srcarr = rewriteImageURL(src);
src = srcarr[0];
if(typeof src != 'undefined'){
var currname = srcarr[1];
var coolbutton = document.createElement('a');
//coolbutton.setAttribute("class", "_button");
coolbutton.setAttribute("href", src);
coolbutton.target = '_blank';
coolbutton.download = srcarr[1];
coolbutton.style = "display: block;";
coolbutton.innerHTML += "Download " + srcarr[1];
$("#mangaimgdownload").append(coolbutton);
}
});
}
}
function doPageSpecific(t, v, loc){
if(config.get("showlargeimagesall") === true){
switch(loc){
case "member_illust.php":
var myurl = t.find("img").attr('src');
myurl = rewriteImageURL(myurl)[0];
myurl = checkIfImageExists(myurl);
t.append($(document.createElement('a'))
.addClass("_button pixivblocker_viewlarge")
.attr("href", myurl)
.text("view large")
.attr("title", "Download Large")
);
break;
default:
console.log("Basic page!");
}
}
}
function determinePage(){
var loc = window.location.pathname.substr(1); //get our page/queries, and remove leading slash
var te = loc.indexOf("/"); //still have slash?
if(te !== -1){
loc = loc.substr(0,te+1); //trim everything after slash
} else {
te = loc.indexOf(".php");
if (te !== -1) {
loc = loc.substr(0,te+4); //trim everything after ".php"
}
}
return loc;
}
function addConfigPanelThing(){
var mydiv = $(document.createElement('div')).addClass("container");
$("#wrapper").append(
$(document.createElement('div'))
.addClass("footer pixivblocker_footer")
.append(mydiv)
);
mydiv.append($(document.createElement('h2'))
.text("Pixiv Blocker Settings")
);
var configbody = $(document.createElement('div'));
mydiv.append(configbody);
config.prettyPrintList(configbody);
}
function init() {
var shitusers = data.get('shitusers');
if(shitusers && typeof shitusers !== "undefined" && shitusers !== "null") {
console.log("[PixivBlocker] Currently blocking " + shitusers.length + " shit users.");
}
var testElements = $("li.image-item");
var pageUrl = determinePage();
if(config.get("blockads") === true){
removeads();
}
//go through list of thumbnails and add stuff to them
$("li.image-item").each(function(v){
var t = $(this);
doPageSpecific(t, v, pageUrl);
if(config.get("hideshitusers") === true){
var username = t.find($(".ui-profile-popup"));
if(username.data("user_name") !== 'undefined'){
var u = "" + username.data("user_name");
u = sescape(u);
//if shit user
if (arrayContains(u, shitusers)) {
if(config.get("nukeshitusers") === true){
t.remove();
} else {
hideThumbs(t);
}
}
else{
username.parent().append($(document.createElement('span'))
.addClass("pixivblocker_plus")
.click(function(){
if(data.listManage('shitusers', u)){
nukeThumbs(u, config.get("nukeshitusers"));
}
})
.text("🚫")
.attr("title", "Add user to pixivblocker")
.appendTo(username.parent())
);
}
}
}
});
addConfigPanelThing();
if(config.get("hideshitusers") === true){
//add list of shitusers at bottom of page
var targetcontainer = $(".pixivblocker_footer");
//we should have found somewhere to put it, but it's possible we haven't so
if(targetcontainer.length !== 0){
targetcontainer.append($(document.createElement('div'))
.attr("id", "pixivblocker_shitdiv").addClass("pixivblocker_shitdiv")
.append($(document.createElement('h3'))
.html("Shit users you've blocked:")
)
.append($(document.createElement("input"))
.attr("list", "shitusers")
.attr("id", "shituserinput")
.focus(function(){addShitUserList();})
)
.append($(document.createElement("button"))
.text("Unblock User")
.click(function(){
data.listManage("shitusers", $("#shituserinput").val(), true);
})
)
.append($(document.createElement("hr")))
.append($(document.createElement("br")))
.append($(document.createElement('h3'))
.html("Shit user import/export:")
)
.append($(document.createElement("input"))
.attr("id", "shituserimportexport")
)
.append($(document.createElement("br")))
.append($(document.createElement('button'))
.text("!! Unblock all users !!")
.click(function(){ data.reset("shitusers"); })
)
.append($(document.createElement('button'))
.text("EXPORT")
.click(function(){
$("#shituserimportexport").val(data.exportSetting("shitusers"));
})
)
.append($(document.createElement('button'))
.text("IMPORT")
.click(function(){
data.importSetting("shitusers", $("#shituserimportexport").val());
})
)
)
}
}
detectPageStuff();
}
populateCSS();
init();