Delete build.py
[Misc-UserScripts.git] / Warosu-Images.user.js
blob2666ae708d17cf5cd5ffa967d66e6d69dd6866b8
1 // ==UserScript==
2 // @name         Warosu Images
3 // @namespace    http://tampermonkey.net/
4 // @version      0.2.5
5 // @description  Add images to your posts
6 // @author       ECHibiki-/qa/
7 // @match http://warosu.org/*
8 // @match https://warosu.org/*
9 // @include http://warosu.org/*
10 // @include https://warosu.org/*
11 // @run-at document-body
12 // @grant GM_setValue
13 // @grant GM_getValue
14 // @grant GM_xmlhttpRequest
15 // @connect https://danbooru.donmai.us/
16 // @connect http://danbooru.donmai.us/
17 // @updateURL    https://github.com/ECHibiki/Misc-UserScripts/raw/master/Warosu-Images.user.js
18 // @downloadURL  https://github.com/ECHibiki/Misc-UserScripts/raw/master/Warosu-Images.user.js
19 // ==/UserScript==
21 /**
22 Hold Control and Shift to hide images, but keep threads open.
23 Doesn't interfere with 4chanx and you can still see images it if you mouse over them.
24 Hidden images reapear in 48 hours.
27 (function() {
28     var bowser;
29     var finished = false;
30     var addedImages = [];
32     function getDataUri(url) {
33         /*try{
34             GM_xmlhttpRequest ( {
35                 method:         'GET',
36                 url:            url,
37                 onload:         function (responseDetails) {
39                 }
40             } );
41         }
42         catch(e){
43         }*/
44         return url;
45     }
47     function modifyDOM(){
48         //var later = Date.now();
49         if(finished) {
50             var start = document.getElementById("postform");
51             if (start === null) start = document.body;
52             else start = start.firstChild;
53             if(!secondObserverSet){
54                 //console.log(start);
55                 observer.disconnect();
56                 observeDynamicMutation(start);
57                 secondObserverSet = true;
58             }
59         }
60         else {
61             var start = document;
62         }
63         if(start === null) return;
64         var itterator = document.createTreeWalker(start, NodeFilter.SHOW_ELEMENTS, NodeFilter.SHOW_ELEMENTS);
65         var node = "";
67         //console.log(start);
68         while((node = itterator.nextNode())){
69             var tag = node.tagName;
70             //console.log(tag);
71             if(tag == "BLOCKQUOTE"){   
72                 while((localNode = itterator.nextNode()) && (localTag = localNode.tagName) !== "TABLE"){
73                     var localHREF = localNode.href;
74                     //console.log(localTag);
75                     if(localTag == "A" && (new RegExp("^http(|s):\/\/.+\.(jpg|png|gif|webm|mp4).*$", "g")).test(localHREF)){
76                         //console.log("pass3");
77                         //console.log(localNode);
78                         //console.log(node);
79                         /*var zeroIndex = localHREF.indexOf("http");
80                         var endIndex = localHREF.indexOf(".jpg") + 4;
81                         if(endIndex == (-1 + 4)) endIndex = localHREF.indexOf(".png") + 4;
82                         if(endIndex == (-1 + 4)) endIndex = localHREF.indexOf(".gif") + 4;
83                         if (endIndex == (-1 + 4))endIndex = localHREF.indexOf(".webm") + 5;
84                         var urlLink = localHREF.substring(zeroIndex, endIndex);
85                         var breakVar = false;
86                         addedImages.forEach(function(url){
87                             if(url === urlLink){
88                                 breakVar = true;
89                             }
90                         });*/
91                         //if(breakVar) break;
93                         var parent = node.parentNode;
95                         if(localHREF.indexOf(".webm") != -1 | localHREF.indexOf(".mp4") != -1){
96                             var vid = document.createElement("VIDEO");
97                             vid.setAttribute("src",getDataUri(localHREF));
98                             vid.setAttribute("type",getDataUri(localHREF));
99                             vid.setAttribute("controls", "true");
100                             //console.log(vid);
101                             //img.setAttribute("id","fl" + parent.id.substring(1));
102                             vid.addEventListener("load", function(i){
103                                 //console.log(this);
104                                 this.setAttribute("height",this.height * 0.5);
105                                 if(this.height < 200) this.height = 200;
106                                 else if (this.height > 1000)this.height = 1000;
107                                 else if(this.height == 0){
108                                     this.setAttribute("src", "https://cs.sankakucomplex.com/data/e8/a4/e8a46437c6ebd2350ac354446a5043c8.webm?4741543");
109                                 }
110                             });
111                             vid.addEventListener("error", function(){
112                                 this.setAttribute("src", "https://cs.sankakucomplex.com/data/e8/a4/e8a46437c6ebd2350ac354446a5043c8.webm?4741543");
113                             });
115                             parent.insertBefore(vid, node);                        
117                             //addedImages.push(urlLink); 
118                         }
119                         else{
120                             var aLink = document.createElement("A");
121                             //aLink.setAttribute("class", "fileThumb");
122                             //aLink.setAttribute("href", "javascript:void(0);");
123                             aLink.setAttribute("href", localHREF);
124                             //aLink.setAttribute("target", "_blank");
126                             var img = document.createElement("IMG");
127                             /*img.setAttribute("onclick",            
128                                         "console.log(\"ets\");\
129                             var metaRedirect = document.createElement(\"META\");\
130                             metaRedirect.setAttribute(\"http-equiv\",\"refresh\");\
131 metaRedirect.setAttribute(\"id\", \"redir\"); \
132                             metaRedirect.setAttribute(\"content\",\"0;" + localHREF + "\");"  + 
133                             "document.head.appendChild(metaRedirect);"
134                         );*/
135                             //console.log(urlLink);
136                             img.setAttribute("src",getDataUri(localHREF));
137                             //img.setAttribute("id","fl" + parent.id.substring(1));
138                             img.addEventListener("load", function(i){
139                                 //console.log(this);
140                                 this.setAttribute("height",this.height * 0.5);
141                                 if(this.height < 200) this.height = 200;
142                                 else if (this.height > 1000)this.height = 1000;
143                                 else if(this.height == 0){                       
144                                     this.setAttribute("src", "http://orig01.deviantart.net/a920/f/2010/095/9/b/konata_error_404_by_zarkfx.png");
145                                     this.width = 200;
146                                     this.height = 200;
147                                 }
148                             });
149                             img.addEventListener("error", function(){
150                                 this.setAttribute("src", "http://orig01.deviantart.net/a920/f/2010/095/9/b/konata_error_404_by_zarkfx.png");
151                             });
153                             aLink.appendChild(img);
154                             //console.log(node);
155                             parent.insertBefore(aLink, node);                        
157                             //addedImages.push(urlLink); 
158                         }
159                         break;
160                     }
161                     else if(localTag == "HR"){
163                         break;
164                     }
165                     else continue;
166                 }
167             }            
168         }
169     }
171     //initial onload setup
172     function swapListener(){
173         modifyDOM()  ;
174         //observeDynamicMutation();
175     }
177     //detect page changes
178     function observeDynamicMutation(){
179         var node = document;
180         var observer = new MutationObserver(function callBack(mutations){
181             var breakVar = false;
183             mutations.forEach(function(mutation){if(mutation.addedNodes[0] !== undefined && mutation.addedNodes[0].className !== undefined  && mutation.addedNodes[0].className == "ImageSwap"){breakVar = true;}});
184             if(breakVar) return;
185             var later = Date.now();
186             modifyDOM();
187             //console.log("Hide: " + (Date.now() - later));
188         });
189         var config = {subtree: true, childList:true};
190         observer.observe(node, config);
191     }
193     if (window.top != window.self)  //-- Don't run on frames or iframes
194         return;
196     
197     window.addEventListener("load", function(event){
198         console.log("start");
199         swapListener();
200         console.log("Script loaded: Swap Images");
201     });
203 })();