UnfollowHater

Shows who unfollowed you on Tumblr

当前为 2015-06-05 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           UnfollowHater
// @namespace      tumblr-unfollowhater
// @description    Shows who unfollowed you on Tumblr
// @include        http://www.tumblr.com/blog/*
// @include        https://www.tumblr.com/blog/*
// @author		   Adrian Sanchez
// @version        2.0.0 beta
// @grant		   GM_xmlhttpRequest     
// @require       http://code.jquery.com/jquery-2.1.4.js
// @require       http://code.jquery.com/ui/1.11.4/jquery-ui.js
// ==/UserScript==

//***********************************************************************************      
//      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.
//
//      You should have received a copy of the GNU General Public License
//      along with this program.  If not, see http://www.gnu.org/licenses/gpl-3.0.html
//***********************************************************************************

function load_css(){
    var css_jqueryui = document.createElement('link');
    css_jqueryui.rel = 'stylesheet';
    css_jqueryui.type = 'text/css';
    css_jqueryui.href = 'https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css';
    
    var style = document.createElement('style');
    style.innerHTML = "#feedback { font-size: 1.4em; }"+
    " #selectable .ui-selecting { background: #FECA40; }"+
    " #selectable .ui-selected { background: #F39814; color: white; }"+
    " #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }"+
    " #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; text-align: center; }";
                                                         
     document.getElementsByTagName("head")[0].appendChild(css_jqueryui);
     document.getElementsByTagName("head")[0].appendChild(style);

}

function getTumblelog(){
	return document.URL.split("/")[4].replace(/[#]/gi,"");
}

function get_dom_from_string(data){
    var element = document.createElement('div');
    element.innerHTML = data;
    return element;
}

function process(){
    load_css();
    $.ajax({
        url: furl,
        cache: false,
        success: function(data) {
           var follower_list = get_follower_list(data);
           var current_list = jQuery.parseJSON(localStorage.getItem(getTumblelog()+"_followersList"));
           var storage_string_unfollower = localStorage.getItem(getTumblelog()+"_unfollowersList");
            
           var unfollowers_list = compare_lists(current_list,follower_list);
           var new_followers_list = compare_lists(follower_list,current_list);
           var total_unfollowers_list = (storage_string_unfollower!=null)?jQuery.parseJSON(storage_string_unfollower).concat(unfollowers_list):[];
            
            if ((unfollowers_list.length !== 0) || (unfollowers_list !=null)){
                localStorage.setItem(getTumblelog()+"_unfollowersList",JSON.stringify(total_unfollowers_list));
            }
            
           set_control(unfollowers_list,{function_name:'unfollower', title:'Unfollowers'});
           set_control(new_followers_list,{function_name:'new_follower', title:'New Followers'});
           set_control(total_unfollowers_list,{function_name:'total_unfollower', title:'Total Unfollowers'});
        }
    });
}

function get_follower_list(data){
    var followerPages = Math.ceil(get_follower_count(data)/followersPage);
    var furl_ext = furl+"/page/";
    var followerList = [];
    
    for(i=0; i<followerPages; i++){
        var a = i+1;
        $.ajax({
            async: false,
            url: furl_ext+a,
            cache: false,
            success: function(data) {
                var element = get_dom_from_string(data);
                $(element).find('.name').each(function(){
                    var link = $(this).find('a').attr('href');
                    followerList[followerList.length] = {name: $(this).text(),
                                       url: link};
                });
            }
        });
    }
    set_new_follower_list(followerList);
    return followerList;
}


function get_follower_count(data){
    var element = get_dom_from_string(data);
    return parseInt($(element).find('.title_and_controls').text().replace(/\,/g,'').replace(/\./g,'').replace(/\ /g,'').match("[0-9]+"));
}

function set_new_follower_list(list){
    if(virgen){
        localStorage.setItem(getTumblelog()+"_followersList",JSON.stringify(list));
    }
}

function compare_lists(current_list, new_list){
    var diff = [];
    for(var i=0; i<current_list.length; i++){
        var found=false;
        for(var j=0; j<new_list.length; j++){
            if(new_list[j]["name"]==current_list[i]["name"]){
                found=true;
                break;
            }
        }
        if(!found){
            diff[diff.length]=current_list[i];
        }
    }
    return diff;
}

function set_control(unfollower_list, control) {
    var unfollower_count = unfollower_list.length;
    $('#dashboard_controls_open_blog').append('<li id="'+control.function_name+'_control" class="controls_section_item "><a class="control-item control-anchor posts"><div class="hide_overflow">'+control.title+'</div><span id="'+control.function_name+'_count" class="count"></span></a></li>');
    $('#'+control.function_name+'_count').text(unfollower_count);
    
    var element = document.createElement('div');
    element.id = control.function_name+"_dialog";
    $('#'+control.function_name+'_control').click(function(){
        var list_element = display_list(unfollower_list);
        $('#'+element.id).empty();
        $(element).append(list_element);
        $(element).append(please_donate());
        $(element).dialog({
            title: control.title,
            close: function( event, ui ) {
                list_element.innerHTML="";
            }
        });
        $(element).position({
            my: "center"
        });
    });
    
}

function display_list(follower_list){
    var ol = document.createElement('ol');
    $(ol).selectable();
    for(i=0;i<follower_list.length;i++){
        var li = document.createElement('li');
        var a = document.createElement('a');
        
        $(a).attr('href',follower_list[i]["url"]).text(follower_list[i]["name"]);
        $(a).attr('target','_blank');
        $(li).addClass('ui-widget-content').append(a);
        $(ol).append(li);
    }
    return ol;
}

function please_donate(){
    var paypal_html='<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank">'+
        '<input type="hidden" name="cmd" value="_s-xclick">'+
        '<input type="hidden" name="hosted_button_id" value="WD4YNAQALVHLC">'+
        '<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">'+
        '<img alt="" border="0" src="https://www.paypalobjects.com/es_XC/i/scr/pixel.gif" width="1" height="1"></form>';
    
    var paypal_element = document.createElement('div');
    paypal_element.innerHTML = paypal_html;
    
    return paypal_element;
}

function top_icon(){
}

var followersPage = 40;
var blogUrl = window.location.protocol+"//www.tumblr.com/blog/";
var furl = blogUrl+getTumblelog()+"/followers";
var virgen = (localStorage.getItem(getTumblelog()+"_followersList")===null);
console.log("start");

$(document).ready(process);