// ==UserScript==
// @name		Nettoyeur de resultats
// @namespace		http://www.guim.info
// @description		N'affiche pas les posts interdits aux freenautes
// @version		2009-02-19
// @author		Mathieu Guilbaud
// @license		GPL v2
// @include		http://www.guim.info/null/*
// @include		http://binnews.in/_bin/liste.php*
// @include		http://www.binnews.in/_bin/liste.php*
// @include		http://binnews.info/_bin/liste.php*
// @include		http://www.binnews.info/_bin/liste.php*
// ==/UserScript==

// sum recursively tags named 'childTag' of the element 'elm'
function countChildTag(elm, childTag) {
	var sum = 0, children = elm.childNodes;
	for (var i=children.length-1; i>=0; i--) {
		if (children[i].tagName != null && children[i].tagName.toLowerCase() == childTag.toLowerCase()) {
			sum++;
		}
		sum += countChildTag(children[i], childTag);
	}
	return sum;
}

// ---- delete unavailable posts
var arrayToDel = [];
var elm;

// select lines like "/td/a/img"
var iterator = document.evaluate("//img[@src='../_images/notfree.gif']/parent::a/parent::td", document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

while (elm = iterator.iterateNext()) { // elm is TD
	arrayToDel.push(elm.parentNode); // parentNode -> TR
}

// test for lines like "/td/div/a/img"
var iterator = document.evaluate("//img[@src='../_images/notfree.gif']/parent::a/parent::div", document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

while (elm = iterator.iterateNext()) { // elm is DIV
	var aItem = countChildTag(elm, 'a');
	var imgItem = countChildTag(elm, 'img');
	if (aItem == 2*imgItem) { // 2 times more a than img
		arrayToDel.push(elm.parentNode.parentNode); // parentNode.parentNode -> TR
	}
}

arrayToDel.forEach(function(e) {if (e.parentNode != null) e.parentNode.removeChild(e);});

// ---- transform column 'Fichier' as link to binsearch

var res = /.*&cat_id=([0-9]+).*/.exec(window.location.href);
var num = 7; //
if (res[1] == 20 || res[1] == 21 || res[1] == 3 || res[1] == 5 || res[1] == 32  || res[1] == 43
	|| res[1] == 10 || res[1] == 9 || res[1] == 12 || res[1] == 28 || res[1] == 33  || res[1] == 34
	|| res[1] == 35 || res[1] == 40 || res[1] == 41 || res[1] == 8 || res[1] == 51  || res[1] == 30
	|| res[1] == 25 || res[1] == 46 || res[1] == 23) {
	num = 6;
}
var iterator = document.evaluate("//tr[@class='ligneclaire']/td["+num+"]|//tr[@class='lignefoncee']/td["+num+"]", document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
var elmToChange = [];

while (elm = iterator.iterateNext()) { // elm is TD
	elmToChange.push(elm);
}
elmToChange.forEach(function(e) {e.innerHTML = "<a href=\"https://binsearch.info/?q="+e.innerHTML+"&max=25&adv_age=7&server=\" target=\"_blank\">"+e.innerHTML+"</a>";});


