root2

table_sorting

Aug 8th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         For sorting a table
  3. // @namespace    table_sorting
  4. // @include     /^https?://(www\.)?mytrafficvalue.com/monetization/(.*)shares/[0-9]{4}-[0-9]{2}-[0-9]{2}.html$/
  5. // @author       facebook.com/igor39
  6. // @version      1.6
  7. // @grant        none
  8. // @description  For Tampermonkey & Greasemonkey
  9. // ==/UserScript==
  10. function main(){
  11. // for sorting any table from the page (including BAP transfers): mytrafficvalue.com/monetization/shares.html
  12. // http://kryogenix.org/code/browser/sorttable/
  13. // this script can sort the table, and now will work with both FireFox and Chrome.
  14. // if the pager is missing, you need to reload the page, if you need to change a number of lines per page.
  15.  
  16. var is,onlyOne,arr=[],prev_price=[],prev_limit=[],market,counter,counter2=0,min_price=999999, max_limit=0, price_sign, tbody1_headers='<tr><th id="TheMarketID" style="width:120px"></th><th>RP\'s amount</th><th>RP\'s value</th></tr>', re=new RegExp('(\\d)(?=(\\d\\d\\d)+([^\\d]|$))', 'g'),
  17. handle,new_attr,script,style,elem,newDiv,script_loaded,qs4,number_of_lines=1, current_number_of_lines=1, my_form_code='<form id="myform">Lines per page: <span id="brd3"></span><input checked type="input" id="number_of_lines" size="5" value="" /> <button id="input_submit1" type="button">Submit</button></form> <form id="myform2"><div id="brd6"><table class="commissions"><tbody>'+tbody1_headers+'</tbody></table>Start with a price: <input type="input" id="input_price" size="7" value="" /> End on a price <input type="input" id="input_limit" size="7" value="" /> <button id="input_submit2" type="button">Submit</button></div></form>',
  18. style_of_table= 'table th.W20{width:19%;} table.commissions th {background: #b9c9fe; border-bottom: 1px solid #fff; border-top: 3px solid #aabcfe; color: #039; font-size: 13px; font-weight: normal; font-family: Verdana; padding: 0px;} table.commissions td {background: #e8edff; border-bottom: 1px solid #fff; border-top: 1px solid transparent; color: #669; font-family: Verdana; font-size: 12px; padding: 0;} table.commissions tr:hover td {background: #ccddff;}#tbody1 th, #tbody1 td{text-align: left;width: 100px;height: auto;font-size: 1.0em;}';
  19.    script=document.createElement('script'); script.setAttribute('id','sorttable'); script.src="//kryogenix.org/code/browser/sorttable/sorttable.js"; script.onload =function(){script_loaded=1;}; elem=getById("main_content"); elem.insertBefore(script, elem.childNodes[0]);
  20.    if(window.location.href.match(/bap/g)){price_sign="";is="bap";market="BAP's Transfers";}else{price_sign="$";is="market";market="RP's Transfers";}
  21. function getById(id){return document.getElementById(id);}
  22. function getBySel(sel){return document.querySelector(sel);}
  23. function click(sel){var event=document.createEvent('MouseEvents');event.initMouseEvent('click',1,1,window,1,0,0,0,0,0,0,0,0,0,null);document.querySelector(sel).dispatchEvent(event);}
  24. function replace2(el){if(current_number_of_lines!=number_of_lines){current_number_of_lines=number_of_lines; new_attr=el.href.substring(0, el.href.lastIndexOf('/') -1); new_attr= new_attr+'1/'+number_of_lines; el.href=new_attr; click('span.paging a.pagination');getById("tbody1").innerHTML=tbody1_headers;}}
  25. function adding_form(){ if(!getById('myform')){
  26. newDiv=document.createElement('div'); newDiv.innerHTML =my_form_code; getBySel('div.mainRound').appendChild(newDiv);
  27. getById("input_submit1").onclick=function(){var n;if(getById('number_of_lines').value){n=getById('number_of_lines').value; n*=1;number_of_lines=n; qs4=getBySel('span.paging a.pagination'); if(qs4){replace2(qs4);} getById("brd3").innerHTML=number_of_lines+' ';}};
  28. }}
  29. function total_sum_on(price){ if(getById(price)){ var RPs_value=0,RPs_amount=0,text,i,x=document.querySelectorAll('tbody#tbody0 tr[id="'+price+'"]');
  30. // we will also add the results of previous calculation to the next:
  31. if(getById('tbody1')){var j,k=document.querySelectorAll('tbody#tbody1 tr[id="'+price+'"]'); for(j=0;j<k.length;j++){var isNum=k[j].children[1].children[0];isNum*=1;isNum=typeof(isNum); if(isNum=="number"){text=k[j].children[1].children[0].innerHTML.replace(/[$\s]/g,''); text*=1;RPs_amount+=text;}}}
  32.  
  33. for(i=0;i < x.length;i++){if(x[i].children[4]){text=x[i].children[4].innerHTML.replace(/\,/g,''); text*=1;RPs_amount+=text;}}
  34. RPs_value=RPs_amount*price; RPs_value=RPs_value.toFixed(2); RPs_value*=1; RPs_value=RPs_value.toString().replace(re,'$1 '); RPs_amount=RPs_amount.toString().replace(re,'$1 '); arr[counter]='<tr id="'+price+'"><td>RPs at '+price+':</td><td><b>'+RPs_amount+'</b></td><td><b>'+price_sign+RPs_value+'</b></td></tr>';counter++;
  35. }}
  36. function run_total_sum_on(price,limit){ if(onlyOne){//Only one calculation per page.(otherwise the results of previous calculation will be added to the next on the same page)
  37. onlyOne=0;price*=1;limit*=1;counter=0;clearInterval(handle);handle=0; prev_price[counter2]=price; prev_limit[counter2]=limit;counter2++;
  38. var j= prev_price.length; while (j--){if(prev_price[j] < min_price){min_price = prev_price[j];}}// price won't be bigger than previous minimum.
  39. var k= prev_limit.length; while (k--){if(prev_limit[k] > max_limit){max_limit = prev_limit[k];}}// limit won't be smaller than previous maximum.
  40. if(price>min_price){price=min_price;}  if(limit<max_limit){limit=max_limit;}
  41.  
  42. var start1 =new Date(),end1,a;
  43. if(is=="market"){limit+=0.0001; while(price <= limit){price= +price.toFixed(5);total_sum_on(price);price+=0.0001;}}
  44. else if(is=="bap"){while(price <= limit){total_sum_on(price);price+=1;}}
  45.  
  46. getById("tbody1").innerHTML=tbody1_headers;   getById("TheMarketID").innerHTML=market;
  47. for(a=0;a<arr.length;a++){if(arr[a]){getById("tbody1").innerHTML+=arr[a];arr[a]='';}}
  48. var i,x=document.querySelectorAll('tbody#tbody1 tr'),RPs_value=0,RPs_amount=0,text;
  49. for(i=1;i < x.length;i++){if(x[i].children[1].children[0]){text=x[i].children[1].children[0].innerHTML.replace(/[$\s]/g,''); text*=1; RPs_amount+=text; text=x[i].children[2].children[0].innerHTML.replace(/[$\s]/g,'');text*=1; RPs_value+=text;}}     RPs_value= +RPs_value.toFixed(2); RPs_value=RPs_value.toString().replace(re,'$1 '); RPs_amount=RPs_amount.toString().replace(re,'$1 ');
  50. getById("tbody1").innerHTML+='<tr><td>Total:</td><td><b>'+RPs_amount+'</b></td><td><b>'+price_sign+RPs_value+'</b></td></tr>';  end1=new Date();console.log('The calculation lasted: '+(end1.getTime()-start1.getTime())+' ms\n'+end1+'\n'); handle=setInterval(main2,100);
  51. }}
  52. function main2(){ adding_form();
  53. if(!getBySel('div.pageable table.sortable')&&script_loaded){getBySel('div.pageable table').className+=" sortable";
  54. sorttable.makeSortable(getBySel("div.pageable table"));// http://kryogenix.org/code/browser/sorttable/#ajaxtables
  55. var a,b=document.querySelectorAll('table tbody');for(a=0;a<b.length;a++){b[a].setAttribute('id',"tbody"+a);}var new_id,i,x=document.querySelectorAll('tbody#tbody0 tr'),sel,current_min=999999,current_max=0;for(i=0;i<x.length;i++){if(x[i].children[3]){new_id=x[i].children[3].innerHTML.replace(/[,$]/g,'');new_id*=1;if(new_id>current_max){current_max=new_id;}if(new_id<current_min){current_min=new_id;}} if(i){new_id*=1;}x[i].setAttribute('id',new_id);if(x[i].children[0]){sel=x[i].children[0]; sel.innerHTML=sel.innerHTML;}} getById("input_price").value =current_min; getById("input_limit").value=current_max;
  56.  
  57. getById("input_submit2").onclick=function(){var price=getById("input_price").value,limit=getById("input_limit").value; run_total_sum_on(price,limit);};
  58. style=document.createElement('style'); style.appendChild(document.createTextNode(style_of_table)); elem=getById("main_content"); elem.insertBefore(style, elem.childNodes[0]);
  59.    onlyOne=1;  getById("TheMarketID").innerHTML=market;
  60. }} handle=setInterval(main2,100);}
  61. var script = document.createElement('script');script.setAttribute('id','mfnc0');
  62. script.appendChild(document.createTextNode('('+ main +')();'));
  63. var handle=setInterval(function(){if(document.readyState=="complete"){if(document.getElementById("main_content")){clearInterval(handle);handle=0;document.getElementById("main_content").appendChild(script);}}},500);
Advertisement
Add Comment
Please, Sign In to add comment