Kirneh / Twitch Clips Sorting

// ==UserScript==
// @name         Twitch Clips Sorting
// @homepageURL  https://hk-inc.dk/
// @supportURL   http://twitter.com/Kirnehx
// @version      0.1
// @description  Sort your Twitch Clips!
// @author       Kirnehx
// @match        https://clips.twitch.tv/my-clips
// @grant        none
// @license      MIT
// @updateURL https://openuserjs.org/meta/Kirneh/Twitch_Clips_Sorting.meta.js
// ==/UserScript==

//For more info see: https://github.com/tristen/tablesort

function sorting_init()
{
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = "https://code.jquery.com/jquery-latest.js?"+Math.random();
    var head = document.getElementsByTagName('head')[0];
    if(head) head.appendChild(script);

    var script2 = document.createElement('script');
    script2.type = 'text/javascript';
    script2.innerHTML = '!function(){function a(b,c){if(!(this instanceof a))return new a(b,c);if(!b||"TABLE"!==b.tagName)throw new Error("Element must be a table");this.init(b,c||{})}var b=[],c=function(a){var b;return window.CustomEvent&&"function"==typeof window.CustomEvent?b=new CustomEvent(a):(b=document.createEvent("CustomEvent"),b.initCustomEvent(a,!1,!1,void 0)),b},d=function(a){return a.getAttribute("data-sort")||a.textContent||a.innerText||""},e=function(a,b){return a=a.toLowerCase(),b=b.toLowerCase(),a===b?0:a<b?1:-1},f=function(a,b){return function(c,d){var e=a(c.td,d.td);return 0===e?b?d.index-c.index:c.index-d.index:e}};a.extend=function(a,c,d){if("function"!=typeof c||"function"!=typeof d)throw new Error("Pattern and sort must be a function");b.push({name:a,pattern:c,sort:d})},a.prototype={init:function(a,b){var c,d,e,f,g=this;if(g.table=a,g.thead=!1,g.options=b,a.rows&&a.rows.length>0)if(a.tHead&&a.tHead.rows.length>0){for(e=0;e<a.tHead.rows.length;e++)if("thead"===a.tHead.rows[e].getAttribute("data-sort-method")){c=a.tHead.rows[e];break}c||(c=a.tHead.rows[a.tHead.rows.length-1]),g.thead=!0}else c=a.rows[0];if(c){var h=function(){g.current&&g.current!==this&&g.current.removeAttribute("aria-sort"),g.current=this,g.sortTable(this)};for(e=0;e<c.cells.length;e++)f=c.cells[e],f.setAttribute("role","columnheader"),"none"!==f.getAttribute("data-sort-method")&&(f.tabindex=0,f.addEventListener("click",h,!1),null!==f.getAttribute("data-sort-default")&&(d=f));d&&(g.current=d,g.sortTable(d))}},sortTable:function(a,g){var h=this,i=a.cellIndex,j=e,k="",l=[],m=h.thead?0:1,n=a.getAttribute("data-sort-method"),o=a.getAttribute("aria-sort");if(h.table.dispatchEvent(c("beforeSort")),g||(o="ascending"===o?"descending":"descending"===o?"ascending":h.options.descending?"ascending":"descending",a.setAttribute("aria-sort",o)),!(h.table.rows.length<2)){if(!n){for(;l.length<3&&m<h.table.tBodies[0].rows.length;)k=d(h.table.tBodies[0].rows[m].cells[i]),k=k.trim(),k.length>0&&l.push(k),m++;if(!l)return}for(m=0;m<b.length;m++)if(k=b[m],n){if(k.name===n){j=k.sort;break}}else if(l.every(k.pattern)){j=k.sort;break}for(h.col=i,m=0;m<h.table.tBodies.length;m++){var p,q=[],r={},s=0,t=0;if(!(h.table.tBodies[m].rows.length<2)){for(p=0;p<h.table.tBodies[m].rows.length;p++)k=h.table.tBodies[m].rows[p],"none"===k.getAttribute("data-sort-method")?r[s]=k:q.push({tr:k,td:d(k.cells[h.col]),index:s}),s++;for("descending"===o?(q.sort(f(j,!0)),q.reverse()):q.sort(f(j,!1)),p=0;p<s;p++)r[p]?(k=r[p],t++):k=q[p-t].tr,h.table.tBodies[m].appendChild(k)}}h.table.dispatchEvent(c("afterSort"))}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}},"undefined"!=typeof module&&module.exports?module.exports=a:window.Tablesort=a}();';
    if(head) head.appendChild(script2);

    var script3 = document.createElement('script');
    script3.type = 'text/javascript';
    script3.innerHTML = "(function(){var cleanNumber=function(i){return i.replace(/[^\-?0-9.]/g,'');},compareNumber=function(a,b){a=parseFloat(a);b=parseFloat(b);a=isNaN(a)?0:a;b=isNaN(b)?0:b;return a- b;};Tablesort.extend('number',function(item){return item.match(/^-?[£\x24Û¢´€]?\d+\s*([,\.]\d{0,2})/)||item.match(/^-?\d+\s*([,\.]\d{0,2})?[£\x24Û¢´€]/)||item.match(/^-?(\d)*-?([,\.]){0,1}-?(\d)+([E,e][\-+][\d]+)?%?$/);},function(a,b){a=cleanNumber(a);b=cleanNumber(b);return compareNumber(b,a);});}());";
    if(head) head.appendChild(script3);

    var script4 = document.createElement('script');
    script4.type = 'text/javascript';
    script4.innerHTML = "function sort(){document.getElementsByClassName('table')[0].id='SortMe'; document.getElementsByClassName('table__cell--header')[3].setAttribute('data-sort-method','number'); document.getElementsByClassName('table__cell--header')[3].setAttribute('data-sort-default',''); var sort = new Tablesort(document.getElementById('SortMe'));}";
    if(head) head.appendChild(script4);

    var css = '#darkToggle {position: fixed;bottom: 10px;left: 10px;padding: 6px 11px;border-radius: 3px;background: #282828;color: ##888;} #darkToggle:hover { background: #333; } .table__cell { cursor: pointer !important;}',
        style = document.createElement('style');

    style.type = 'text/css';
    if (style.styleSheet){
        style.styleSheet.cssText = css;
    } else {
        style.appendChild(document.createTextNode(css));
    }

    head.appendChild(style);
    var a = document.createElement('a');
    a.innerHTML = "Toggle Sorting";
    a.setAttribute('onClick','sort()');
    a.id="darkToggle";
    document.body.appendChild(a);
}

window.onload = sorting_init();