ynet talkbacker

By Yehuda B. Last update Oct 20, 2008 — Installed 269 times. Daily Installs: 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0
// ynet talkbacker script
// version 0.1
// 2008-10-21
// Copyright (c) 2008, Yehuda B.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// http://www.gnu.org/licenses/gpl.html
//
// ==UserScript==
// @name		ynet talkbacker
// @description	Give better feedback on talkback recommendations and support negative recommendations on ynet news portal. Version: 0.1
// @namespace	http://yehudab.com
// @include		http://www.ynet.co.il/Ext/App/TalkBack/CdaDisplayTalkbackIframe/*
// @include		http://www.ynet.co.il/articles/*
// ==/UserScript==

var AppBaseUrl = "http://yehudab.com/apps/";
var unexpectedError = "ynet talkbacker:\r\n\u05ea\u05e7\u05dc\u05d4 \u05dc\u05d0 \u05e6\u05e4\u05d5\u05d9\u05d9\u05d4. \u05e0\u05e1\u05d4 \u05e9\u05e0\u05d9\u05ea \u05de\u05d0\u05d5\u05d7\u05e8 \u05d9\u05d5\u05ea\u05e8.";   // תקלה לא צפוייה. נסה שנית מאוחר יותר.
var allRecommendations = new Array();
function setRecommendationTd(id, prevRecom)
{
	var tdElement = document.getElementById("recommendationTd" + id);
	if (tdElement != null)
	{
		var pos = "", neg = "";
		if (prevRecom != null)
		{
			pos = " (" + prevRecom.pos + ")";
			neg = " (" + prevRecom.neg + ")";
			if (prevRecom.pos - prevRecom.neg != 0)
			{
				var title = document.getElementsByClassName("text12");
				if (title != null)
					paintElement(title[0], prevRecom.pos - prevRecom.neg)
			}
		}
		tdElement.innerHTML = '\u05dc\u05d4\u05de\u05dc\u05e6\u05d4:' + //'להמלצה:'
			' <a href="#" onclick="recommendPlusTb(' + id + '); return false;" class=bluelink style="font-size:11;font-weight:bold;">\u05d1\u05e2\u05d3' + pos +'</a> /' + //בעד
			' <a href="#" onclick="recommendMinusTb(' + id + '); return false;" class=bluelink style="font-size:11;font-weight:bold;">\u05e0\u05d2\u05d3' + neg +'</a>'; //נגד
	}	
}
function addOptions(id)
{
	unsafeWindow.setRecommendationLabel = function() {
		setRecommendationTd(id, unsafeWindow.parent.getRecommendationsForTb(id));
		unsafeWindow.pageLoad();
	}
}
function getTBId()
{
	var matches = location.href.match(/0,11381,L-([0-9]+),00\.html/);
	if (matches)
	{
		return parseInt(matches[1]);
	}
	return null;
}

function recommend(id, isPos)
{
	var url = AppBaseUrl + "talkbacker/recommend.php";
	var data = "site_id=1&article_id=" + unsafeWindow.dcContentID + "&tb_id=" + id + (isPos ? "&positive" : "&negative") + "=1";
	window.setTimeout(
		function() {
			GM_xmlhttpRequest({
				method:"POST",
				headers:{'Content-type':'application/x-www-form-urlencoded'},
				data:data,
				url:url,
				onload:function(details) 
				{
					if (details.status != 200)
					{
						GM_log("url: " + url + " status:" + details.status);
						alert(unexpectedError);
					}
					else if (details.responseText.indexOf("Error 1062:") >= 0)
					{
						alert("\u05d0\u05d9\u05df \u05d0\u05e4\u05e9\u05e8\u05d5\u05ea \u05dc\u05d4\u05de\u05dc\u05e6\u05d4 \u05db\u05e4\u05d5\u05dc\u05d4");  //"אין אפשרות להמלצה כפולה"
					}
					else if (details.responseText != "OK")
					{
						GM_log("url: " + url + " response:" + details.responseText);
						alert(unexpectedError);
					}
					else
					{
						if (typeof allRecommendations[id] == "undefined")
						{
							allRecommendations[id] = {pos: 0, neg: 0};
						}
						if (isPos)
						 	allRecommendations[id].pos++;
						else
							allRecommendations[id].neg++;
						paintTb(id);
						setRecommendationTd(id, allRecommendations[id]);
					}
				}
			});
		}, 
		0);
}

function getRecommendations()
{
	var url = AppBaseUrl + "talkbacker/getRecommendation.php";
	var data = "site_id=1&article_id=" + unsafeWindow.dcContentID;
	window.setTimeout(
		function() {
			GM_xmlhttpRequest({
				method:"POST",
				headers:{'Content-type':'application/x-www-form-urlencoded'},
				data:data,
				url:url,
				onload:function(details) 
				{
					if (details.status != 200)
					{
						GM_log("url: " + url + " status:" + details.status);
						alert(unexpectedError);
					}
					else if (details.responseText.indexOf("OK") != 0)
					{
						GM_log("url: " + url + " response:" + details.responseText);
						alert(unexpectedError);
					}
					else 
					{
						unsafeWindow.paintRecommendations(details.responseText.substr(3));
					}
				}
			});
		}, 
		0);
}
function paintTb(tb_id)
{
	var pos = 0, neg = 0;
	if (typeof allRecommendations[tb_id] != "undefined")
	{
		pos = allRecommendations[tb_id].pos;
		neg = allRecommendations[tb_id].neg;
	}
	var total = pos - neg;

	tbTitle = document.getElementById("viewTbTitle" + tb_id);
	if (tbTitle != null)
	{
		tbInner = tbTitle.rows[1].cells[1].firstChild.firstChild;
		paintElement(tbInner, total);
		return true;
	}
	return false;
}

function paintElement(element, total){
	var color = "", fontWeight = "", textDecoration = "";
	if (total > 0)
	{
		color = "#9B1A1F";
		fontWeight = "bold";
	}
	else if (total < 0)
	{
		color = "blue";
		fontWeight = "bold";
		textDecoration = "line-through";
	}
	element.style.color = color;
	element.style.textDecoration = textDecoration;
	element.style.fontWeight = fontWeight;
}

unsafeWindow.paintRecommendations = function(response)
{
	if (response != "")
	{
		var arr = response.split("|");
		var i, l = arr.length, tb_id, anyFound = false;
		for (i = 0; i < l; i += 3)
		{
			tb_id = arr[i];
			allRecommendations[tb_id] = {pos: parseInt(arr[i+1]), neg: parseInt(arr[i+2])};
			anyFound = paintTb(tb_id) || anyFound;
		}
		if (!anyFound)
		{
			GM_log("retry...");
			window.setTimeout("paintRecommendations('" + response + "')", 500);
		}
	}
}

function setRcmdsFunctions()
{
	unsafeWindow.recommendPlusTb = function(id)
	{
		recommend(id, true);
	}
	unsafeWindow.recommendMinusTb = function(id)
	{
		recommend(id, false);
	}
	unsafeWindow.getRecommendationsForTb = function(id)
	{
		return allRecommendations[id];
	}
}
if (window.location.href.match(/\/Ext\/App\/TalkBack\/CdaDisplayTalkbackIframe\//))
{
	addOptions(getTBId());
}
else if (typeof unsafeWindow.dcContentID != "undefined")
{
	setRcmdsFunctions();
	getRecommendations();
}