Created
August 26, 2012 18:28
-
-
Save sdvg/3482358 to your computer and use it in GitHub Desktop.
INWX.de Domain-"price sorter"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name inwx.de price-sorter | |
// @namespace inwx.de-price-sorter-SD12 | |
// @version 0.1 | |
// @description appends buttons to the domain list to sort the list after prices | |
// @match http://www.inwx.de/de/domain/* | |
// @copyright 2012+, Stefan Dietz <dev@stefan-dietz.eu> | |
// ==/UserScript== | |
var appendControls = function() { | |
var fd_result = document.getElementById('fd_result'); | |
var sortWrapper = document.createElement('div'); | |
sortWrapper.id = 'sortWrapper'; | |
sortWrapper.style.marginBottom = '35px'; | |
sortWrapper.style.clear = 'both'; | |
var btn_upward = document.createElement('button'); | |
btn_upward.style.float = 'right'; | |
btn_upward.innerHTML = '▲ price upward' | |
btn_upward.addEventListener('click', sort_upward); | |
var btn_downward = document.createElement('button'); | |
btn_downward.style.float = 'right'; | |
btn_downward.innerHTML = '▼ price downward'; | |
btn_downward.addEventListener('click', sort_downward); | |
sortWrapper.appendChild(btn_downward); | |
sortWrapper.appendChild(btn_upward); | |
fd_result.parentNode.insertBefore(sortWrapper, fd_result); | |
} | |
window.onload = function() { | |
appendControls(); | |
} | |
var sort_downward = function() { | |
sort(true); | |
} | |
var sort_upward = function() { | |
sort(false); | |
} | |
var sort = function(reverse) { | |
var domainList = document.querySelectorAll('div[id^=domain_container_]:not(.check_domain_row_taken)'); | |
var items = new Array(); | |
for( var i in domainList ) { | |
if (domainList[i].nodeType !== 1) { | |
continue; | |
} | |
var node = domainList[i].cloneNode(true); | |
var priceSpan = node.querySelector('.domain_extra span'); | |
if(priceSpan === null) { | |
continue; | |
} | |
var originalPrice = priceSpan.querySelector('font'); | |
if( originalPrice != null ) { | |
priceSpan.removeChild(originalPrice); | |
} | |
var price = parseFloat(priceSpan.innerText.replace('.', '').match(/[0-9]+(,)?[0-9]+/)[0].replace(',', '.')) | |
items.push({ | |
'node': domainList[i], | |
'price': price | |
}); | |
} | |
items.sort(function(a, b) { | |
return a.price == b.price | |
? 0 | |
: (a.price > b.price ? 1 : -1); | |
}); | |
if( reverse === true ) { | |
items = items.reverse(); | |
} | |
var unavailableDomains = document.querySelectorAll('div[id^=domain_container_].check_domain_row_taken'); | |
var newList = document.getElementById('fd_result').cloneNode(); | |
items.forEach(function(item) { | |
newList.appendChild(item.node); | |
}); | |
unavailableDomains = Array.prototype.slice.call(unavailableDomains); | |
unavailableDomains.forEach(function(item) { | |
newList.appendChild(item); | |
}); | |
var oldNode = document.getElementById('fd_result'); | |
oldNode.parentNode.replaceChild(newList, oldNode); | |
} | |
Array.prototype.reverse = function () { | |
var tmp_arr = new Array(); | |
for (var i = this.length - 1; i >= 0; i--) | |
{ | |
tmp_arr.push (this[i]); | |
} | |
return tmp_arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment