Thumb

TwittSeven

By dofy Last update Oct 18, 2009 — Installed 4,624 times. Daily Installs: 16, 16, 19, 11, 13, 15, 19, 20, 25, 18, 24, 17, 12, 12, 11, 13, 17, 13, 11, 13, 8, 8, 11, 14, 30, 12, 13, 3, 12, 5, 11, 4

There are 73 previous versions of this script.

Add Syntax Highlighting (this will take a few seconds, probably freezing your browser while it works)

// ==UserScript==
// @name           TwittSeven
// @namespace      http://phpz.org/
// @description    Manual/auto update twitter timeline.
// @version        2.9.3
// @author         Seven Yu
// @include        http://twitter.com/*
// @include        https://twitter.com/*
// @require        http://gm.phpz.org/public/checkVersion.js
// @resource       img_icon http://gm.phpz.org/public/img_icon.gif
// ==/UserScript==

var username = ''; // your username of twitter.com
var password = ''; // and password

var waitForUpload  = 3;  // 自动加载频率 (单位:分钟 min)
var waitForPreview = 0.7 // in reply to 预览延时 (单位:秒 sec)
var checkMaxTimes  = 15  // 最后访问点最多检测页数 (pages)

GM_addStyle(<><![CDATA[
.status, .direct_message {
    min-height: 60px;
}
.unread {
    background-color: #ff9;
}
.tome {
    background-color: #cff;
}
.ts_selected {
    background-color: #f00;
}
.auto_update {
    float: right;
    margin: 0 3px;
    padding: 5px 5px 5px 10px;
    background-color: #e8f7fa;
}
.auto_update a {
    margin: 0;
    text-decoration: none;
    padding: 3px 5px 3px 18px;
}
#ts_ltv {
    text-align: center;
    background-color: #eee;
}
.ts_ret {
    background-image: url("data:image/gif,GIF89a%10%00%10%00%B3%00%00%FF%FF%FF%ED%ED%ED%DB%DB%DB%C8%C8%C8%B6%B6%B6%92%92%92%80%80%80%5B%5B%5BIII%FF%FF%FF%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%01%07%00%09%00%2C%00%00%00%00%10%00%10%00%00%04R0%C9I%2B-8g%9B%0A%F8%20%60%20%96%17~%06A%5E'%8A%BC%2C%60%82%C6H%81%C7%01%06%3C%A9a9%1D%AF%D7%09%1As%C3%80%EF%18D%04%04P%D2k%8A%C8!%A0%D8%95%04w%85%0E%BE%DA%84%E1%D3%15%7C%C1%941%80%E7%3D%87G%D4x%98c%89%00%00%3B");
	background-repeat: no-repeat;

    background-position:50% 50%;
    display:block;
    outline-color:-moz-use-text-color;
    outline-style:none;
    outline-width:medium;
    padding:3px 8px;
    visibility:hidden;
}
.ts_edt {
    padding: 3px 12px;
    background-image: url("data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%19tEXtSoftware%00Adobe%20ImageReadyq%C9e%3C%00%00%01uIDATx%DAb%FC%FF%FF%3F%03%25%80%05%9B%60WW%17%3ES%83%CB%CA%CA%D6%E14%00%A6%D9%C3%C3%83AOO%0F%9B%E1k%81%14%23%8C%CF%84%AE%19%A4%11%06%BE%7F%FF%CE%F0%F5%EBW%86%CF%9F%3F3%7C%FC%F8%11%2Cfmm%0D%A2%1A%808%09%88%C5%98%D05%03m%DD%02%13%FB%FB%F7%2F%18%FF%FB%F7%8F%81%85%05%E2%D8%85%D3%3B%19%D2b%FC%EA%81L9%20%F6a%04%05%22%BA%9F%A5%A5%A5_%EA%EA%EA%8A%CB%CB%CB%83%F9%CC%CC%CC%0C%8C%8C%8C%0C%85%E9%91%0C%BD%81%BF%C0b%C5%EB%D9%18f-%D9%D4%08%0F%03d%A7%03%81%B8%82%82%02%D8fl%9A%AF%BE%60b%B0P%F8%07v%09%DC%00P%80%81%FC%0A%D2%04%C2%20%A7%E3%D2%8C%0CPx0%3F%83%BC%05%D2%CC%C4%C4%C4P%94%11%85%A1%F9%DAs%26%86%13%0F%98%C0%5E%C0j%00%CCfB%9A%81%DCG%2C%D8%0C%E0%E0%E0%60(%CE%8Cfh%EC%F2f%F8%02%14%7F%B8~%23V%CD%40%BC%05%C5%05%7F%FE%FC%81G%1B%0C%DC%BC%F6%92%E1%87%BA%05%5Cs%7Cf9%03L3%10%BF%C2%EA%85_%BF%20%CE%AE%2F%DB%0A%A6o%5D%7F%05%B3%99%E1%D3%A7O%0C0%CD%18I%19f%00(6%60%60%D9%823%60%BA%B9w6%98%BE%7B%F7.%03L3%86%01%A0%B8%87%01%90m%D8%C0%B7o%DF%B0%E6F%3F%60j%DCDd%0E%F6C%E6%00%04%18%00%F2%D6%D5%AD%19%E2%F4N%00%00%00%00IEND%AEB%60%82");
	background-repeat: no-repeat;
    background-position: 50%;
}
.ts_refresh {
    background-image: url("data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%19tEXtSoftware%00Adobe%20ImageReadyq%C9e%3C%00%00%02%E6IDATx%DA%A4SIOSQ%14%3E%F7%0D%1D%A0%94ZZl%11%11%A8%A0%88q%C1%03%C5H%D5%20*%91%18M%14%17%86%98%B8%E2%0F%B8%F1o%18%17.%DC%98%E8%02%E2%14c%0CA%A3%A2%C1%10%A5%04Q)%04(%16h%19J%A1%A5tz%C3%BD%9E%D7%A2%12V%26%BE%E4%CB%1D%DE%FD%BE%7B%BEs%CE%25%8C1%F8%9FO8zg%26%3F%11E%10%04%018%9E%B7%11B%BC%B8u%0A%D1%84(DL!%06%F1%B2%A7%94%D20%CF%F3%5D%B8%F6%BE%BBY%DA-%EC%10%AC%168v%AD%AA%CC%7C%A4%BC%D4%5Cg-%14%AAq%CF%9AH%AAu%0B%AB%99%96%99%F9%E4%15%CA%D8%A0%A6%AA%ED%BF%09%DC%1F*cn%83%C0%BC%1D%C7%9D%AE%CAR%B3%D5%40%88Q%91)%E8%10%09)%D8%E70%95%9For%9CV%91%DCy%C6-%E1%98%8F%9Cj%1A%60%C8%06%02Z%DD%B9F%97u%3D%9A%01%8D%03%3A%11%8C%87%E6%96R%11%DDB%85%AB%D0%D6P%BB%CB%13_W%E0%B2%D7%25m%C4%B3(%2C%E7%054E%D1%C7%92J%97%D5%12%5DI%02%E5%09y%F39%3C%8E%A2%1F8Ax%A5%E2%C1%400%7B7%10%5Ck%BE%DAV)-%2F%C4%C1b5%82%9A%CD%E6%05%E4%FC%C4b%2F%10%0CrV%A1%DF%E7c%8B%A8%DEO%08%8C%A8%A9%94%1E%5D%17%A2%F9zG%AD%94%88e%404%E8%AE%19%C8%BF%05%F4%1B%D0%BFb6%F2%11U%D5V%83s%D1%84F%B5%91%BC5%0EDQ%F4%EA%A5~%F0d%CC%B7%3D%DB(%9A%5B%0B%E9dR_%DC~%FE%FA%87%B4%B3%C6%94%A9%3E%24wc%D9%20w%11!R%AB%F7%40%91%88%A5%EA%7B%3B%1E%FC%1B%01%80%D4~%B1IJe%F2%99-)6%C2%B3%C7C%3E%14%FE%88u%CF%91E%83%A1aw%99%FDl4C%13TN%2F%2B%D9l8'%A0lyY%CC%00%AC%26%18T8L%D0%FBh%40%0Fo%08%C9%0F5%B4%82%0Dv%01%F3r%D2%E1%D9k%8E%82%90L%85%17e%E4mnO%22%C4y%03%D8%9C%26%18%8B)Ps%A9UZ%F92f%8B%CC%86%86%F1W%B2x%8FCv%D6%D7%A4%C3%9C)%E0.%12%C0%3F5%BF%A9)jt%BB%050ZL%F0%E9%5E%8F%AF%F2F%A74%2F3%E0%A5F%8F%FB%98%DE%C9%B0%91QY(DY%C0U%C8%81%AF%E7%E5%86%92L%FB%B1%E5sD%C29%0F%02%DFrkx%2Bo%7D%C4h%3C%E18%5C%BB_%F4T%D9%89%DD%5E%A0%0B%B0%E8Z%40%9E%9E%F1%C7%FC%D3c%CA%C2%B7%5E%3A%F9%22%40W%26%40%AF%8E%40%23%13%E5%B0%EC%1F%87l%7C%94%FA%EE%0Fp%A5%87%DE%2F%FDlk%81%92%9Az%E0D%FD-l%82%9A%F9%0A%AB%FEQ6%DD%3F%C9%D6gM%B8%E7D%AC!4%A2%3F%16%84%A8%DBA%F0%FF%F0%82%F5%D6M!%D2%18%01%FD%25%C0%002%B4l%A7%01%CA%5C%D4%00%00%00%00IEND%AEB%60%82");
	background-repeat: no-repeat;
}
.ts_read_all {
    background-image: url("data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%19tEXtSoftware%00Adobe%20ImageReadyq%C9e%3C%00%00%02%B0IDATx%DA%A4%93_H%D3Q%14%C7%BFw%CE%3Fm%3A%B76%9C%AE%E9%5C%F3O%E6p%D3%E5D%C5%CAY4%5DRD!D%F4PB%3E%D4C%F4%10%88T%84%0F%BD%14%08%25%15%D5CA%05%9A%11m%A8%19hQ%1A%E9d%11%3A*MV%A6%88.%A7.%DD%FF%DB%FD%E5%7C%0A)%F0%07%1F%CE%E1%FC%BB%E7w%EE%B9%84R%8A%8D%7C%FC%D6z%1E%B8%12%5C%1D%0EBV%1D%9C%3C%7D%97b%BF%81%C0R%B4%EA%8B0%7CN%8A8%3E%CC%E1%08%9A%03A4%F1%FE%E3%90R%96w%87%83%E9%3BY!%F3%B2%0F%CDe%D5%87%0C%BFV%D0%CC%FFGr%96%D5N%0BZn%5D9%C9%B5%60%BD%D1(eI%E9F%D3.C%DB%FD%0E%7B(%BC~%07b%16%7F%86%C9%BDcc%E37%3B%DF%25%C2%3F3%83%9A%FA%8B%07%93%B4d%EA%C9%E3W%F6%C9Y4E%22%E8%E2%3A%88e%243d%8C4%96X%9C%20%14%17g%EA%CA%F4%23u%05j%9B%CD%8A%AA*3%3A%BB%FD%A8T%7D%C1%BEc%E7j%03%A1%AB6%96%EC%E3%E6%C2g%83%B9%90%20%94%14%C7%0BE%02A%92T%24K%CF%96K%E4JYp)%C0_%9At%A3%DA(%40%87%F5)L%26%0Bl%DD%CB%A8R%8F%C3t%E0%84%A5%BB%FD%DE%02%2B%D0%C7c%95z%BC%0B%F3%FE%5CcME%9A%C6%A0%F3yh%EA%C4%80%93%EF%9D%9D%87%40)%C1%CF%D1%EF%A8-Z%84%D5%DA%86%8A%0A%0B%9E%8F%A4a%D1%B5%88P%08%3C%06b%CC%3A%E2%EA%B7SO%7F%EF%60%5E%22%85B%98%2CDJ%B6%121q%B1%98v%7C%85D%93%8A%90%D7%8F%3C%F12%1E%BEt%20'%A7%04ug%2F%3F*%D7%B0%9B%A1%98X%BB%85%AEB-Y%99s%0F%9F%CF-%D5V%8F%0D%3B%C1%A7%3Cd%14d%C1%3B%E7%01%F5%85!V%C9q%3C%DF%03cC%5D%3B%8Boe%BF%FF%86%5B%99%18%C26f%C1%0Bh%14%C4%E5%18%A5%EE%C1%B7%1F%F2J%CAu%8A-%DB31%E5t!%E4%0FB%A4%94a%FE%D3%14%DA%9E%3DxmH%C7%B5%A1o%A4%2Fc3%F0%F1%07%5B%B8%B5%7BS%A7%10%A8%84%14%E5ZR%C9%E6%D2%98%9DS%BA'%C2%14%8D%3E%17s%2C%B9%AB%B7g%80m%DF%25%F7%0A%5EXG%08%D6%7D%02*)%C1n5%CCG%F21%D4y%FD0%BD%7Dj%07%3D%AA%C7%7B%D3V%98%E5I%7F%C7s%1Dp%CB%24d%88%18%5C%C8%26%CE%A6W%A0p%9B%14%0D%DC%FE%7Fv%A3%C51%8D%5Ef%0F0%FC%8C%60T%0F%93h%918F%7C%14~%D4%C6%F5%18%89%12f%84%A2r%8D%3F%3E%B2%D1%E7%FC%5B%80%01%00%9A%7F%1A%1D%97%5E0%8E%00%00%00%00IEND%AEB%60%82");
	background-repeat: no-repeat;
}
#update-submit {
    position: relative;
    z-index: 2;
}
#show_txt, #show_pic {
    color: #ddd;
    z-index: 1600;
    min-width: 4px;
    min-height: 4px;
    position: absolute;
    border: 5px solid #666;
    background-color: #333;
}
#show_txt hr {
    margin: 5px;
    clear: both;
    display: block;
}
#show_pic {
    line-height: 0;
    min-width: 16px;
    min-height: 16px;
}
#show_pic img {
    max-width: 300px;
    max-height: 200px;
}
#show_txt {
    padding: 6px;
    max-width: 350px;
    text-align: left;
    font-size: 1.2em;
    line-height: 1.4em;
}
#show_txt img {
    float: left;
    width: 50px;
    height: 50px;
    margin: 5px 10px 5px 5px;
}
#show_txt .status-body a {
    margin-right: 5px;
    color: #fff;
}
#show_txt .meta {
    color:#999;
    float: right;
    font-family:georgia;
    font-size:0.8em;
    font-style:italic;
}
#show_txt .meta a {
    color: #999;
}
#fly_state {
    top: 0;
    left: 0;
    color: #fff;
    width: 100%;
    z-index: 1500;
    padding: 10px;
    font-size: 18px;
    position: fixed;
    font-weight: bold;
}
.ts_loading {
    background: url(http://assets0.twitter.com/images/loader.gif) no-repeat right bottom;
}
]]></>);

// ----------------------------------------------------
var $, jQuery;
var frmStatus;
var interid, inReplyTimeId, stateTimeId;
var delid = 0, lastid = 0, unread = 0;
var runfirst = true, lastview, runtimes = 1;
var me_name = '';
var curScrollTop = 0;
var is_updating = false;
var title = 'Twitter', token = '';
var lastInReplyId = 0;
var rpc_data = {};
var STATUS_NOT_FOUND = 'No status found with that ID.<br />Perhaps it has been deleted.';
// resource
var img_icon = GM_getResourceURL('img_icon');
// template
var edt = '<a class="ts_edt" title="edit" href="javascript:void 0;">&nbsp;&nbsp;</a>';
var ret = '<a class="ts_ret" title="retweet" href="javascript:void 0;">&nbsp;&nbsp;</a>';
var loc = '<span class="lock-icon" title="{rname}&rsquo;s tweets are protected."/>';
var del = '<a title="delete this update" class="del">&nbsp;&nbsp;</a>';
var rep = '<a title="reply to {screen_name}" href="/home?status=@{screen_name}%20&amp;in_reply_to_status_id={id}&amp;in_reply_to={screen_name}" class="reply">&nbsp;&nbsp;</a>';
var rto = '<a class="ts_inreply" href="/{replyto}/status/{statusid}">in reply to {replyto}</a>'
var tmp = '<li id="status_{id}" class="unread hentry status u-{screen_name} {tome} {mine}">' + 
        '<span class="thumb vcard author"><a class="url" href="/{screen_name}">' + 
        '<img width="48" height="48" src="{profile_image_url}" class="photo fn" alt="{screen_name}"/>' + 
        '</a></span><span class="status-body"><strong>' + 
        '<a title="{screen_name}" class="tweet-url screen-name" href="/{screen_name}">' + 
        '{screen_name}</a></strong> {lock}<span class="entry-content">{text}</span>' + 
        '<span class="meta entry-meta"><a rel="bookmark" class="entry-date" href="/{screen_name}/status/{id}">' + 
        '<span class="published">{created_at}</span></a> <span>from {source}</span> {replyto}</span></span>' + 
        '<span class="actions"><div>' + 
        '<a title="favorite this update" id="status_star_{id}" class="fav-action non-fav">&nbsp;&nbsp;</a>' + 
        '{action}' + ret + '</div></span></li>';
        
var showRep = '';

loading();

function loading()
{
    unsafeWindow.jQuery ? init() : setTimeout(loading, 100);
}

function init()
{
    $ = jQuery = unsafeWindow.jQuery;
    me_name = $.trim($('#me_name').text());
    token = $('#authenticity_token').val();
    lastview = getCookie('ts_last') || 0;
    inShowPage() && checkPage() && createUI() && bindEvents() && successHandler() && gotoLastView();
}

function checkPage()
{
    return 'home,replies,direct_messages,favorites,search,profile,profile_favorites'
           .indexOf(document.body.id) != -1;
}

function createUI()
{
    if($('#auto_update').size() > 0) return false;
    lastid = getLastId();
    $('<div id="show_txt" class="round"></div><div id="show_pic" class="round ts_loading"></div>').appendTo('body').hide();
    $('<div class="auto_update round" >' +
        '<input id="auto_update" type="checkbox"> <label for="auto_update">Auto</label> ' +
        '<a href="javascript:void 0;" class="ts_refresh" title="Refresh">Refresh</a> ' +
        '<a href="javascript:void 0;" class="ts_read_all" title="Mark all as read">Mark all as read</a>' +
        '</div>')
        .prependTo('#timeline_heading')
        .find('a').click(function()
        {
            this.blur();
            if(this.className == 'ts_refresh')
                update();
            else
            {
                unread = 0;
                $('.unread').removeClass('unread').removeClass('tome');
                document.title = document.title.replace(/\(\d+\)\s/g, '');;
            }
        }).end()
        .find(':checkbox').click(function()
        {
            if(this.checked)
            {
                loopUpdate();
                update();
            }
            else
            {
                clearInterval(interid);
                setCookie('ts_auto', 'false', 60 * 60 * 24 * 15);
            }
        });
    frmStatus = $('#status_update_box');
    $('#status').css('height', '3.5em');
    $('<div id="fly_state"></div>').appendTo('body').hide();
    // auto update
    if(getCookie('ts_auto') != 'false')
    {
        document.getElementById('auto_update').checked = true;
        loopUpdate();
    }
    return true;
}

function bindEvents()
{
    $.ajaxSetup(
    {
        'username': username,
        'password': password
    });
    $(window)
        .ajaxSuccess(successHandler)
        .ajaxError(function(e, XHR, settings)
        {
            if(XHR.status == '404')
            {
                var id = settings.url.split('/').pop().split('.')[0];
                setData('id_' + id, STATUS_NOT_FOUND);
                $('#show_txt').html(STATUS_NOT_FOUND).removeClass('ts_loading');
            }
        })
        .keydown(function(e)
        {
            if(e.altKey && e.which == 65)
            {
                var status = document.getElementById('status');
                status && status.focus();
            }
        })
        .scroll(function(e)
        {
            if(getAutoUpdate() && nearingBottom())
            {
                $('#more[class*=loading]').size() == 0 && $('#more').click();
                $('#search_more[class*=loading]').size() == 0 && $('#search_more').click();
            }
        })
        .mouseup(function(e)
        {
            if($.inArray(e.target.id, 'status,update-submit'.split(',')) != -1) return ;
            resetDelid();
            frmStatus.prependTo('#content>.wrapper');
        });
    $('fieldset').mouseup(function(e){e.stopPropagation();});
    $('.unread').live('mouseover', function(){$(this).removeClass('unread tome');unread--;checkUnread();});
    $('.ts_ret,.ts_edt,.reply')
        .live('mousedown', function(e)
        {
            curScrollTop = e.pageY - e.clientY;
        })
        .live('mouseup', setStatus);
    $(':input').keyup(function(e)
    {
        e.stopPropagation();
    });
    $('#status').keydown(function(e)
    {
        e.stopPropagation();
        if(e.ctrlKey && e.which == 13)
        {
            $(this.form).submit();
        }
        else if(e.which == 27)
        {
            $(window).mouseup();
        }
    });
    $('#status_update_form')
        .submit(function()
        {
            $('#status')[0].disabled = 'disabled';
            clearTimeout(stateTimeId);
            $('#fly_state').show().css('background-color', '#BE1414').html('Sending...');
            frmStatus.prependTo('#content>.wrapper');
            if(delid > 0)
            {
                $.post('/status/destroy/' + delid
                , {'authenticity_token': token}
                , function()
                {
                    $('#status_' + delid).fadeOut(function(){$(this).remove();resetDelid();});
                });
            }
        });
    $('.entry-meta>a:gt(0)[href*=/status/],.ts_inreply')
        .live('mouseover', function(e)
        {
            var id = this.href.split('/').pop();
            if(lastInReplyId > 0 && lastInReplyId <= id)
                return;
            if(lastInReplyId == 0)
            {
                var offset = $(this).offset();
                $('#show_txt').html('').css({'left':offset.left, 'top':offset.top});
            }
            inReplyTimeId = setTimeout(function()
            {
                showInReplyTo(id);
            }, 1000 * waitForPreview);
        })
        .live('mouseout', function()
        {
            clearTimeout(inReplyTimeId);
        });
    $('#show_txt')
        .mouseleave(function(){this.style.display = 'none';lastInReplyId = 0;});
    return true;
}

function showInReplyTo(id)
{
    var html = lastInReplyId == 0 ? '' : $('#show_txt').html() + '<hr />';
    lastInReplyId = id;
    if(getData('id_' + id))
    {
        html += getData('id_' + id);
        $('#show_txt').show().html(html).removeClass('ts_loading');
    }
    else
    {
        $('#show_txt').show().addClass('ts_loading');        
        $.getJSON('/statuses/show/' + id + '.json', function(data)
        {
            var reto = '';
            if(data.in_reply_to_status_id)
            {
                reto =  rto.replace(/\{replyto\}/g, data.in_reply_to_screen_name);
                reto = reto.replace(/\{statusid\}/g, data.in_reply_to_status_id);
            }
            var cont  = '<img src="' + data.user.profile_image_url + '" alt="' + data.user.screen_name + '" />' + 
                        '<span class="status-body">' +
                        '<strong><a href="/' + data.user.screen_name + '" title="' + data.user.name + '">' + 
                        data.user.screen_name + '</a></strong> ' +
                        textHandler(data.text) + '</span>' + 
                        '<span class="meta"><a href="/' + data.user.screen_name + '/status/' + data.id + '">' + 
                        getTime(data.created_at) + '</a> from ' + data.source + ' ' + reto + '</span> ';
            setData('id_' + data.id, cont);
            html += cont;
            $('#show_txt').show().html(html).removeClass('ts_loading');
        });
    }
}

function successHandler(e, XHR, settings)
{
    document.body.id == 'home' && setTimeout(function(){setCookie('ts_last', getLastId(), 60 * 60 * 24 * 15);}, 500);
    $('#fly_state').css('background-color', '#008000').html('Send completed.');
    stateTimeId = setTimeout(function(){$('#fly_state').fadeOut('slow');}, 3000);
    $('.actions:not(:has(.ts_ret))').find('div:eq(0)').append(ret);
    'sent,profile'.indexOf(document.body.id) == -1  && $('.mine:not(:has(.ts_edt))').find('.meta').append(edt);
    $('#loader').hide();
    settings && settings.url == '/status/update' && statusBlur();
    showImage();
    showTinyUrl();
    runfirst && settings && settings.url.indexOf('/home?max_id') != -1 && setTimeout(gotoLastView, 100);
    return true;
}

function inShowPage()
{
    if(document.body.id == 'show')
    {
        $(ret).appendTo($('#permalink .actions'))
        .click(function()
        {
            var user = $('#permalink .screen-name').text();
            var cont = $('#permalink .entry-content').html();
            cont = cont.replace(/@<a[^\>]*>([^<]*)<\/a>/gi, '@$1');
            cont = cont.replace(/<img[^>]*>/gi, '');
            cont = cont.replace(/<a[^\>]*>(#[^\s]*)<\/a>/gi, '$1');
            cont = cont.replace(/<a[^\>]*href=\"([^\s]*)\"[^\>]*>[^<]*<\/a>/gi, '$1');
            cont = cont.replace(/<[^>]*>/gi, '');
            cont = cont.replace(/\s*rt:?\s*@/ig, ' > @');
            cont = cont.replace(/:\s+&gt;\s+@/ig, ': > @');
            cont = 'RT @' + user + ': ' + cont + ' //';

            location.href = '/home?status=' + urlencode(cont);
        });
    }
    return true;
}

function gotoLastView()
{
    if(runfirst == false || lastview == 0 || document.body.id != 'home') return;
    var cat = '#ts_ltv', gostatus = $('#status_' + lastview);
    runfirst = gostatus.length == 0 && ++runtimes < checkMaxTimes;
    if(runfirst)
        $('#more').click();
    else
    {
        gostatus.length > 0 
            ? gostatus.before('<li id="ts_ltv"><span class="meta">~~~~~~~ Last visit ~~~~~~~</span></li>') 
            : cat = '#footer';
        window.scrollTo(0, $(cat).offset().top - window.innerHeight + 60);
    }
}

function setStatus(e)
{
    e.stopPropagation();
    resetDelid();
    $(this).closest('li').append(frmStatus);
    if(this.className == 'reply')
    {
        setTimeout(function(){window.scrollTo(0, curScrollTop);}, 0);
        return;
    }
    var base = $(this).closest('li').find('.status-body');
    var user = base.find('.screen-name').text() || $.trim($('#content h2:eq(0)').text() || base.find('a:eq(0)').text());
    var cont = base.find('span').filter('.entry-content').html();
        cont = cont.replace(/@<a[^\>]*>([^<]*)<\/a>/gi, '@$1');
        cont = cont.replace(/<img[^>]*>/gi, '');
        cont = cont.replace(/<a[^\>]*>(#[^<]*)<\/a>/gi, '$1');
        cont = cont.replace(/<a[^\>]*href=\"([^\s]*)\"[^\>]*>[^<]*<\/a>/gi, '$1');
        cont = cont.replace(/<[^>]*>/gi, '');

    switch(this.className)
    {
        case 'ts_ret':
        {
            cont = cont.replace(/\s*rt:?\s*@/ig, ' > @');
            cont = cont.replace(/:\s+&gt;\s+@/ig, ': > @');
            cont = 'RT @' + user + ': ' + cont + ' //';
            break;
        }
        case 'ts_edt':
        {
            var relk = base.find('.meta a:contains(in reply to)');
            if(relk.length > 0)
            {
                $('#in_reply_to').val(relk.text().replace('in reply to ', ''));
                $('#in_reply_to_status_id').val(relk.attr('href').split('/').pop());
            }
            delid = base.find('.meta a.entry-date').attr('href').split('/').pop();
            break;
        }
        default:
        {
            break;
        }
    }
    $('#status_update_box:visible').size()>0 ? $('#status').val(cont).focus() : location.href = '/home?status=' + urlencode(cont);
}

function showTinyUrl()
{
    // bit.ly
    createTinyUrl('[href*=/bit.ly/]',
        'http://bit.ly/favicon.ico',
        function(shortUrl)
        {
            var hash = shortUrl.split('/').pop();
            var login  = 'dofy';
            var apiKey = 'R_19826aa48e46d889fc1bed17df804835';
            var url = 'http://api.bit.ly/expand?callback=?&version=2.0.1&shortUrl=' + hash + '&login=' + login + '&apiKey=' + apiKey;
            $.getJSON(url, function(data)
            {
                var result;
                if(data.errorCode == 0)
                {
                    result = data.results[hash].longUrl;
                }
                else
                {
                    result = data.errorMessage;
                }
                setData(shortUrl, result);
                $('#show_txt').removeClass('ts_loading').html(result);
            });
        })
}

function createTinyUrl(filter, icon, handler)
{
    $('.entry-content,.msgtxt')
        .filter(':not(:has(img))')
        .find(filter)
        .append('<img src="' + icon + '" />')
        .hover(function(e)
        {
            var offset = $(this).offset();
            var height = $(this).height();
            $('#show_txt').html('').css({'left':offset.left, 'top':offset.top + height});
            if(getData(this.href))
            {
                $('#show_txt').show().html(getData(this.href));
            }
            else
            {
                $('#show_txt').show().addClass('ts_loading');
                handler(this.href);
            }
        }, function(e)
        {
            $('#show_txt').hide();
        });
}

function showImage()
{
    // all image
    createImagePreview('[href$=.jpg],[href$=.jpeg],[href$=.gif],[href$=.png],[href$=.bmp]', img_icon);
    // twitpic
    createImagePreview(
        '[href*=twitpic.com]',
        'http://twitpic.com/favicon.ico',
        function(obj)
        {
            var imageid  = obj.href.split('/').pop();
            return obj.href.replace(imageid, 'show/thumb/' + imageid);
        });
}

function createImagePreview(filter, icon, previewurl)
{
    $('.entry-content,.msgtxt')
        .filter(':not(:has(img))')
        .find(filter)
        .append('<img src="' + icon + '" />')
        .hover(function(e)
        {
            var imgurl = previewurl ? previewurl(this) : this.href;
            $('#show_pic').css({'left':e.pageX+20, 'top':e.pageY})
                .show().html('<img src="' + imgurl + '" />');
        }, function()
        {
            $('#show_pic').hide();
        });
}

function loopUpdate()
{
    interid = setInterval(update, 1000 * 60 * waitForUpload);
}

function update()
{
    if(is_updating) return;
    is_updating = true;
    var url = '';
    setCookie('ts_auto', getAutoUpdate(), 60 * 60 * 24 * 15);
    switch(document.body.id)
    {
        case 'home':
            url = 'friends_timeline';
            break;
        case 'replies':
            url = 'replies';
            break;
        default:
            url = '';
            return;
    }
    if(url == '') return;
    url = '/statuses/' + url + '.json';

    getAjaxData(url + '?since_id=' + lastid, 
        function(data)
        {
            $('#timeline').prepend(data);
            lastid = getLastId();
        });
}

function getLastId()
{
    return getStatusId(0);
}

function getStatusId(index)
{
    return $('#timeline > li[id!=ts_ltv]')[index].id.replace('status_', '');
}

function selectStatus(index)
{
    var status = $('#status_' + getStatusId(index));
    var y = status.offset().top - window.innerHeight / 3;
    $('.ts_selected').removeClass('ts_selected');
    status.addClass('ts_selected');
    window.scrollTo(0, y);
}

function getAutoUpdate()
{
    return document.getElementById('auto_update').checked;
}

function getAjaxData(dataurl, callback)
{
    $.ajax(
    {
        'url' : dataurl,
        'dataType': 'json',
        'error': function(){$('#loader').hide();is_updating = false;},
        'success': function(data)
        {
            var ids = [];
            $('#loader').hide();
            for(var i=0, l=data.length, item='', list=''; i<l; i++)
            {
                var reto = '';
                ids.push(data[i].id);
                lock = data[i].user.protected ? loc.replace(/\{rname\}/g, data[i].user.name) : '';
                if(data[i].in_reply_to_status_id)
                {
                    reto =  rto.replace(/\{replyto\}/g, data[i].in_reply_to_screen_name);
                    reto = reto.replace(/\{statusid\}/g, data[i].in_reply_to_status_id);
                }
                unread++;
                item = tmp;
                item = item.replace(/\{mine\}/g, me_name == data[i].user.screen_name ? 'mine' : '');
                item = item.replace(/\{tome\}/g, me_name == data[i].in_reply_to_screen_name ? 'tome' : '');
                item = item.replace(/\{text\}/g, textHandler(data[i].text));
                item = item.replace(/\{profile_image_url\}/g, data[i].user.profile_image_url);
                item = item.replace(/\{source\}/g, data[i].source);
                item = item.replace(/\{created_at\}/g, getTime(data[i].created_at));
                item = item.replace(/\{action\}/g, me_name == data[i].user.screen_name ? del : rep);
                item = item.replace(/\{lock\}/g, lock);
                item = item.replace(/\{replyto\}/g, reto);
                item = item.replace(/\{id\}/g, data[i].id);
                item = item.replace(/\{screen_name\}/g, data[i].user.screen_name);
                list += item;
            }
            ids.length > 0 && $('#status_' + ids.join(',#status_')).remove();
            callback(list);
            checkUnread();
            is_updating = false;
        },
    });
}

function getTime(time)
{
    var m = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var d = new Date(time.replace('+', 'GMT+'));
    return d.getHours() + ':' + d.getMinutes() + ' ' + m[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear();
}

function checkUnread()
{
    title = document.title.replace(/\(\d+\)\s/g, '');
    document.title = (unread > 0 ? '(' + unread + ') ' : '') + title;
}

function statusBlur()
{
    var status = document.getElementById('status');
    if(status)
    {
        status.disabled = 'disabled';
        setTimeout(function(){$('#status')[0].disabled = '';}, 500);
    }
}

function resetDelid()
{
    delid = 0;
}

function textHandler(text)
{
    var reg_url = /((?:http|https|ftp|telnet|file)\:\/\/[^\s]+)/ig;
    var reg_rpl = /@([a-z0-9_]+)/ig;
    var reg_tag = /#([^\s]+)/ig;
    text = text.replace(reg_url, '<a href="$1" target="_blank">$1</a>');
    text = text.replace(reg_rpl, '@<a href="/$1">$1</a>');
    text = text.replace(reg_tag, '<a href="/search?q=%23$1">#$1</a>');
    return text;
}

function nearingBottom() {
    var viewportBottom = window.scrollY + window.innerHeight,
        nearNextPageLink = document.body.clientHeight - window.innerHeight/4;
    return viewportBottom >= nearNextPageLink
}

// Data
function setData(key, value)
{
    rpc_data[keyHandler(key)] = value;
}

function getData(key, value)
{
    return rpc_data[keyHandler(key)] || value;
}

function keyHandler(key)
{
    return key.replace('http://', '').replace(/\.|\-|\//g, '_');
}

// Cookie
function setCookie(sName, sValue, iTime){
    var date = new Date();
    date.setTime(date.getTime()+iTime*1000);
    document.cookie = escape(sName) + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}

function getCookie(sName){
    var aCookie = document.cookie.split("; ");
    for (var i=0; i <aCookie.length; i++){
        var aCrumb = aCookie[i].split("=");
        if (escape(sName) == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    return null;
}

function delCookie(sName){
    var date = new Date();
    document.cookie = sName + "= ; expires=" + date.toGMTString();
}

// urlencode
function urlencode(str)
{
    // ! @   #   $   %   ^ &   *()_ +   - =   /   ?   ,   <.> ;   :   '"[{]}`~\|
    // ! %40 %23 %24 %25 ^ %26 *()_ %2B - %3D %2F %3F %2C <.> %3B %3A '"[{]}`~\|
    str = str.replace(/\%/ig, '%25');
    
    str = str.replace(/\#/g, '%23');
    str = str.replace(/\$/g, '%24');
    str = str.replace(/\&/g, '%26');
    str = str.replace(/\+/g, '%2B');
    str = str.replace(/\,/g, '%2C');
    str = str.replace(/\//g, '%2F');
    str = str.replace(/\:/g, '%3A');
    str = str.replace(/\;/g, '%3B');
    str = str.replace(/\=/g, '%3D');
    str = str.replace(/\?/g, '%3F');
    str = str.replace(/\@/g, '%40');
    return str;
}

// check new version
var name = 'TwittSeven', version = '293';
checkVersion(name, version);