Help icon and small things
[4Free-FSE.git] / Danbooru-Image-Adder.user.js
blob20f4bad5a18dc37f31ccd999d2b0ce42d453e512
1 // ==UserScript==
2 // @name         Danbooru-Image-Adder
3 // @namespace    http://tampermonkey.net/
4 // @version      3.1
5 // @description  Add images to posts
6 // @author       ECHibiki /qa/
7 // @match *://boards.4chan.org/*
8 // @grant         GM_xmlhttpRequest
9 // @updateURL    https://github.com/ECHibiki/4chan-UserScripts/raw/master/Danbooru-Image-Adder.user.js
10 // @downloadURL  https://github.com/ECHibiki/4chan-UserScripts/raw/master/Danbooru-Image-Adder.user.js
11 // @run-at document-start
12 // ==/UserScript==
14 function alert4ChanX(message, type){
15     var detail = {type: type, content: message, lifetime: 10};
16     if (typeof cloneInto === 'function') {
17         detail = cloneInto(detail, document.defaultView);
18     }
19     var event = new CustomEvent('CreateNotification', {bubbles: true, detail: detail});
20     document.dispatchEvent(event);
23 var number_of_posts = 0;
24 var page_number;
25 var json_page;
26 var json_tag;
27 var smallest_tag_size;
29 var top_page_max =  10000000;
30 var top_page = top_page_max;
31 var maximum_attempts = 20;
32 var number_of_attempts = maximum_attempts ;
34 var img_URL = "";
35 var send_URL = "";
36 var old_tags_before_change = "";
38 var timeout = false;
39 var failed_to_find_required_tags_state = false;
40 var tag_incorrect_state = false;
41 var tool_top_visible = false;
43 var time_max = 10;
44 var time = time_max;
45 var intervalFunction;
46 var timeout_functions = [];
47 var json_page_numbers_used = Array();
48 var previous_images = [];
49 var taggingFunction;
51 window_displayed = false;
53 var help_icon_source = " data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4QA6RXhpZgAATU0AKgAAAAgAA1EQAAEAAAABAQAAAFERAAQAAAABAAAAAFESAAQAAAABAAAAAAAAAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAw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
58 document.addEventListener("QRDialogCreation", function(e){
59                 //create custom interface
60                 enhance4ChanX();
61                 //ENHANCE DUMP TABS (COVER, 482PX - 482PX)
62                 //DUMP LIST MAX-HEIGHT TO 490
63                 
64                 var width = parseInt(localStorage.getItem("width_DIA"));                
65                 var qr_width = parseInt(localStorage.getItem("qr_width_DIA"));          
66                 var height = parseInt(localStorage.getItem("height_DIA"));      
67         
68                 if(width === null) width = 400;
69                 if(qr_width === null) width = 480;
70                 if(height === null) height = 400;
72                 document.getElementById("fourchanx-css").textContent += ".qr-preview { height:" +  height + "px; width: " + width +  "px; left:8%;background-size: cover;}";
73                 document.getElementById("fourchanx-css").textContent += "#dump-list { min-height: " + (width - 20) +  "px; width: " + (qr_width) + "px;}";
75 }, false);
77 document.addEventListener("4chanXInitFinished", imageAdderButton);
79 //Alter 4chanX interface
80 var enhance4ChanX = function(){
81     var qr_window = document.getElementById("qr");
83     //check if elements already exist
84         /*Probably Depreciated*/
85     // if(document.getElementById("qrImages") !== null){
86         // qr_window.removeChild(document.getElementById("qrImages"));
87         // clearInterval(taggingFunction);
88         // //4chanx autodeletes images
89         // clearImage();
90     // }
91         
92     var imagedump_opener = document.getElementById("dump-button");
93     if(imagedump_opener !== null){imagedump_opener.click();}
94     else{return;}
96     var imagedump_file_list = document.getElementById("dump-list");
97     var filename_container = document.getElementById("qr-filename-container");
99     //used for setting and unsetting high resolution thumbs for dump list.
100     var dumplist_image = "";
101     var previous_dumplist_image = "";
102     var observer = new MutationObserver(function(mutate){
103         dumplist_image = imagedump_file_list.firstChild.style.backgroundImage;
104         if(dumplist_image !== previous_dumplist_image && img_URL !== ""){
105             imagedump_file_list.firstChild.style.backgroundImage = "url(" + img_URL + ")";
106             previous_dumplist_image = imagedump_file_list.firstChild.style.backgroundImage;
107         }
108         else if (img_URL == ""){
109         }
110     });
111     observer.observe(imagedump_file_list , {attributes: true,subtree:true, chilimagedump_file_list: true, characterData: true });
112     //make the image clear button clear images;
113     document.getElementById("qr-filerm").addEventListener("click", clearImage);
115     //image setting html elements.
116     var qr_image_adder_table = document.createElement("TABLE");
117     qr_image_adder_table.setAttribute("id", "qrImages");
118     qr_image_adder_table.setAttribute("style", "text-align:center");
119     qr_window.appendChild(qr_image_adder_table);
121     var options_row = document.createElement("TR");
122     options_row.setAttribute("ID", "or");
123     options_row.setAttribute("style", "margin:5px;");
124     qr_image_adder_table.appendChild(options_row);
125     var checkbox_safe = document.createElement("INPUT");
126     checkbox_safe.setAttribute("id", "safe");
127     checkbox_safe.setAttribute("type", "checkbox");
128     var checkbox_safe_text  = document.createTextNode("Safe");
129     var checkbox_questionable= document.createElement("INPUT");
130     checkbox_questionable.setAttribute("id", "questionable");
131     checkbox_questionable.setAttribute("type", "checkbox");
132     var checkbox_questionable_text= document.createTextNode("Questionable");
133     var checkbox_explicit = document.createElement("INPUT");
134     checkbox_explicit.setAttribute("id", "explicit");
135     checkbox_explicit.setAttribute("type", "checkbox");
136     var checkbox_explicit_text = document.createTextNode("Explicit");
138     options_row.appendChild(checkbox_safe_text);
139     options_row.appendChild(checkbox_safe);
140     options_row.appendChild(checkbox_questionable_text);
141     options_row.appendChild(checkbox_questionable);
142     options_row.appendChild(checkbox_explicit_text);
143     options_row.appendChild(checkbox_explicit);
145         option_text_size = "18";
147     var image_tagging_row = document.createElement("TR");
148         
149         var help_icon_container = document.createElement("A");
150         help_icon_container.href = "javascript:void(0)";
151         help_icon_container.title = "Click to View Help!";
152         var help_icon = document.createElement("IMG");
153         help_icon.setAttribute("style", "height:" + option_text_size * 1.25 + "px;margin:-4px 10px");
154         help_icon.src = help_icon_source;
156         help_icon_container.appendChild(help_icon);
157         image_tagging_row.appendChild(help_icon_container);
159         var tooltip_div = document.createElement("DIV");
160         tooltip_div.innerHTML = "Insert Tags to search from danbooru in the text box to the side.<br/>The URL for the image will be bellow. Some browsers such as chrome allow you to select this text<br/>Do Not Use \"order:\" tags<br/>Do Not Use \"rating:\" tags<br/>For more speed uncheck all boxes!";
161         tooltip_div.setAttribute("style", "z-index:9;padding:5px;border:1px solid black;background-color:white;word-wrap:break-word;display:none;position:absolute;");
162         help_icon_container.addEventListener("click", function(ev){
163                 if(tool_top_visible)
164                         tooltip_div.setAttribute("style", "z-index:9;padding:5px;border:1px solid black;background-color:white;word-wrap:break-word;display:none;position:absolute;");
165                 else
166                         tooltip_div.setAttribute("style", "z-index:9;padding:5px;border:1px solid black;background-color:white;word-wrap:break-word;display:block;position:absolute;"
167                                 + "left:" +  (ev.clientX - qr_window.getBoundingClientRect().x) +
168                                 "px;top:" +  (ev.clientY - qr_window.getBoundingClientRect().y ) + "px;");
169                 tool_top_visible = !tool_top_visible;
170         });
171         qr_window.appendChild(tooltip_div);
173     var second_row_nodes = [
174         document.createTextNode("Tags: "),
175         document.createElement("INPUT"),
176         document.createElement("INPUT"),
177         document.createElement("A"),
178         document.createElement("INPUT"),
179     ];
180     second_row_nodes.forEach(
181         function(node){
182             image_tagging_row.appendChild(node);
183         });
184     qr_image_adder_table.appendChild(image_tagging_row);
186     var auto_complete_row = document.createElement("TR");
187     auto_complete_row.setAttribute("ID", "acr");
188     auto_complete_row.setAttribute("style", "margin:5px;");
189     qr_image_adder_table.appendChild(auto_complete_row);
191     second_row_nodes[1].setAttribute("ID", "tags");
192     second_row_nodes[1].setAttribute("style", "width:44.9%;"+"font-size:" + option_text_size + "px");
193     second_row_nodes[3].setAttribute("ID", "timer");
194     second_row_nodes[3].setAttribute("style", "width:20%;margin:0 5px");
195     second_row_nodes[4].setAttribute("ID", "urlContainer");
196     second_row_nodes[4].setAttribute("style", "width:75%;margin:5px -25px");
197     second_row_nodes[4].setAttribute("disabled", "");
199     var tag_input_node = second_row_nodes[1];
201     second_row_nodes[2].setAttribute("ID", "imageButton");
202     second_row_nodes[2].setAttribute("type", "button");
203     second_row_nodes[2].setAttribute("value", "Set Image");
205     //event listener logic
206     second_row_nodes[2].addEventListener("click", buttonClickFunction);
208         //textarea expansion;
209         qr_window.getElementsByTagName("TEXTAREA")[0].style.width = "110%";
210     //ping every 0.5s for changes
211     taggingFunction = setInterval(
212         function(){setTagInterface(tag_input_node, auto_complete_row, second_row_nodes);},
213         500);
214         qr_window.appendChild(document.createElement("hr"));
217 //settings for time expiration on image hiding
218 function imageAdderWindow(){
219     var style = document.createElement('style');
220     style.innerHTML = ".inputs{background-color:rgb(200,200,200);margin:5px 7px;width:100px;}";
221     document.body.appendChild(style);
223     var background_div = document.createElement("div");
224     background_div.setAttribute("style", "border:solid 1px black;position:fixed;width:100%;height:100%;background-color:rgba(200,200,200,0.3);top:0;left:0;display:none; z-index:9");
225     background_div.setAttribute("id", "image_adder_Background");
226     document.body.appendChild(background_div);
227     background_div.addEventListener("click", imageAdderToggle);
229     var window_div = document.createElement("div");
230     window_div.setAttribute("style", "border:solid 1px black;position:fixed;width:400px;background-color:rgb(200,200,200);left:40%;top:20%;margin-bottom:0;  display:none; z-index:10");
231     window_div.setAttribute("id", "image_adder_Window");
233     var close_div = document.createElement("div");
234     close_div.setAttribute("style", "border:solid 1px black;position:absolute;width:25px;height:25px;background-color:rgba(255,100,90,0.9); right:3px;top:3px; z-index:10");
235     close_div.addEventListener("click", imageAdderToggle);
236     window_div.appendChild(close_div);
238     var title_para = document.createElement("p");
239     title_para.setAttribute("style", "margin-left:5px;margin-top:5px");
240     var title_text = document.createTextNode("Image Adder Settings");
241     title_para.appendChild(title_text);
242     window_div.appendChild(title_para);
244     var container_div = document.createElement("div");
245     container_div.setAttribute("style","background-color:white;margin:auto;padding:5px;");
246     window_div.appendChild(container_div);
247         
248         var radio_table = document.createElement("TABLE")
249         radio_table.setAttribute("style", "text-align:center;margin-left:5px");
251         var v_large_row = document.createElement("TR");
252         var v_large_label_col = document.createElement("TD");
253     var v_large_label = document.createElement("label");
254     var v_large_text = document.createTextNode("Very Large: ");//489
255     v_large_label.appendChild(v_large_text);
256         v_large_label_col.appendChild(v_large_label);
257     v_large_row.appendChild(v_large_label_col);
258         var v_large_radio_col = document.createElement("TD");
259     var v_large_input = document.createElement("input");
260     v_large_input.setAttribute("id", "v_large");
261         v_large_input.setAttribute("type", "radio");
262         v_large_input.setAttribute("name", "preivew-size");
263         v_large_input.setAttribute("style", "display:inline");  
264         v_large_radio_col.appendChild(v_large_input);
265         v_large_row.appendChild(v_large_radio_col);
266         v_large_input.addEventListener("click", function(){
267                 document.getElementById("width_DIA").value = 489;
268                 document.getElementById("height_DIA").value = 489;
269         });
271         radio_table.appendChild(v_large_row);
272         
273         var large_row = document.createElement("TR");
274         var large_label_col = document.createElement("TD");
275     var large_label = document.createElement("label");
276     var large_text = document.createTextNode("Large: ");//400
277     large_label.appendChild(large_text);
278         large_label_col.appendChild(large_label);
279     large_row.appendChild(large_label_col);
280         var large_radio_col = document.createElement("TD");
281     var large_input = document.createElement("input");
282     large_input.setAttribute("id", "large");
283         large_input.setAttribute("type", "radio");
284         large_input.setAttribute("name", "preivew-size");
285         large_input.setAttribute("style", "display:inline");    
286         large_radio_col.appendChild(large_input);
287         large_row.appendChild(large_radio_col);
288         large_input.addEventListener("click", function(){
289                 document.getElementById("width_DIA").value = 400;
290                 document.getElementById("height_DIA").value = 400;
291         });
292         
293         radio_table.appendChild(large_row);
294         
295         var medium_row = document.createElement("TR");
296         var medium_label_col = document.createElement("TD");
297     var medium_label = document.createElement("label");
298     var medium_text = document.createTextNode("Medium: ");//300
299     medium_label.appendChild(medium_text);
300         medium_label_col.appendChild(medium_label);
301     medium_row.appendChild(medium_label_col);
302         var medium_radio_col = document.createElement("TD");
303     var medium_input = document.createElement("input");
304     medium_input.setAttribute("id", "medium");
305         medium_input.setAttribute("type", "radio");
306         medium_input.setAttribute("name", "preivew-size");
307         medium_input.setAttribute("style", "display:inline");   
308         medium_radio_col.appendChild(medium_input);
309         medium_row.appendChild(medium_radio_col);
310         medium_input.addEventListener("click", function(){
311                 document.getElementById("width_DIA").value = 300;
312                 document.getElementById("height_DIA").value = 300;
313         });
315         radio_table.appendChild(medium_row);
316         
317         var small_row = document.createElement("TR");
318         var small_label_col = document.createElement("TD");
319     var small_label = document.createElement("label");
320     var small_text = document.createTextNode("Very Large: ");//200
321     small_label.appendChild(small_text);
322         small_label_col.appendChild(small_label);
323     small_row.appendChild(small_label_col);
324         var small_radio_col = document.createElement("TD");
325     var small_input = document.createElement("input");
326     small_input.setAttribute("id", "small");
327         small_input.setAttribute("type", "radio");
328         small_input.setAttribute("name", "preivew-size");
329         small_input.setAttribute("style", "display:inline");    
330         small_radio_col.appendChild(small_input);
331         small_row.appendChild(small_radio_col);
332         small_input.addEventListener("click", function(){
333                 document.getElementById("width_DIA").value = 200;
334                 document.getElementById("height_DIA").value = 200;
335         });
336         
337         radio_table.appendChild(small_row);
338         
339         var width_row = document.createElement("TR");
340         var width_label_col = document.createElement("TD");
341     var width_label = document.createElement("label");
342     var width_text = document.createTextNode("Width: ");//W
343     width_label.appendChild(width_text);
344         width_label_col.appendChild(width_label);
345     width_row.appendChild(width_label_col);
346         var width_radio_col = document.createElement("TD");
347     var width_input = document.createElement("input");
348     width_input.setAttribute("id", "width_DIA");
349         width_input.setAttribute("type", "text");
350         width_input.setAttribute("name", "preivew-size");
351         width_input.setAttribute("style", "width:20%");         
352         width_radio_col.appendChild(width_input);
353         width_row.appendChild(width_radio_col);
354         var width = localStorage.getItem("width_DIA");
355         if(width === null) width = 400;
356         width_input.setAttribute("value", width);
357                 
358         radio_table.appendChild(width_row);
359         
360         var height_row = document.createElement("TR");
361         var height_label_col = document.createElement("TD");
362     var height_label = document.createElement("label");
363     var height_text = document.createTextNode("Height: ");//H
364     height_label.appendChild(height_text);
365         height_label_col.appendChild(height_label);
366     height_row.appendChild(height_label_col);
367         var height_radio_col = document.createElement("TD");
368     var height_input = document.createElement("input");
369     height_input.setAttribute("id", "height_DIA");
370         height_input.setAttribute("type", "text");
371         height_input.setAttribute("name", "preivew-size");
372         height_input.setAttribute("style", "width:20%");        
373         height_radio_col.appendChild(height_input);
374         height_row.appendChild(height_radio_col);
375         var height = localStorage.getItem("height_DIA");
376         if(height === null) height = 400;
377         height_input.setAttribute("value", height);
378         
379         radio_table.appendChild(height_row);
380         
381         container_div.appendChild(radio_table); 
382         container_div.appendChild(document.createElement("hr"));
383         
384     var qr_width_label = document.createElement("label");
385     var qr_width_text = document.createTextNode("Quick Reply Min Width: ");//H
386     qr_width_label.appendChild(qr_width_text);
387     var qr_width_input = document.createElement("input");
388     qr_width_input.setAttribute("id", "qr_width_DIA");
389         qr_width_input.setAttribute("type", "text");
390         qr_width_input.setAttribute("name", "preivew-size");
391         qr_width_input.setAttribute("style", "width:20%");
392         var qr_width = localStorage.getItem("qr_width_DIA");
393         if(qr_width === null) qr_width = 480;
394         qr_width_input.setAttribute("value", qr_width);
396         
397         container_div.appendChild(qr_width_label);
398         container_div.appendChild(qr_width_input);
399         container_div.appendChild(document.createElement("hr"));
400         
401     var set_button = document.createElement("input");
402     set_button.setAttribute("type", "button");
403     set_button.setAttribute("id", "setTime");
404     set_button.setAttribute("value", "Set Preview Size");
405         
406     set_button.addEventListener("click", function(){
407         if (storageAvailable('localStorage')) {
408                         var width = parseInt(document.getElementById("width_DIA").value);       
409             localStorage.setItem("width_DIA", width);
410                         
411                         var qr_width = parseInt(document.getElementById("qr_width_DIA").value); 
412             localStorage.setItem("qr_width_DIA", qr_width);
413                         
414                         var height = parseInt(document.getElementById("height_DIA").value);     
415             localStorage.setItem("height_DIA", height);
416         
417                         if(width === null) width = 400;
418                         if(qr_width === null) qr_width = 480;
419                         if(height === null) height = 400;
421                         document.getElementById("fourchanx-css").textContent += ".qr-preview { height:" +  height + "px; width: " + width +  "px; left:8%;background-size: cover;}";
422                         document.getElementById("fourchanx-css").textContent += "#dump-list { min-height: " + (width - 20) +  "px; width: " + (qr_width) + "px;}";
424                         imageAdderToggle();
425         }
426     });
427     container_div.appendChild(set_button);
429     document.body.appendChild(window_div);
431         if(document.getElementById("width_DIA").value == "489") v_large_input.checked = true;   
432         if(document.getElementById("width_DIA").value == "400") large_input.checked = true;
433         if(document.getElementById("width_DIA").value == "300") medium_input.checked = true;
434         if(document.getElementById("width_DIA").value == "200") small_input.checked = true;
435         
439 //is storage possible
440 function storageAvailable(type) {
441     try {
442         var storage = window[type],
443             x = '__storage_test__';
444         storage.setItem(x, x);
445         storage.removeItem(x);
446         return true;
447     }
448     catch(e) {
449                 //From https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
450         return e instanceof DOMException && (
451             // everything except Firefox
452             e.code === 22 ||
453             // Firefox
454             e.code === 1014 ||
455             // test name field too, because code might not be present
456             // everything except Firefox
457             e.name === 'QuotaExceededError' ||
458             // Firefox
459             e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
460             // acknowledge QuotaExceededError only if there's something already stored
461             storage.length !== 0;
462     }
465 function imageAdderToggle(){
466     if(window_displayed){
467         document.getElementById("image_adder_Window").style.display = "none";
468         document.getElementById("image_adder_Background").style.display = "none";
469         window_displayed = false;
470     }
471     else{
472         document.getElementById("image_adder_Window").style.display = "inline-block";
473         document.getElementById("image_adder_Background").style.display = "inline-block";
474         window_displayed = true;
475     }
478 function imageAdderButton(){
479     var image_adder__button = document.createElement("input");
480     image_adder__button.setAttribute("Value", "Image Adder Settings");
481     image_adder__button.setAttribute("type", "button");
482     image_adder__button.setAttribute("style", "position:absolute;top:135px");
483     imageAdderWindow();
484     if(document.body === null){
485         setTimeout(imageAdderButton, 30);
486     }
487     else{
488         document.body.appendChild(image_adder__button);
489         image_adder__button.addEventListener("click", imageAdderToggle);
490     }
493 //on setimage click clear flags, timers and start another search
494 function buttonClickFunction(){
495         json_page_numbers_used = Array();
496         //reset a failed_to_find_required_tags boolean
497         primed_for_fail = false;
498         for(var i = 0 ; i < timeout_functions.length; i++){
499                 clearInterval(timeout_functions[i]);
500         }
501         tag_incorrect_state = false;
502         timeout = false;
503         //freeze interface to prevent mid opperation changes
504         document.getElementById("tags").setAttribute("disabled", 1);
505         document.getElementById("imageButton").setAttribute("disabled", 1);
506         time = time_max;
507         timeout_functions.push(setInterval(counterFunction, 1000));
508         //start the search
509         setImage();
512 //remove the high quallity image from the dump list
513 function clearImage(){
514     var imagedump_file_list = document.getElementById("dump-list");
515     imagedump_file_list.firstChild.style.backgroundImage = "url()";//trigger mutation event
516     img_URL = ""; //get mutation to set to dead
519 var setTagInterface =  function(tag_input_node, auto_complete_row, second_row_nodes){
520     tags = tag_input_node.value;
521     if(old_tags_before_change !== tags){
522                 previous_images = [];
524         var tag_carat_position = tag_input_node.selectionStart - 1;
525         var closest_tag =  (function(){
526             var current_chararcter = tags.charAt(tag_carat_position);
527             var i = 0;
528             right_most = tag_carat_position;
529             while(current_chararcter != " " && current_chararcter != "" && current_chararcter !== undefined){
530                 i++;
531                 current_chararcter = tags.charAt(tag_carat_position + i);
532                 if(current_chararcter != " " && current_chararcter != "") right_most = tag_carat_position + i;
533             }
534             right_most += 1;
535             current_chararcter = tags.charAt(tag_carat_position);
536             i = 0;
537             leftMost = tag_carat_position;
538             while(current_chararcter != " " && current_chararcter != ""  && current_chararcter !== undefined){
539                 i++;
540                 current_chararcter = tags.charAt(tag_carat_position - i);
541                 if(current_chararcter != " " && current_chararcter != "") leftMost = tag_carat_position - i;
542             }
543             return tags.substring(leftMost, right_most);
544         })();
545         var xhr = new GM_xmlhttpRequest(({
546             method: "GET",
547             url: "https://danbooru.donmai.us/tags.json?search[name_matches]=" + closest_tag + "*&search[order]=count",
548             responseType : "json",
549             onload: function(data){
550                 data = data.response;
551                 var tagArray = tags.split(" ");
552                 while (auto_complete_row.hasChildNodes()) {
553                     auto_complete_row.removeChild(auto_complete_row.lastChild);
554                 }
555                 var qr_width = document.getElementById("qr").offsetWidth;
557                                 var tag_table = document.createElement("TABLE");
558                                 tag_table.setAttribute("style", "border:1px solid black;margin-top:5px");
559                                 var tag_row = document.createElement("TR");
560                 for (var i = 0 ; i < 5 ; i++){
561                     var a  = document.createElement("A");
562                     var tagText = data["" + i];
563                     if(tagText == "" || tagText === undefined) break;
564                     tagText = tagText["name"];
566                     var a_txt  = document.createTextNode(data[i]["name"]);
567                                         var tag_data = document.createElement("TD");
568                                         tag_data.setAttribute("style", "padding:5px;font-size:15px;font-weight:bold;border:1px solid black;");
569                                         a.appendChild(a_txt);
570                                         tag_data.appendChild(a);
571                                         tag_row.appendChild(tag_data);
572                                         tag_table.appendChild(tag_row);
573                                         auto_complete_row.appendChild(tag_table);
575                     if(tag_table.offsetWidth > qr_width - 10){
576                                                 tag_row.removeChild(tag_data);
577                                                 tag_table = document.createElement("TABLE");
578                                                 tag_row = document.createElement("TR");
580                                                 tag_row.appendChild(tag_data);
581                                                 tag_table.appendChild(tag_row);
582                                                 tag_table.setAttribute("style", "border:1px solid black;");
583                                                 auto_complete_row.appendChild(tag_table);
585                                         }
586                     a.addEventListener("click", function(evt){
587                         tagArray[tagArray.indexOf(closest_tag)] = this.textContent;
588                         second_row_nodes[1].value = tagArray.join(" ");
589                     });
590                 }
591             }}));
592     }
593     old_tags_before_change =  tag_input_node.value;
596 //a series of calls on other functions that leads to the image being searched for
597 var setImage = function(){
598     //Set image tags.
599     var tags = document.getElementById("tags").value.trim();
601     if(tags.indexOf(":") > -1) {
602         alert4ChanX("Character ':' not used for file characteristic searches", "warning");
603     }
604     tags = tags.split(" ");
606     var xhr_image_load = new GM_xmlhttpRequest(({
607         method: "GET",
608         //returns a list of all tags and their properties
609         url: "https://danbooru.donmai.us/tags.json?search[name]=" + tags.join() + "&search[order]=count",
610         responseType : "json",
611         onload: function(data)
612         {
613             verifyTags(data, tags);
614                         if(failed_to_find_required_tags_state) return;
616             //set the end
617             var end_URL = ratingURL(tags, json_tag);
619             var URL = setPostAndPage(end_URL, tags);
620             send_URL = URL;
621             //final check, sends final request after function or calls this function again
622             getJSON(URL, checkPageFromDanbooru, tags);
623         }}));
625 //make 4chanX alerts on issues, and account for error cases.
626 function verifyTags(data, tags){
627     data = data.response;
628         //if data is blank, use a no-tag approach
629     if(tags.length == 1 && tags[0] == "") json_tag = [{"name":""}];
630     else json_tag = data;
631         failed_to_find_required_tags_state = false;
632         //if data has a null or undefined case, return an error
633     if(data.length == 0){
634         alert4ChanX("All tags incorrect", "error");
635                 failed_to_find_required_tags_state = true;
636                 document.getElementById("timer").textContent = "";
637                 document.getElementById("tags").removeAttribute("disabled");
638                 document.getElementById("imageButton").removeAttribute("disabled");
639         return;
640     }
641         else if(data.length != tags.length && !tag_incorrect_state){
642                 tag_incorrect_state = true;
643                 if(document.getElementById("tags").value.trim() == "") alert4ChanX("No Tags", "info");
644                 else alert4ChanX("One Tag Incorrect", "warning");
645         }
646     //tag size. Smallest tag is placed at bottom of JSON
647     smallest_tag_size = parseInt(data[data.length-1]["post_count"]);
650 //evaluate the rating restrictions to account for danbooru's tagging limitations
651 var ratingURL = function(tags, data){
652     var URL = "";
653         //evaluate the 3! possible permutations
654     if(document.getElementById("safe").checked){
655         if(document.getElementById("questionable").checked){
656             if(document.getElementById("explicit").checked){
657                 if(data.length > 1)  URL =  "&utf8=%E2%9C%93&tags=" + data[data.length-2]["name"] + "+" + data[data.length-1]["name"];
658                 else  URL =  "&utf8=%E2%9C%93&tags=" + data[data.length-1]["name"];
659             }
660             else{
661                 URL =  "&utf8=%E2%9C%93&tags=" + "-rating%3Aexplicit" + "+" + data[data.length-1]["name"];
662             }
663         }
664         else if(document.getElementById("explicit").checked){
665             URL = "&utf8=%E2%9C%93&tags=" + "-rating%3Aquestionable" + "+" + data[data.length-1]["name"];
666         }
667         else{
668             URL = "&utf8=%E2%9C%93&tags=" + "rating%3Asafe" + "+" + data[data.length-1]["name"];
669         }
670     }
671     else if(document.getElementById("questionable").checked){
672         if(document.getElementById("explicit").checked){
673             URL =  "&utf8=%E2%9C%93&tags=" + "-rating%3Asafe" + "+" + data[data.length-1]["name"];
674         }
675         else{
676             URL =  "&utf8=%E2%9C%93&tags=" + "rating%3Aquestionable" + "+" + data[data.length-1]["name"];
677         }
678     }
679     else if(document.getElementById("explicit").checked){
680         URL =  "&utf8=%E2%9C%93&tags=" + "rating%3Aexplicit" + "+" + data[data.length-1]["name"];
681     }
682     else{
683         if(data.length > 1)  URL =  "&utf8=%E2%9C%93&tags=" + data[data.length-2]["name"] + "+" + data[data.length-1]["name"];
684         else  URL = "&utf8=%E2%9C%93&tags=" + data[data.length-1]["name"];
685     }
686     return URL;
689 //set where to search
690 var setPostAndPage = function(end_URL, tags){
691         //posts
692         if(number_of_posts > 0)
693     number_of_posts = 0;
694    //page
695         if(top_page != top_page_max) smallest_tag_size = top_page * 20;
696     if(smallest_tag_size == 0) smallest_tag_size = 100;
697         do{
698                 escape_cond = true;
699                 page_number = ((Math.floor(Math.random() * 10000)) % Math.ceil(smallest_tag_size / 20)) % 1000;    //1000 is max page search limit
700                 json_page_numbers_used.forEach(function(page){
701                         if(page == 0){
702                                 primed_for_fail = true; // no more pages to search and looped once
703                                 escape_cond = true;
704                                 return;
705                         }
706                         else if(page == page_number){
707                                 escape_cond = false;
708                                 return;
709                         }
710                 });
711         } while(!escape_cond);
712         json_page_numbers_used.push(page_number);
714     var URL = "https://danbooru.donmai.us/posts.json?page=" + page_number + end_URL;
715     return URL;
718 //check if valid url location
719 var primed_for_fail = false;
720 var checkPageFromDanbooru = function(err, data, tags){
721         if (err != null) {
722                 console.log('Something went wrong: ' + err);
723                 alert4ChanX("Danbooru Server Did Not Perform request -- Error: "  + err, "error");
724                 reset_search_timer_fields();
725                 page_number = 0;
726         }
727         else {
728                 do{
729                         var duplicate = false;
730                         //check for repeating images found
731                         previous_images.forEach(function(item){
732                                 if(item[0] == page_number && item[1] == number_of_posts){
733                                         duplicate = true;
734                                 }
735                                 number_of_posts++;
736                         });
737                 }while(duplicate == false && previous_images < number_of_posts);
739                 if(primed_for_fail){
740                         alert4ChanX("No Results: All found for tags \"" + document.getElementById("tags").value + "\"", "error");
741                         reset_search_timer_fields();
742                         return;
743                 }
744                 //redo
745                 else if((data.length < number_of_posts+1) && number_of_attempts > 0) {
746                         if(top_page > page_number){
747                                 top_page = page_number + number_of_posts / 20;
748                         }
749                         number_of_attempts--;
750                         document.getElementById("timer").textContent = number_of_attempts + "|" + time;
751                         setImage();
752                 }
753                 //process page
754                 else if (number_of_attempts > 0){
755                         //ALL PARAMETERS WILL BE RESET INSIDE JSON
756                         document.getElementById("timer").textContent =  number_of_attempts + "|" + time;
757                         getJSON(send_URL, setImageFromDanbooru, tags);
758                 }
759                 else{
760                         alert4ChanX("Not found", "error");
761                         reset_search_timer_fields();
762                         return;
763                 }
764         }
767 function reset_search_timer_fields(){
768         top_page = top_page_max;
769         number_of_attempts = maximum_attempts;
770         document.getElementById("timer").textContent = "";
771         document.getElementById("tags").removeAttribute("disabled");
772         document.getElementById("imageButton").removeAttribute("disabled");
775 //finally draw from the JSON page to generate and place the post into the 4chanX dumplist
776 var setImageFromDanbooru = function(err, data, tags){
777     if (err != null) {
778         console.log('Something went wrong: ' + err);
779         alert4ChanX("Danbooru Server Did Not Perform request -- Error: "  + err, "error");
780                 reset_search_timer_fields();
781     }
782     else {
783                 json_page = data;
784                 var image_found = false;
785                 for (number_of_posts = number_of_posts; number_of_posts < 20 ; number_of_posts++){
786                         if(timeout){
787                                 //Case1: Took too long to scan the page.
788                                 //Result: Kills search
789                                 alert4ChanX("timeout after " + time +" seconds", "error");
790                                 clearInterval(counterFunction);
791                                 reset_search_timer_fields();
792                                 return;
793                         }
794                         else if(json_page["" + number_of_posts] == undefined){
795                                 //Case2: reaches an undefined page.
796                                 //Result: Switches to a new page
797                                 top_page = page_number;
798                                 number_of_attempts--;
799                                 setImage();
800                                 return;
801                         }
803                         //set the page to search
804                         var end_URL = json_page["" + number_of_posts].file_url;
805                         var URL = "https://danbooru.donmai.us" + end_URL;
806                         if(RegExp("(raikou|hijiribe)\d*\.").test(end_URL))
807                                 URL = end_URL;
809                         //place url in visible box
810                         urlContainterFunction(URL);
812                         /*
815 :{id: 3038118, created_at: "2018-03-02T15:27:56.469-05:00", uploader_id: 49091, score: 6,…}
816 approver_id:null
817 bit_flags:0
818 children_ids:null
819 created_at:"2018-03-02T15:27:56.469-05:00"
820 down_score:0
821 fav_count:10
822 fav_string:"fav:553974 fav:467363 fav:455311 fav:490034 fav:505064 fav:482030 fav:351935 fav:66907 fav:467355 fav:519151"
823 file_ext:"jpg"
824 file_size:30492
825 file_url:"/data/__miyuki_kantai_collection_drawn_by_kumadano__7a12a196cc1aa9f794bca81a2a14bb81.jpg"
826 has_active_children:false
827 has_children:false
828 has_large:false
829 has_visible_children:false
830 id:3038118
831 image_height:800
832 image_width:450
833 is_banned:false
834 is_deleted:false
835 is_flagged:false
836 is_note_locked:false
837 is_pending:false
838 is_rating_locked:false
839 is_status_locked:false
840 large_file_url:"/data/__miyuki_kantai_collection_drawn_by_kumadano__7a12a196cc1aa9f794bca81a2a14bb81.jpg"
841 last_comment_bumped_at:null
842 last_commented_at:null
843 last_noted_at:null
844 md5:"7a12a196cc1aa9f794bca81a2a14bb81"
845 parent_id:null
846 pixiv_id:null
847 pool_string:""
848 preview_file_url:"/data/preview/7a12a196cc1aa9f794bca81a2a14bb81.jpg"
849 rating:"s"
850 score:6
851 source:"https://twitter.com/kumadano/status/969629578137251840"
852 tag_count:28
853 tag_count_artist:1
854 tag_count_character:1
855 tag_count_copyright:1
856 tag_count_general:24
857 tag_count_meta:1
858 tag_string:"1girl black_legwear blue_sailor_collar blue_skirt brown_eyes brown_hair commentary_request full_body grin kantai_collection kumadano miyuki_(kantai_collection) pleated_skirt ribbon sailor_collar school_uniform serafuku short_hair short_sleeves simple_background skirt smile socks solo standing wavy_hair white_background wrists_extended"
859 tag_string_artist:"kumadano"
860 tag_string_character:"miyuki_(kantai_collection)"
861 tag_string_copyright:"kantai_collection"
862 tag_string_general:"1girl black_legwear blue_sailor_collar blue_skirt brown_eyes brown_hair full_body grin pleated_skirt ribbon sailor_collar school_uniform serafuku short_hair short_sleeves simple_background skirt smile socks solo standing wavy_hair white_background wrists_extended"
863 tag_string_meta:"commentary_request"
864 up_score:6
865 updated_at:"2018-03-03T09:09:32.357-05:00"
866 uploader_id:49091
867 uploader_name:"---"
869                         */
871                         var failed_to_find_required_tags = false;
872                         if(end_URL === undefined ||
873                            end_URL.indexOf(".mp4") > -1 || end_URL.indexOf(".webm") > -1 || end_URL.indexOf(".swf") > -1 || end_URL.indexOf(".zip") > -1){
874                                 continue;
875                         }
876                         else{
877                                 tags.forEach(function(tag){
878                                         //if tag contains an order then whatever
879                                         if(tag.indexOf("order:") > -1);
880                                         //if it contains a raiting, check the rating character at the seventh index
881                                         else if(tag.indexOf("rating:") > -1){
882                                                 if(tag.charAt(7) !== json_page["" + number_of_posts]["rating"]){
883                                                         failed_to_find_required_tags = true;
884                                                 }
885                                         }
886                                         //otherwise check if the tagstring contains the tags
887                                         else if(json_page["" + number_of_posts]["tag_string"].indexOf(tag) == -1){
888                                                 failed_to_find_required_tags = true;
889                                         }
890                                 });
891                         }
892                         if(failed_to_find_required_tags){
893                                 continue;
894                         }
895                         else{
896                                 if(json_page["" + number_of_posts].file_size >= 4000000){
897                                         var end_URL = json_page["" + number_of_posts].large_file_url;
898                                         var URL = "https://danbooru.donmai.us" + end_URL;
899                                         if(RegExp("(raikou|hijiribe)\d*\.").test(end_URL))
900                                                 URL = end_URL;
902                                 }
903                                 document.getElementById("timer").textContent = "...";
904                                 img_URL = URL;
905                                 var xhr = new GM_xmlhttpRequest(({
906                                         method: "GET",
907                                         url: URL,
908                                         responseType : "arraybuffer",
909                                         onload: function(response)
910                                         {
911                         //is it a non existent image?
912                         if(response.response.byteLength <= 387){
913                             alert4ChanX("Image Does Not Exist on Danbooru(404 error)", "error");
914                         }
915                                                 reset_search_timer_fields();
916                                                 clearInterval(intervalFunction);
917                                                 time = time_max;
918                                                 var counter = document.getElementById("timer");
919                                                 while(counter.hasChildNodes())
920                                                         document.getElementById("timer").removeChild(document.getElementById("timer").lastChild);
922                                                 var blob;
923                                                 if(end_URL.indexOf(".jpg") > -1)
924                                                         blob = new Blob([response.response], {type:"image/jpeg"});
925                                                 else if(end_URL.indexOf(".png") > -1)
926                                                         blob = new Blob([response.response], {type:"image/png"});
927                                                 else if(end_URL.indexOf(".gif") > -1)
928                                                         blob = new Blob([response.response], {type:"image/gif"});
931                                                 var name = end_URL.replace(/(data|cached)/g, "");
932                                                 name = name.replace(/\//g, "");
934                                                 //SEND RESULTING RESPONSE TO 4CHANX FILES === QRSetFile
935                                                 var detail = {file:blob, name:name};
936                                                 if (typeof cloneInto === 'function') {
937                                                         detail  = cloneInto(detail , document.defaultView);
938                                                 }
939                                                 document.getElementById("dump-list").firstChild.click();
940                                                 document.dispatchEvent(new CustomEvent('QRSetFile', {bubbles:true, detail}));
942                                         }
943                                 }));
944                                                                                                 //end function;
945                                 image_found = true;
946                                                                 //SET PAGE&POST AS FOUND
947                                 previous_images.push([page_number, number_of_posts]);
948                                 number_of_posts = 9001;
949                         }
950                 }
951                 if(!image_found){
952                         top_page = page_number;
953                         number_of_attempts--;
954                         setImage();
955                 }
956     }
959 var urlContainterFunction = function(url){
960     var url_box = document.getElementById("urlContainer");
961     url_box.value = url;
964 var counterFunction  = function(){
965     if(!timeout){
966         time--;
967         if(time < 0){
968             timeout = true;
969             time = time_max;
970         }
971     }
974 var getJSON = function(url, callback, extra) {
975     var xhr = new XMLHttpRequest();
976     xhr.open('GET', url, true);
977     xhr.responseType = 'json';
978     xhr.onload = function() {
979         var status = xhr.status;
980         if (status == 200) {
981             callback(null, xhr.response, extra);
982         } else {
983             callback(status);
984         }
985     };
986     xhr.send();