// ==UserScript==
// @name            Anti MyMiniCity (LM plugin)
// @namespace       http://twitter.com/kosugi
// @description     hide/notice all posts includes links leads to MyMiniCity
// @include         http://twitter.com/*
// @include         http://favotter.matope.com/*
// @include         http://twitter.1x1.jp/search/*
// ==/UserScript==
//
// version: 2008-06-23T13:11:26+09:00
// author:  KOSUGI Tomo (kosugi dot tomo at gmail dot com)
// license: GPL <http://www.gnu.org/copyleft/gpl.html>
//
// LM-priority: 200
//

new function() {

    // handle mode: choose 'notice', 'hide' or 'remove'
    //   notice: replace the anchor text to '<MyMiniCity Link>'
    //   hide:   hide the post includes links
    //   remove: remove the user from following (DANGEROUS!! DO NOT USE)
    //
    var ACTION = 'notice';

    var handle = function() {}
    if (ACTION == 'notice') {
        handle = function(node) {
            node.textContent = '<MyMiniCity Link>';
        }
    } else if (ACTION == 'hide') {
        handle = function(node) {
            var row = node.parentNode.parentNode.parentNode;
            if (row.className == 'hentry')
                row.style.display = 'none';
        }
    } else if (ACTION == 'remove') {
        handle = function(node) {
            var row = node.parentNode.parentNode.parentNode;
            var username = '';
            if (row.className == 'hentry')
                username = document.evaluate('td[@class="content"]/strong/a/text()', row, null, XPathResult.STRING_TYPE, null).stringValue;
            if (username.length == 0 && usernamePattern.test(window.location.href))
                username = RegExp.$1;
            urlPattern.test(node.href);
            var subdomain = RegExp.$1;
            if (username == subdomain) // limitation for safety
                GM_xmlhttpRequest({ method: 'GET', url: 'http://twitter.com/friendships/destroy/' + encodeURIComponent(username) });
        }
    }

    var usernamePattern = /^http:\/\/twitter\.com\/([^\/\?]+)/;
    var urlPattern = /^http:\/\/([^\.]+)\.myminicity\.com/;

    var doIt = function(node, chain) {
        if (!node.getAttribute('anti-myminicity') && urlPattern.test(node.href)) {
            node.setAttribute('anti-myminicity', '1');
            handle(node);
        }
        chain();
    }

    setTimeout(function() {
        if (window.LinkManipulator) {
            window.LinkManipulator.addManipulator(doIt);
        }
    }, 200);
}
