Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Greasy Fork
- // @version 1.0.0
- // @description Enhancement for greasyfork.org site
- // @author Sonny Razzano
- // @namespace srazzano.com
- // @license CC by-nc-sa http://creativecommons.org/licenses/by-nc-sa/3.0/
- // @include https://greasyfork.org*
- // @grant none
- // ==/UserScript==
- // ********************** CUSTOMIZE TEXT **********************
- var sv_1 = '#';
- var sv_2 = 'Name';
- var sv_3 = 'Description';
- var sv_4 = 'Author';
- var sv_5 = 'Daily';
- var sv_6 = 'Total';
- var sv_7 = 'Created';
- var sv_8 = 'Updated';
- // ******************** END CUSTOMIZE TEXT ********************
- (function () {
- testGM();
- function testGM() {
- constSTORAGE_PREFIX = 'ustoe-';
- constLOG_PREFIX = 'greasyfork.org Enhancer: ';
- constLOG = true;
- constDEBUG = false;
- isGM = typeof GM_getValue != 'undefined' && typeof GM_getValue('a', 'b') != 'undefined';
- log = isGM ? function (msg) {
- if (LOG) GM_log(msg)
- }
- : window.opera ? function (msg) {
- if (LOG) opera.postError(LOG_PREFIX + msg)
- }
- : function (msg) {
- try {
- if (LOG) console.log(LOG_PREFIX + msg)
- } catch (e) {}
- }
- debug = function (msg) {
- if (LOG && DEBUG) log('** Debug: ' + msg + ' **')
- }
- addStyle = isGM ? GM_addStyle : function (css) {
- var head = $('head') [0];
- if (!head) return ;
- var style = $c('style', {
- type: 'text/css',
- innerHTML: css
- });
- head.appendChild(style)
- }
- setValue = isGM ? GM_setValue : function (name, value) {
- switch (typeof (value)) {
- case 'string':
- localStorage.setItem(STORAGE_PREFIX + name, 'S]' + value);
- break;
- case 'number':
- if (value.toString() .indexOf('.') < 0) {
- localStorage.setItem(STORAGE_PREFIX + name, 'N]' + value)
- }
- break;
- case 'boolean':
- localStorage.setItem(STORAGE_PREFIX + name, 'B]' + value);
- break
- } }
- getValue = isGM ? GM_getValue : function (name, defValue) {
- var value = localStorage.getItem(STORAGE_PREFIX + name);
- if (value == null) {
- return defValue
- } else {
- switch (value.substr(0, 2)) {
- case 'S]':
- return value.substr(2);
- case 'N]':
- return parseInt(value.substr(2));
- case 'B]':
- return value.substr(2) == 'true';
- } }
- return value
- }
- deleteValue = isGM ? GM_deleteValue : function (name) {
- localStorage.removeItem(STORAGE_PREFIX + name)
- }
- xhr = isGM ? GM_xmlhttpRequest : function (obj) {
- var request = new XMLHttpRequest();
- request.onreadystatechange = function () {
- if (obj.onreadystatechange) {
- obj.onreadystatechange(request)
- };
- if (request.readyState == 4 && obj.onload) {
- obj.onload(request)
- } }
- request.onerror = function () {
- if (obj.onerror) {
- obj.onerror(request)
- } }
- try {
- request.open(obj.method, obj.url, true)
- } catch (e) {
- if (obj.onerror) {
- obj.onerror({
- readyState: 4,
- responseHeaders: '',
- responseText: '',
- responseXML: '',
- status: 403,
- statusText: 'Forbidden'
- })
- };
- return
- }
- if (obj.headers) {
- for (name in obj.headers) {
- request.setRequestHeader(name, obj.headers[name])
- } }
- request.send(obj.data);
- return request;
- } }
- var scriptID = 'greasyfork';
- var version = '1.0.0';
- var url = window.location.href.toLowerCase();
- var onScriptPage = url.match(/^https?:\/\/greasyfork\.org\/scripts/);
- var onUserPage = url.match(/^https?:\/\/greasyfork\.org\/users\/\d+/);
- var scriptList = onScriptPage ? $('#browse-script-list') : $('#user-script-list');
- var scriptArray = [];
- var scripts = $('li', scriptList);
- var scriptCount = scripts.length;
- addStyle('\
- #script-table {-moz-user-select: none; border-collapse: separate !important; border-spacing: 0 !important;}\
- #script-table tr > #header2,\
- #script-table tr > #header4 {text-align: left;}\
- #script-table tr > #header1,\
- #script-table tr > #header2,\
- #script-table tr > #header4,\
- #script-table tr > #header5,\
- #script-table tr > #header6,\
- #script-table tr > #header7,\
- #script-table tr > #header8 {cursor: pointer;}\
- #script-table tr > #header3 {cursor: default; text-align: left;}\
- #script-table tr > td {}\
- #script-table tr > td:nth-child(1),\
- #script-table tr > td:nth-child(5),\
- #script-table tr > td:nth-child(6),\
- #script-table tr > td:nth-child(7),\
- #script-table tr > td:nth-child(8) {cursor: default;}\
- #script-table tr > td:nth-child(1),\
- #script-table tr > td:nth-child(5),\
- #script-table tr > td:nth-child(6),\
- #script-table tr > td:nth-child(7),\
- #script-table tr > td:nth-child(8) {text-align: center;}\
- #script-table tr > td:nth-child(2),\
- #script-table tr > td:nth-child(3) {cursor: default; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;}\
- #script-table tr > td:nth-child(1) {width: 44px;}\
- #script-table tr > td:nth-child(2) {width: 25%;}\
- #script-table tr > td:nth-child(3) {width: 50%;}\
- #script-table tr > td:nth-child(4) {width: 180px;}\
- #script-table tr > td:nth-child(5),\
- #script-table tr > td:nth-child(6) {width: 60px;}\
- #script-table tr > td:nth-child(7),\
- #script-table tr > td:nth-child(8) {width: 120px;}\
- #script-table tr > td:nth-child(2) a,\
- #script-table tr > td:nth-child(4) a {display: block !important;}\
- ');
- if(onUserPage) {
- addStyle('\
- #script-table tr > #header4,\
- #script-table tr > td:nth-child(4) {text-align: center;}\
- #script-table tr > td:nth-child(4) {cursor: default;}\
- #script-table tr > td:nth-child(4),\
- #script-table tr > td:nth-child(5) {width: 60px;}\
- ')
- }
- function $(q, root, single, context) {
- root = root || document;
- context = context || root;
- if (q[0] == '#') return root.getElementById(q.substr(1));
- if (q.match(/^[\/*]|^\.[\/\.]/)) {
- if (single) return root.evaluate(q, context, null, 9, null) .singleNodeValue;
- var arr = [];
- var xpr = root.evaluate(q, context, null, 7, null);
- for (var i = 0; i < xpr.snapshotLength; i++) arr.push(xpr.snapshotItem(i));
- return arr;
- }
- if (q[0] == '.') {
- if (single) return root.getElementsByClassName(q.substr(1)) [0];
- return root.getElementsByClassName(q.substr(1));
- }
- if (single) return root.getElementsByTagName(q) [0];
- return root.getElementsByTagName(q);
- }
- function $c(type, props, evls) {
- var node = document.createElement(type);
- if (props && typeof props == 'object') {
- for (prop in props) {
- if (typeof node[prop] == 'undefined') node.setAttribute(prop, props[prop]);
- else node[prop] = props[prop];
- } }
- if (evls instanceof Array) {
- for (var i = 0; i < evls.length; i++) {
- var evl = evls[i];
- if (typeof evl.type == 'string' && typeof evl.fn == 'function') node.addEventListener(evl.type, evl.fn, false);
- } }
- return node;
- }
- function toCustStr(num) {
- return num.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
- }
- String.prototype.toCustNum = function () {
- return parseFloat(this.replace(',', ''));
- }
- function insertAfter(newNode, refNode) {
- if (refNode.nextSibling) return refNode.parentNode.insertBefore(newNode, refNode.nextSibling);
- else return refNode.parentNode.appendChild(newNode);
- }
- function remove(node) {
- if (node) node.parentNode.removeChild(node);
- }
- function ucFirst(str) {
- var firstLetter = str.slice(0, 1);
- return firstLetter.toUpperCase() + str.substring(1);
- }
- function capAll(str) {
- var words = str.toLowerCase() .split(' ');
- for (var i = 0; i < words.length; i++) {
- var wd = words[i],
- first = wd.substr(0, 1),
- rest = wd.substr(1, wd.length - 1);
- words[i] = first.toUpperCase() + rest;
- }
- return words.join(' ');
- }
- for(var i = 0; i < scriptCount; i++) {
- var scriptObj = {};
- var script = scripts[i];
- var link = $('a', script, 1);
- var desc = $('p', script, 1);
- var auth = $('dd', script, 1);
- var num = $('dd', script);
- scriptObj.name = link.textContent;
- scriptObj.nhref = link.href;
- scriptObj.description = desc.textContent;
- scriptObj.author = auth.textContent;
- scriptObj.ahref = auth.lastChild.href;
- scriptObj.daily = num[1].textContent;
- scriptObj.total = num[2].textContent;
- scriptObj.created = num[3].textContent;
- scriptObj.updated = num[4].textContent;
- scriptArray.push(scriptObj);
- }
- var scriptTable = $c('table', {id:'script-table'});
- var scriptTableHeaderRow = $c('tr');
- if(onScriptPage) var theaders = [sv_1, sv_2, sv_3, sv_4, sv_5, sv_6, sv_7, sv_8];
- else var theaders = [sv_1, sv_2, sv_3, sv_5, sv_6, sv_7, sv_8];
- for(var i = 0; i < theaders.length; i++) scriptTableHeaderRow.appendChild($c('th', {id:'header' + (i+1), className:'header', textContent:theaders[i]}));
- scriptTable.appendChild(scriptTableHeaderRow);
- for(var i = 0; i < scriptArray.length; i++) {
- var script = scriptArray[i], row = $c('tr');
- row.appendChild($c('td', {textContent:(i + 1)}));
- var cellN = $c('td');
- cellN.appendChild($c('a', {href:script.nhref, textContent:script.name, title:script.name}));
- var cellDe = $c('td', {textContent:script.description, title:script.description});
- if(onScriptPage) {
- var cellA = $c('td');
- cellA.appendChild($c('a', {href:script.ahref, textContent:script.author}));
- }
- var cellDa = $c('td', {textContent:script.daily});
- var cellT = $c('td', {textContent:script.total});
- var cellC = $c('td', {textContent:script.created});
- var cellU = $c('td', {textContent:script.updated});
- row.appendChild(cellN);
- row.appendChild(cellDe);
- if(onScriptPage) row.appendChild(cellA);
- row.appendChild(cellDa);
- row.appendChild(cellT);
- row.appendChild(cellC);
- row.appendChild(cellU);
- scriptTable.appendChild(row);
- }
- var tableContainer = $c('div', {id:'table-container'});
- tableContainer.appendChild(scriptTable);
- scriptList.parentNode.replaceChild(tableContainer, scriptList);
- var th = $('th', scriptTable);
- for(var i = 0; i < th.length; i++) {
- th[i].addEventListener('click', function(e) {
- if(e.target.nodeName == 'TH' && e.target.textContent != sv_3)
- sortTable(e.target);
- },false);
- }
- var colIndex = 0;
- function sortTable(source) {
- var table = source;
- while(table.nodeName.toLowerCase() != 'table') {table = table.parentNode}
- var newRows = [];
- for(var i = 0; i < table.rows.length - 1; i++) {newRows[i] = table.rows[i + 1]}
- if(colIndex == source.cellIndex) newRows.reverse();
- else {
- colIndex = source.cellIndex;
- var cell = table.rows[1].cells[colIndex].textContent;
- if(colIndex == 0) newRows.sort(sortF);
- else newRows.sort(sortT);
- }
- function sortR(a, b) {
- return a.localeCompare(b);
- }
- function sortF(a, b) {
- var res = a.cells[colIndex].textContent.toCustNum() - b.cells[colIndex].textContent.toCustNum();
- if(res == 0) {
- var index = (colIndex == 2) ? 3 : 2;
- res = a.cells[index].textContent.toCustNum() - b.cells[index].textContent.toCustNum();
- }
- if(res == 0) res = sortR(a, b);
- return res;
- }
- function sortT(a, b) {
- a = a.cells[colIndex].textContent.toLowerCase();
- b = b.cells[colIndex].textContent.toLowerCase();
- return a.localeCompare(b);
- }
- for(var i = 0; i < newRows.length; i++) {table.appendChild(newRows[i])}
- }
- }) ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement