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