Advertisement
erinx

8chan Mobile 0.3

May 28th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  194.  
  195. var statusInit = 0;
  196.  
  197. if(statusInit == 1){
  198.  
  199. var statusBoard = {};
  200. statusBoard['id'] = "statusBoard";
  201. statusBoard['style'] = 'position:fixed;top:7px;left:1px;width:99%;background:#d6daf0;border:1px solid;max-height:75px;overflow-y:scroll;';
  202. statusBoard = elm('div', statusBoard, db);
  203.  
  204. document.onscroll = function(){
  205. if(db.scrollTop < 100){
  206. statusBoard.style.display = 'none';
  207. }else{
  208. statusBoard.style.display = '';
  209. }
  210.  
  211. };
  212.  
  213. }
  214.  
  215.     var postForm = {};
  216.     postForm['id'] = 'mobileQR';
  217.     postForm['style'] = 'position:fixed;bottom:0px;left:1px;width:99%;background:#d6daf0;border:1px solid;color:black';
  218.     postForm = elm('div', postForm, db);
  219.    
  220.     var persona = '<div id="mobile-persona" style="display:inline-block;width:96%;">\
  221.    <input id="mobile-name" style="width:100%;" placeholder="Name"><br>\
  222.    <input id="mobile-email" style="width:100%;" placeholder="Email"><br>\
  223.    <input id="mobile-subject" style="width:100%;" placeholder="Subject">\
  224.    </div>';
  225.    
  226.     var comment = '<div style="display:inline-block;width:69%;">\
  227.    <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>\
  228.    </div>';
  229.    
  230.     var controls = '<div id="mobile-buttons" style="display:inline-block;width:18%;margin-left:5px;margin-top:0px;margin-bottom:0px;padding:0px !important;">\
  231.    <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>\
  232.    <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>\
  233.    </div>';
  234.    
  235.     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>';
  236.    
  237.     var formCSS = addCss('.fauxButton{vertical-align:middle;display:inline-block;border-color: black;color:black;}');
  238.    
  239.     postForm.innerHTML = persona + comment + controls + menuButtonInp;
  240.    
  241.     q('#mobile-persona').style.display = 'none';
  242.    
  243.     q('#mobile-faux-file').addEventListener('click', function(){
  244.       spawnEvent('click', {}, q('#mobile-file'));
  245.     }, false);
  246.    
  247.     q('#mobile-file').addEventListener('change', function(){
  248.       var fileName = q('#mobile-file').value.split('\\')[q('#mobile-file').value.split('\\').length - 1];
  249.       q('#faux-file-tag').innerHTML = fileName.substring(0, 10) + '...';
  250.     }, false);
  251.    
  252.     function flipMenu(){
  253.       if(q('#mobile-persona').style.display == 'none'){
  254.        q('#mobile-persona').style.display = '';
  255.       }else{
  256.        q('#mobile-persona').style.display = 'none';
  257.       }
  258.     }
  259.    
  260.     q('#mobile-menu-button').addEventListener('click', function(){
  261.       flipMenu();
  262.     }, false);
  263.    
  264.    
  265.     function buttonText(tx){
  266.       if(tx == 'post'){
  267.         q('#faux-post-tag').innerHTML = 'Posting';
  268.       }else if(tx == 'fail'){
  269.         q('#faux-post-tag').innerHTML = 'Retry';
  270.       }else{
  271.         q('#faux-post-tag').innerHTML = 'Post';
  272.       }    
  273.     }
  274.    
  275.     var ownData = getVal('own', 'false');
  276.    
  277.     if(ownData != 'false'){
  278.       var ownPosts = doJson(ownData);
  279.     }else{
  280.       var ownPosts = doJson('[]');
  281.     }
  282.    
  283.    
  284.    
  285.     function submitPost(){
  286.       buttonText('post');
  287.       var formData = new FormData();  
  288.       formData.append('json_response', '0');
  289.       formData.append('post', q('input[type="submit"]', q('form[name="post"]')).value);
  290.       formData.append("board", k.board);
  291.       if(k.thread != 'index' && k.thread != 'catalog'  && k.thread != 'mod'){
  292.         formData.append("thread", k.thread);
  293.       }
  294.       formData.append("name", q('#mobile-name').value);
  295.       formData.append("email", q('#mobile-email').value);
  296.       formData.append("subject", q('#mobile-subject').value);
  297.       formData.append("file", q("#mobile-file").files[0]);
  298.       formData.append("body", q('#mobile-comment').value);
  299.      
  300.       var xhr = new XMLHttpRequest();
  301.  
  302.       xhr.open("POST", k.proto + '//sys.8ch.net/post.php');
  303.    
  304.       xhr.onreadystatechange = function(){
  305. if(statusInit == 1){statusBoard.innerHTML = xhr.readyState + '...';}
  306.         if(xhr.readyState == 4){
  307.           var re = doJson(xhr.responseText);
  308.          if(statusInit == 1){ statusBoard.innerHTML = xhr.status + ': ' + xhr.responseText; }
  309.           if(re.hasOwnProperty('error')){  
  310.             alert(re['error']);
  311.             buttonText('fail');
  312.           }else{
  313.             ownPosts.push(re['id']);
  314.             var newPosts = doJson(ownPosts);
  315.             setVal('own', newPosts);
  316.             q('#mobile-comment').value = '';
  317.             buttonText();
  318.             q('#mobile-file-container').innerHTML = '<input type="file" id="mobile-file" style="display:none;">';
  319.             q('#faux-file-tag').innerHTML = 'File';
  320.           }
  321.            
  322.            
  323.         }
  324.        
  325.       }
  326.      
  327.      
  328.       function updateProgress(e){
  329.         if (e.lengthComputable){
  330.           var percentComplete = parseInt((e.loaded / e.total)*100).toFixed(0);
  331.           if(percentComplete > 100) percentComplete = 100;
  332.           q('#faux-post-tag').innerHTML = percentComplete + '%';
  333.         }
  334.       }
  335.      
  336.       xhr.upload.addEventListener("progress", updateProgress, false);
  337.      
  338.       xhr.send(formData);
  339.    
  340.     }
  341.    
  342.    
  343.    
  344.     function parseQuotes(){
  345.       doThese(qq('.post_no'), function(xe, xi){
  346.         if(xe.innerHTML != 'No.'){
  347.           if(thisAtt(xe, 'data-mobile') != 'passed'){
  348.             thisAtt(xe, 'data-mobile', 'passed');
  349.             xe.addEventListener('click', function(e){
  350.               e.preventDefault();
  351.               e.stopPropagation();              
  352.               q('#mobile-comment').value += '>>' + xe.innerHTML + '\n';
  353.               q('#mobile-comment').focus();
  354.             }, false);
  355.           }
  356.         }
  357.       });
  358.       doThese(qq('.body'), function(ze, zi){
  359.         doThese(qq('a', ze), function(ye, yi){
  360.           if(ye.innerHTML.indexOf('&gt;&gt;') == 0){
  361.             var postNum = ye.innerHTML.split('&gt;&gt;')[1];
  362.             if(ownPosts.indexOf(postNum) > -1 && ye.innerHTML.indexOf('(You)') < 0){
  363.               ye.innerHTML += ' (You)';
  364.             }
  365.           }
  366.         });
  367.       });
  368.     }
  369.    
  370.    
  371.    
  372.     q('#mobile-name').onfocus = function(e){
  373.       window.scrollTo(0, (db.scrollTop-30));
  374.       postForm.style.position = 'absolute';
  375.       postForm.style.bottom = '';
  376.       postForm.style.top = (db.scrollTop+100) + 'px';
  377.     }
  378.    
  379.     q('#mobile-email').onfocus = function(e){
  380.       window.scrollTo(0, (db.scrollTop-30));
  381.       postForm.style.position = 'absolute';
  382.       postForm.style.bottom = '';
  383.       postForm.style.top = (db.scrollTop+100) + 'px';
  384.     }
  385.    
  386.     q('#mobile-subject').onfocus = function(e){
  387.       window.scrollTo(0, (db.scrollTop-30));
  388.       postForm.style.position = 'absolute';
  389.       postForm.style.bottom = '';
  390.       postForm.style.top = (db.scrollTop+100) + 'px';
  391.     }
  392.    
  393.     q('#mobile-comment').onfocus = function(e){
  394.       window.scrollTo(0, (db.scrollTop-30));
  395.       postForm.style.position = 'absolute';
  396.       postForm.style.bottom = '';
  397.       postForm.style.top = (db.scrollTop+100) + 'px';
  398.     }
  399.    
  400.     q('#mobile-file').onfocus = function(e){
  401.       window.scrollTo(0, (db.scrollTop-30));
  402.       postForm.style.position = 'absolute';
  403.       postForm.style.bottom = '';
  404.       postForm.style.top = (db.scrollTop+100) + 'px';
  405.     }
  406.    
  407.     q('#mobile-name').onblur = function(e){
  408.       postForm.style.position = 'fixed';
  409.       postForm.style.bottom = '0px';
  410.       postForm.style.top = '';
  411.       putNext(postForm, q('bottom'));
  412.  
  413.     }
  414.    
  415.     q('#mobile-email').onblur = function(e){
  416.       postForm.style.position = 'fixed';
  417.       postForm.style.bottom = '0px';
  418.       postForm.style.top = '';
  419. putNext(postForm, q('bottom'));
  420.  
  421.     }
  422.    
  423.     q('#mobile-subject').onblur = function(e){
  424.       postForm.style.position = 'fixed';
  425.       postForm.style.bottom = '0px';
  426.       postForm.style.top = '';
  427.       putNext(postForm, q('bottom'));
  428.  
  429.     }
  430.    
  431.     q('#mobile-comment').onblur = function(e){
  432.       postForm.style.position = 'fixed';
  433.       postForm.style.bottom = '0px';
  434.       postForm.style.top = '';
  435.       putNext(postForm, q('bottom'));
  436.     }
  437.    
  438.     q('#mobile-file').onblur = function(e){
  439.       postForm.style.position = 'fixed';
  440.       postForm.style.bottom = '0px';
  441.       postForm.style.top = '';
  442.       putNext(postForm, q('bottom'));
  443.     }
  444.    
  445.    
  446.     q('#mobile-name').addEventListener('input', function(){
  447.       setVal('name', q('#mobile-name').value);
  448.     }, false);
  449.    
  450.     q('#mobile-email').addEventListener('input', function(){
  451.       setVal('email', q('#mobile-email').value);
  452.     }, false);
  453.    
  454.     q('#mobile-subject').addEventListener('input', function(){
  455.       setVal('subject', q('#mobile-subject').value);
  456.     }, false);
  457.    
  458.     q('#mobile-name').value = getVal('name', '');
  459.     q('#mobile-email').value = getVal('email', '');
  460.     q('#mobile-subject').value = getVal('subject', '');
  461.    
  462.    
  463.     q('#mobile-submit').addEventListener('click', submitPost, false);
  464.    
  465.     setTick(3000, function(){
  466.       parseQuotes();
  467.     }, true);
  468.    
  469.   }
  470.   buildMobile();
  471.  
  472.  
  473.    
  474. }
  475.  
  476. mobile();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement