Advertisement
erinx

8chan mobile script 1

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