iWiW messageboard fetcher and userblock

By KARASZI István Last update Feb 26, 2008 — Installed 632 times.
// iWiw messageboard full
// version 0.2
// 2008-02-26
// Copyright (c) 2008, Istvan Karaszi <iwiw@spam.raszi.hu>.
// All rights Reserved.
//
// ==UserScript==
// @name          iWiW messageboard fetcher and userblock
// @namespace     http://userscripts.org/users/20715/scripts
// @description	  For show all messages from the messageboard on the first page
// @include       http://*wiw.hu/pages/main/index.jsp
// @include       https://*wiw.hu/pages/main/index.jsp
// @include       http://*wiw.hu/pages/message/messageboard.jsp*
// @include       https://*wiw.hu/pages/message/messageboard.jsp*
// ==/UserScript==

const COOKIENAME = "iwiw-blocked-users";
const SPLITCHAR = ":";
const CSSRULES = [
	"div.blocked { min-height: 0 !important; } ",
	"div.blocked p { display: none; } ",
	"div.blocked a.user_image { display: none; } ",
	"div.blocked a { color: #a8b8de !important; }"
];

var users = _getBlockedUsers();

_appendCSSRules();
_addBlockButtons();
_formatMessageBoard();

var wLocation = String(window.location.href);
var wPath = String(window.location.pathname);
if (wPath.match(/messageboard.jsp/)) {
	// we don't fetch further results on messageboard page
	return;
}
var mBoardURL = wLocation.substr(0, wLocation.length - wPath.length) + "/pages/message/messageboard.jsp?page=";
var parser = new DOMParser();

var mBoardSnapshot = _XPath("//xhtml:div[contains(@class, 'messageboard')]");
//xhtml:div[contains(@class, 'sendtext')]");
if (mBoardSnapshot.snapshotLength < 1) return;

var mBoard = mBoardSnapshot.snapshotItem(0);

var mBoardSendSnapshot = _XPath("xhtml:div[contains(@class, 'sendtext')]", mBoard);
if (mBoardSendSnapshot.snapshotLength < 1) return;

var mBoardSend = mBoardSendSnapshot.snapshotItem(0);

//
// fetch further pages
//
var a = function( i, response ) {
		if (typeof(response) == "object") {
			_putMessages(i, response);
		}

		// there are only four pages of messages now
	   	if (i > 4) return;

		return _getMessages(i, function( response ) {
				a(++i, response);
		});
}

a(0);

function _putMessages( page, response ) {
	//
	// remove old elements because they're stripped
	//
	if (page == 1) {
		var olditems = _XPath(".//xhtml:div[contains(@class, 'item')]", mBoard);
		for(var i = 0; i < olditems.snapshotLength; i++) {
			mBoard.removeChild(olditems.snapshotItem(i));
		}
	}

	var dom = parser.parseFromString(response.responseText, "application/xml");

	var items = _XPath("//xhtml:div[contains(@class, 'messageboard_list_inner')]//xhtml:div[contains(@class, 'item')]", dom);
	for(var i = 0; i < items.snapshotLength; i++) {
		var newNode = document.importNode(items.snapshotItem(i), true);
		mBoard.insertBefore(newNode, mBoardSend);
	}

	_addBlockButtons();
	_formatMessageBoard();
}

function _getMessages( page, fnc ) {
	GM_xmlhttpRequest({
		method: "GET",
		url: mBoardURL + page,
		onload: function( response ) {
			if (typeof(fnc) != "function") return;
			fnc(response);
		}
	});
}

function _appendCSSRules() {
	for (var i = 0; i < CSSRULES.length; i++) {
		GM_addStyle(CSSRULES[i])
	}
}

function _getItems() {
	var items = _XPath("//xhtml:div[contains(@class, 'messageboard')]//xhtml:div[contains(@class, 'item')]");
	return items;
}

function _addBlockButtons() {
	var items = _getItems();
	for(var i = 0; i < items.snapshotLength; i++) {
		var item = items.snapshotItem(i);

		var uid = _getUserID(item);
		if (!uid) continue;

		_addBlockButton(uid, item);
	}
}

function _formatMessageBoard() {
	var items = _getItems();
	for(var i = 0; i < items.snapshotLength; i++) {
		var item = items.snapshotItem(i);

		var uid = _getUserID(item);
		if (!uid) continue;

		_formatItem(uid, item);
	}
}

function _isUserBlocked( uid ) {
	for(var i = 0; i < users.length; i++) {
		if (users[i] == uid) return i;
	}

	return null;
}

function _getBlockedUsers() {
	var users = new String(GM_getValue(COOKIENAME, ""));
	return users.split(SPLITCHAR);
}

function _setBlockedUsers() {
	GM_setValue(COOKIENAME, users.join(SPLITCHAR));
}

function _addBlockedUser( uid ) {
	if (_isUserBlocked(uid) == null) {
		users.push(uid);
		_setBlockedUsers(users);
	}
}

function _removeBlockedUser( uid ) {
	var pos = _isUserBlocked(uid);
	if (pos != null) {
		users.splice(pos, 1);
		_setBlockedUsers(users);
	}
}

function _formatItem( uid, item ) {
	if (_isUserBlocked(uid) != null) {
		// user is blocked
		_addClass(item, "blocked");
	} else {
		_removeClass(item, "blocked");
	}
}

function _addBlockButton( uid, item ) {
	var h5s = _XPath(".//xhtml:h5[1]", item);
	if (h5s.snapshotLength == 0) return;

	var h5 = h5s.snapshotItem(0);
	var blockButtons = _XPath(".//xhtml:a[contains(@class, 'block')]", h5);
	if (blockButtons.snapshotLength >= 1) return;

	h5.appendChild(unsafeWindow.document.createTextNode(" "));

	var blockButton = unsafeWindow.document.createElement("a");
	blockButton.href = "#";
	blockButton.className = "block";
	blockButton.appendChild(unsafeWindow.document.createTextNode("[block/unblock]"));
	blockButton.onclick = function() {
		return _handleClick(uid, item);
	}

	h5.appendChild(blockButton);
}

function _handleClick( uid, item ) {
	if (_isUserBlocked(uid) == null) {
		_addBlockedUser(uid);
	} else {
		_removeBlockedUser(uid);
	}

	_formatMessageBoard();
	return false;
}

function _addClass( item, newclass ) {
	var currentClass = new String(item.className);
	item.className = currentClass.concat(" blocked");
}

function _removeClass( item, oldclass ) {
	var currentClass = new String(item.className);
	var classes = currentClass.split(" ");
	
	var newclasses = new Array();
	for(var i = 0; i < classes.length; i++) {
		if (classes[i] == oldclass) continue;
		newclasses.push(classes[i]);
	}

	item.className = newclasses.join(" ");
}

function _getUserID( item ) {
	var results = _XPath(".//xhtml:h5/xhtml:a[contains(@href, 'userID')]", item);
	if (!results.snapshotLength) return null;

	var anchor = results.snapshotItem(0);

	var url = new String(anchor.href);
	var regex = /userID=(\d+)/;
	var matches = url.match(regex);

	if (!matches) return null;
	if (matches.length == 0) return null;

	return matches[1];
}
function _formatMessageBoard() {
	var items = _getItems();
	for(var i = 0; i < items.snapshotLength; i++) {
		var item = items.snapshotItem(i);

		var uid = _getUserID(item);
		if (!uid) continue;

		_formatItem(uid, item);
	}
}

function _XPath( query, context ) {
	if (!context) context = document;

	var d = context;
	if (context.ownerDocument) d = context.ownerDocument;

	return d.evaluate(query, context, nsResolver, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}

function nsResolver( prefix ) {
	var ns = {
		'xhtml' : 'http://www.w3.org/1999/xhtml'
	};

	return ns[prefix] || null;
}

// vim: ts=4 sw=4