Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name For sorting a table
- // @namespace table_sorting
- // @include /^https?://(www\.)?mytrafficvalue.com/monetization/(.*)shares/[0-9]{4}-[0-9]{2}-[0-9]{2}.html$/
- // @author facebook.com/igor39
- // @version 1.6
- // @grant none
- // @description For Tampermonkey & Greasemonkey
- // ==/UserScript==
- function main(){
- // for sorting any table from the page (including BAP transfers): mytrafficvalue.com/monetization/shares.html
- // http://kryogenix.org/code/browser/sorttable/
- // this script can sort the table, and now will work with both FireFox and Chrome.
- // if the pager is missing, you need to reload the page, if you need to change a number of lines per page.
- 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'),
- 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>',
- 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;}';
- 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]);
- if(window.location.href.match(/bap/g)){price_sign="";is="bap";market="BAP's Transfers";}else{price_sign="$";is="market";market="RP's Transfers";}
- function getById(id){return document.getElementById(id);}
- function getBySel(sel){return document.querySelector(sel);}
- 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);}
- 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;}}
- function adding_form(){ if(!getById('myform')){
- newDiv=document.createElement('div'); newDiv.innerHTML =my_form_code; getBySel('div.mainRound').appendChild(newDiv);
- 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+' ';}};
- }}
- 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+'"]');
- // we will also add the results of previous calculation to the next:
- 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;}}}
- 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;}}
- 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++;
- }}
- 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)
- onlyOne=0;price*=1;limit*=1;counter=0;clearInterval(handle);handle=0; prev_price[counter2]=price; prev_limit[counter2]=limit;counter2++;
- 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.
- 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.
- if(price>min_price){price=min_price;} if(limit<max_limit){limit=max_limit;}
- var start1 =new Date(),end1,a;
- if(is=="market"){limit+=0.0001; while(price <= limit){price= +price.toFixed(5);total_sum_on(price);price+=0.0001;}}
- else if(is=="bap"){while(price <= limit){total_sum_on(price);price+=1;}}
- getById("tbody1").innerHTML=tbody1_headers; getById("TheMarketID").innerHTML=market;
- for(a=0;a<arr.length;a++){if(arr[a]){getById("tbody1").innerHTML+=arr[a];arr[a]='';}}
- var i,x=document.querySelectorAll('tbody#tbody1 tr'),RPs_value=0,RPs_amount=0,text;
- 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 ');
- 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);
- }}
- function main2(){ adding_form();
- if(!getBySel('div.pageable table.sortable')&&script_loaded){getBySel('div.pageable table').className+=" sortable";
- sorttable.makeSortable(getBySel("div.pageable table"));// http://kryogenix.org/code/browser/sorttable/#ajaxtables
- 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;
- getById("input_submit2").onclick=function(){var price=getById("input_price").value,limit=getById("input_limit").value; run_total_sum_on(price,limit);};
- style=document.createElement('style'); style.appendChild(document.createTextNode(style_of_table)); elem=getById("main_content"); elem.insertBefore(style, elem.childNodes[0]);
- onlyOne=1; getById("TheMarketID").innerHTML=market;
- }} handle=setInterval(main2,100);}
- var script = document.createElement('script');script.setAttribute('id','mfnc0');
- script.appendChild(document.createTextNode('('+ main +')();'));
- 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