SHARE
TWEET

8chanmobile.js

Grim_Wrath Feb 26th, 2016 56 Never
  1. function mobile(){
  2.   var d, db, h, k, w;  
  3.  
  4.   d = document;
  5.   db = document.body;
  6.   h = document.getElementsByTagName('head')[0];
  7.  
  8.   w = window.location.href;
  9.   k = {};  
  10.   k.end = w.split('.net')[1];    
  11.   k.proto = window.location.protocol;
  12.   if(k.end.indexOf('mod.php?') > 0){
  13.     if(k.end.length > 3 && w.split('mod.php?/')[1].indexOf('/') > 0 && w.indexOf('/src/') < 1){
  14.       k.board = w.split('mod.php?/')[1].split('/')[0];
  15.     }else{
  16.       k.board = null;
  17.     }
  18.     if(w.indexOf('/res/') > 1){
  19.       k.thread = w.split('/res/')[1].split('+')[0].split('.')[0];
  20.     }else if(w.indexOf('catalog') > 2){
  21.       k.thread = 'catalog';
  22.     }else{
  23.       k.thread = 'mod';
  24.     }
  25.   }else{
  26.     if(k.end.length > 3 && w.split('8ch.net/')[1].indexOf('/') > 0 && w.indexOf('/src/') < 1){
  27.       k.board = w.split('8ch.net/')[1].split('/')[0];
  28.     }else{
  29.       k.board = null;
  30.     }
  31.     if(w.indexOf('/res/') > 1){
  32.       k.thread = w.split('/res/')[1].split('+')[0].split('.h')[0];
  33.     }else if(w.indexOf('catalog') > 2){
  34.       k.thread = 'catalog';
  35.     }else if(w.indexOf('index') > 2 || w.substring((w.length-1), w.length) == '/'){
  36.       k.thread = 'index';
  37.     }else{
  38.       k.thread = null;
  39.     }
  40.   }
  41.  
  42.   function q(s, p){
  43.     if(!p || p == null){
  44.       p = db;
  45.     }
  46.     return p.querySelector(s);
  47.   };
  48.  
  49.   function qq(s, p){
  50.     if(!p || p == null){
  51.       p = db;
  52.     }
  53.     return p.querySelectorAll(s);
  54.   };
  55.  
  56.   function getVal(k, v){
  57.     if(typeof(Storage) !=="undefined"){
  58.       if(v == null){
  59.         if(localStorage.getItem(namespace + "." + k) != null){
  60.           return localStorage.getItem(namespace  + "." + k);
  61.         }else{
  62.           return 'undefined';
  63.         }
  64.       }else{
  65.         if(localStorage.getItem("mobile." + k) != null){
  66.           return localStorage.getItem("mobile." + k);
  67.         }else{
  68.           return v;
  69.         }
  70.       }
  71.     }else{
  72.       return 'storage unavailable';
  73.     }
  74.   };
  75.  
  76.   function setVal(k, v){
  77.     if(typeof(Storage) !=="undefined"){
  78.       if(v == null){
  79.         return 'undefined';
  80.       }else{
  81.         return localStorage.setItem("mobile." + k, v);
  82.       }
  83.     }else{
  84.       return 'storage unavailable';
  85.     }
  86.   };
  87.  
  88.   function delVal(k){
  89.     return localStorage.removeItem("mobile." + k);
  90.   };  
  91.  
  92.   function elm(t, a, s){
  93.     var e = d.createElement(t);
  94.     if(a){
  95.       for (key in a){
  96.         e.setAttribute(key, a[key]);
  97.       }
  98.     }
  99.     if(s){
  100.       s.appendChild(e);
  101.     }
  102.     return e;
  103.   };
  104.  
  105.   function doThese(a, c, e){
  106.     for(var i = 0; i < a.length; i++){
  107.       c(a[i], i);
  108.       if(i == (a.length)-1){
  109.         if(e && e != null){
  110.           return e(a[i], i);
  111.         }else{
  112.           return a;
  113.         }
  114.       }
  115.     }
  116.   };
  117.  
  118.   function doJson(s){
  119.     if(typeof s == 'string'){
  120.       return JSON.parse(s);
  121.     }else{
  122.       return JSON.stringify(s);
  123.     }
  124.   };
  125.  
  126.   function spawnEvent(t, i, s){
  127.     if (i == null) {
  128.       i = {};
  129.     }
  130.     if (s == null) {
  131.       s = d;
  132.     }
  133.     return s.dispatchEvent(new CustomEvent(t, i));
  134.   };
  135.  
  136.   function addCss(s){
  137.     var e = d.createElement('style');
  138.     e.type = 'text/css';
  139.     if (e.styleSheet){
  140.       e.styleSheet.cssText = s;
  141.     } else {
  142.       e.appendChild(d.createTextNode(s));
  143.     }
  144.     h.appendChild(e);
  145.     return e;
  146.   };
  147.  
  148.   function thisAtt(s, a, v){
  149.     if (!v || v == null || v == false) {
  150.       if (v == false) {
  151.         return s.removeAttribute(a);
  152.       }else{
  153.         return s.getAttribute(a);
  154.       }
  155.     }else{
  156.       s.setAttribute(a, v);
  157.       return s;
  158.     }
  159.   };
  160.  
  161.   function setTick(t, c, l){
  162.     if(c == false){
  163.       return clearInterval(t);
  164.     }else{
  165.       if(l == true){
  166.         return setInterval(function(){
  167.           c();
  168.         },t);
  169.       }else{
  170.         return setTimeout(function(){
  171.           c();
  172.         },t);
  173.       }
  174.     }
  175.   };
  176.  
  177.   function putNext(e, s){
  178.     s.parentNode.insertBefore(e, s.nextSibling);
  179.     return e;
  180.   };
  181.  
  182.  
  183.  
  184.   function buildMobile(){
  185.    
  186.     var mobileCSS = addCss('body{ padding-bottom:100px !important; padding-left:0px !important;padding-right:0px !important;margin-left:0px !important;margin-right:0px !important; } .reply{ width:98% !important; } .board_image{ display:none !important; }');
  187.    
  188.     var mobileMeta = {};
  189.     mobileMeta['name'] = 'viewport';
  190.     mobileMeta['content'] = 'width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, target-densityDpi=device-dpi';
  191.     mobileMeta = elm('meta', mobileMeta, h);
  192.    
  193.     var postForm = {};
  194.     postForm['id'] = 'mobileQR';
  195.     postForm['style'] = 'position:fixed;bottom:0px;left:1px;width:99%;background:#d6daf0;border:1px solid;color:black';
  196.     postForm = elm('div', postForm, db);
  197.    
  198.     var persona = '<div id="mobile-persona" style="display:inline-block;width:96%;">\
  199.     <input id="mobile-name" style="width:100%;" placeholder="Name"><br>\
  200.     <input id="mobile-email" style="width:100%;" placeholder="Email"><br>\
  201.     <input id="mobile-subject" style="width:100%;" placeholder="Subject">\
  202.     </div>';
  203.    
  204.     var comment = '<div style="display:inline-block;width:69%;">\
  205.     <textarea id="mobile-comment" placeholder="comment" style="width:100%;height:70px;margin:0px;padding:0px;margin-top:0px;margin-bottom: -15px ! important;padding:0px !important;"></textarea>\
  206.     </div>';
  207.    
  208.     var controls = '<div id="mobile-buttons" style="display:inline-block;width:18%;margin-left:5px;margin-top:0px;margin-bottom:0px;padding:0px !important;">\
  209.     <div id="mobile-faux-file" style="width:100%;height:32px;background:#b7c5d9;margin:2px;text-align:center;border-radius:3px;border:1px solid;"><span style="line-height:30px;"></span><div id="faux-file-tag" class="fauxButton">File</div></div><span id="mobile-file-container"><input type="file" id="mobile-file" style="display:none;"></span>\
  210.     <div id="mobile-submit" style="width:100%;height:32px;background:#b7c5d9;margin:2px;text-align:center;border-radius:3px;border:1px solid;"><span style="line-height:30px;"></span><div id="faux-post-tag" class="fauxButton">Post</div></div>\
  211.     </div>';
  212.    
  213.     var menuButtonInp = '<div style="display:inline-block;width:8%;margin-left:5px;"><div id="mobile-menu-button" style="width:100%;height:68px;background:#b7c5d9;margin:2px;text-align:center;border-radius:3px;border:1px solid;"><span style="line-height:102px;"></span><div id="faux-menu-tag" class="fauxButton" style="margin-top:-32px;">+</div></div></div>';
  214.    
  215.     var formCSS = addCss('.fauxButton{vertical-align:middle;display:inline-block;border-color: black;color:black;}');
  216.    
  217.     postForm.innerHTML = persona + comment + controls + menuButtonInp;
  218.    
  219.     q('#mobile-persona').style.display = 'none';
  220.    
  221.     q('#mobile-faux-file').addEventListener('click', function(){
  222.       spawnEvent('click', {}, q('#mobile-file'));
  223.     }, false);
  224.    
  225.     q('#mobile-file').addEventListener('change', function(){
  226.       var fileName = q('#mobile-file').value.split('\\')[q('#mobile-file').value.split('\\').length - 1];
  227.       q('#faux-file-tag').innerHTML = fileName.substring(0, 10) + '...';
  228.     }, false);
  229.    
  230.     function flipMenu(){
  231.       if(q('#mobile-persona').style.display == 'none'){
  232.        q('#mobile-persona').style.display = '';
  233.       }else{
  234.        q('#mobile-persona').style.display = 'none';
  235.       }
  236.     }
  237.    
  238.     q('#mobile-menu-button').addEventListener('click', function(){
  239.       flipMenu();
  240.     }, false);
  241.    
  242.    
  243.     function buttonText(tx){
  244.       if(tx == 'post'){
  245.         q('#faux-post-tag').innerHTML = 'Posting';
  246.       }else if(tx == 'fail'){
  247.         q('#faux-post-tag').innerHTML = 'Retry';
  248.       }else{
  249.         q('#faux-post-tag').innerHTML = 'Post';
  250.       }    
  251.     }
  252.    
  253.     var ownData = getVal('own', 'false');
  254.    
  255.     if(ownData != 'false'){
  256.       var ownPosts = doJson(ownData);
  257.     }else{
  258.       var ownPosts = doJson('[]'); 
  259.     }
  260.    
  261.    
  262.    
  263.     function submitPost(){
  264.       buttonText('post');
  265.       var formData = new FormData();   
  266.       formData.append('json_response', '0');
  267.       formData.append('post', q('input[type="submit"]', q('form[name="post"]')).value);
  268.       formData.append("board", k.board);
  269.       if(k.thread != 'index' && k.thread != 'catalog'  && k.thread != 'mod'){
  270.         formData.append("thread", k.thread);
  271.       }
  272.       formData.append("name", q('#mobile-name').value);
  273.       formData.append("email", q('#mobile-email').value);
  274.       formData.append("subject", q('#mobile-subject').value);
  275.       formData.append("file", q("#mobile-file").files[0]);
  276.       formData.append("body", q('#mobile-comment').value);
  277.      
  278.       var xhr = new XMLHttpRequest();
  279.  
  280.       xhr.open("POST", k.proto + '//8ch.net/post.php');
  281.    
  282.       xhr.onreadystatechange = function(){
  283.         if(xhr.readyState == 4){
  284.           var re = doJson(xhr.responseText);
  285.           if(re.hasOwnProperty('error')){  
  286.             alert(re['error']);
  287.             buttonText('fail');
  288.           }else{
  289.             ownPosts.push(re['id']);
  290.             var newPosts = doJson(ownPosts);
  291.             setVal('own', newPosts);
  292.             q('#mobile-comment').value = '';
  293.             buttonText();
  294.             q('#mobile-file-container').innerHTML = '<input type="file" id="mobile-file" style="display:none;">';
  295.             q('#faux-file-tag').innerHTML = 'File';
  296.           }
  297.            
  298.            
  299.         }
  300.        
  301.       }
  302.      
  303.      
  304.       function updateProgress(e){
  305.         if (e.lengthComputable){
  306.           var percentComplete = parseInt((e.loaded / e.total)*100).toFixed(0);
  307.           if(percentComplete > 100) percentComplete = 100;
  308.           q('#faux-post-tag').innerHTML = percentComplete + '%';
  309.         }
  310.       }
  311.      
  312.       xhr.upload.addEventListener("progress", updateProgress, false);
  313.      
  314.       xhr.send(formData);
  315.    
  316.     }
  317.    
  318.    
  319.    
  320.     function parseQuotes(){
  321.       doThese(qq('.post_no'), function(xe, xi){
  322.         if(xe.innerHTML != 'No.'){
  323.           if(thisAtt(xe, 'data-mobile') != 'passed'){
  324.             thisAtt(xe, 'data-mobile', 'passed');
  325.            
  326.             /*
  327.            
  328.             putNext(q('time', q('#reply_' + xe.innerHTML)), q('.post_anchor', q('#reply_' + xe.innerHTML)));
  329.            
  330.             q('time', q('#reply_' + xe.innerHTML)).style.marginRight = '20px';
  331.            
  332.             //q('label', q('#reply_' + xe.innerHTML)).style.float = 'right';
  333.            
  334.             putNext(q('label', q('#reply_' + xe.innerHTML)), q('.intro', q('#reply_' + xe.innerHTML)));
  335.            
  336.             var xBra = elm('span', {}, db);
  337.            
  338.             xBra.innerHTML = '<hr>';
  339.            
  340.            var xBrb = elm('span', {}, db);
  341.            
  342.             xBrb.innerHTML = '<hr>';
  343.                    
  344.             thisAtt(q('label', q('#reply_' + xe.innerHTML)), 'class', 'intro');
  345.            
  346.             var xBr = elm('span', {}, db);
  347.            
  348.             xBr.innerHTML = '<hr><span style="font-size:10px;">Replies: </span>';
  349.            
  350.             putNext(q('.mentioned', q('#reply_' + xe.innerHTML)), q('.body', q('#reply_' + xe.innerHTML)));
  351.            
  352.             putNext(xBr, q('.body', q('#reply_' + xe.innerHTML)));
  353.            
  354.             putNext(xBra, q('.intro', q('#reply_' + xe.innerHTML)));
  355.            
  356.             putNext(xBrb, q('label', q('#reply_' + xe.innerHTML)));
  357.            
  358.             //q('.body', q('#reply_' + xe.innerHTML)).style.borderBottom = '1px solid';         */
  359.                        
  360.             xe.addEventListener('click', function(e){
  361.               e.preventDefault();
  362.               e.stopPropagation();              
  363.               q('#mobile-comment').value += '>>' + xe.innerHTML + '\n';
  364.             //  putNext(postForm, q('#reply_' + xe.innerHTML));
  365.              // postForm.style.position = 'inherit';
  366.               q('#mobile-comment').focus();
  367.             }, false);
  368.           }
  369.         }
  370.       });
  371.       doThese(qq('.body'), function(ze, zi){
  372.         doThese(qq('a', ze), function(ye, yi){
  373.           if(ye.innerHTML.indexOf('&gt;&gt;') == 0){
  374.             var postNum = ye.innerHTML.split('&gt;&gt;')[1];
  375.             if(ownPosts.indexOf(postNum) > -1 && ye.innerHTML.indexOf('(You)') < 0){
  376.               ye.innerHTML += ' (You)';
  377.             }
  378.           }
  379.         });
  380.       });
  381.     }
  382.    
  383.    
  384.    
  385.     q('#mobile-name').onfocus = function(e){
  386.       window.scrollTo(0, (db.scrollTop-30));
  387.       postForm.style.position = 'absolute';
  388.       postForm.style.bottom = '';
  389.       postForm.style.top = (db.scrollTop+100) + 'px';
  390.     }
  391.    
  392.     q('#mobile-email').onfocus = function(e){
  393.       window.scrollTo(0, (db.scrollTop-30));
  394.       postForm.style.position = 'absolute';
  395.       postForm.style.bottom = '';
  396.       postForm.style.top = (db.scrollTop+100) + 'px';
  397.     }
  398.    
  399.     q('#mobile-subject').onfocus = function(e){
  400.       window.scrollTo(0, (db.scrollTop-30));
  401.       postForm.style.position = 'absolute';
  402.       postForm.style.bottom = '';
  403.       postForm.style.top = (db.scrollTop+100) + 'px';
  404.     }
  405.    
  406.     q('#mobile-comment').onfocus = function(e){
  407.       window.scrollTo(0, (db.scrollTop-30));
  408.       postForm.style.position = 'absolute';
  409.       postForm.style.bottom = '';
  410.       postForm.style.top = (db.scrollTop+100) + 'px';
  411.     }
  412.    
  413.     q('#mobile-file').onfocus = function(e){
  414.       window.scrollTo(0, (db.scrollTop-30));
  415.       postForm.style.position = 'absolute';
  416.       postForm.style.bottom = '';
  417.       postForm.style.top = (db.scrollTop+100) + 'px';
  418.     }
  419.    
  420.     q('#mobile-name').onblur = function(e){
  421.       postForm.style.position = 'fixed';
  422.       postForm.style.bottom = '0px';
  423.       postForm.style.top = '';
  424.       putNext(postForm, q('bottom'));
  425.  
  426.     }
  427.    
  428.     q('#mobile-email').onblur = function(e){
  429.       postForm.style.position = 'fixed';
  430.       postForm.style.bottom = '0px';
  431.       postForm.style.top = '';
  432. putNext(postForm, q('bottom'));
  433.  
  434.     }
  435.    
  436.     q('#mobile-subject').onblur = function(e){
  437.       postForm.style.position = 'fixed';
  438.       postForm.style.bottom = '0px';
  439.       postForm.style.top = '';
  440.       putNext(postForm, q('bottom'));
  441.  
  442.     }
  443.    
  444.     q('#mobile-comment').onblur = function(e){
  445.       postForm.style.position = 'fixed';
  446.       postForm.style.bottom = '0px';
  447.       postForm.style.top = '';
  448.       putNext(postForm, q('bottom'));
  449.     }
  450.    
  451.     q('#mobile-file').onblur = function(e){
  452.       postForm.style.position = 'fixed';
  453.       postForm.style.bottom = '0px';
  454.       postForm.style.top = '';
  455.       putNext(postForm, q('bottom'));
  456.     }
  457.    
  458.    
  459.     q('#mobile-name').addEventListener('input', function(){
  460.       setVal('name', q('#mobile-name').value);
  461.     }, false);
  462.    
  463.     q('#mobile-email').addEventListener('input', function(){
  464.       setVal('email', q('#mobile-email').value);
  465.     }, false);
  466.    
  467.     q('#mobile-subject').addEventListener('input', function(){
  468.       setVal('subject', q('#mobile-subject').value);
  469.     }, false);
  470.    
  471.     q('#mobile-name').value = getVal('name', '');
  472.     q('#mobile-email').value = getVal('email', '');
  473.     q('#mobile-subject').value = getVal('subject', '');
  474.    
  475.    
  476.     q('#mobile-submit').addEventListener('click', submitPost, false);
  477.    
  478.     setTick(3000, function(){
  479.       parseQuotes();
  480.     }, true);
  481.    
  482.   }
  483.   buildMobile();
  484.  
  485.  
  486.    
  487. }
  488.  
  489. mobile();
RAW Paste Data
Top