Dribbble Extender

Shows who follows you on your following list

目前為 2016-08-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Dribbble Extender
// @description  Shows who follows you on your following list
// @author       Kos
// @namespace    http://tampermonkey.net/
// @version      0.2
// @license      CC BY-SA 2.0
// @homepage     https://greasyfork.org/scripts/22003-dribbble-extender
// @include      https://dribbble.com/*/following
// @require	 https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @grant        none
// ==/UserScript==

;(function() {
    'use strict';
    
    var followers = [],
        cacheKey = location.pathname.replace(/\/(.*?)\/.*/, '$1');
    
    // functions for working with localStorage
    
    function lsTest()
    {
        var test = 'test';
        try
        {
            localStorage.setItem(test, test);
            localStorage.removeItem(test);
            return true;
        }
        catch(e)
        {
            return false;
        }
    }
        
    function getLocalStorageArray(name)
    {
        var list = localStorage.getItem(name);
        if (!list)
        {
            return [];
        }
        return JSON.parse(list);
    }
    
    function saveUpdatedFollowersList(list)
    {
        localStorage.setItem(cacheKey+'_followers_latest', JSON.stringify(list));
    }
    
    function addFollowerLocalStorage(id)
    {
        var list = getLocalStorageArray(cacheKey+'_followers_latest');
        
        if (list.indexOf(id) == -1)
        {
            list.push(id);
        }
        
        saveUpdatedFollowersList(list);
    }
    
    function saveFinishedAmountOfFollowers()
    {
        var list = getLocalStorageArray(cacheKey+'_followers_latest');
        
        localStorage.setItem(cacheKey+'_followers', JSON.stringify(list));
        saveUpdatedFollowersList([]);
        
        // update on page
        followers = list;
    }

    var localStorageAvailable = lsTest();
    
    $(document).ready(function(){
        var fetchActive = false,
            fullFetchFinished = false,
            curPage = 1,
            parseDelay = 200,
            repaintDelay = 2500,
            waitForAllFetch = false,
            skipParseCounter = 0;
        
        if (localStorageAvailable)
        {
            followers = getLocalStorageArray(cacheKey+'_followers');
            
            // if have previously saved followers, increase delay,
            // update on page only when all users parsed
            if (followers.length)
            {
                parseDelay += 300;
                waitForAllFetch = true;
            }
            
            // clear previously parsed data, to parse all new
            saveUpdatedFollowersList([]);
        }
        
        function getPage(page)
        {
            page = Math.round(page);
            fetchActive = true;
            $.ajax({
                url: 'https://'+document.location.hostname+document.location.pathname.replace('following', 'followers')+'?page='+page
            }).done(function(data){
                
                var match = data.match(/Dribbble\.PlayerCards\.update\({"id":\d+,"users":(\[.*?\])/),
                arr = JSON.parse(match[1]);
                
                for (var i = 0; i < arr.length; i++)
                {
                    setFollowed(arr[i].id);
                }
                
                fetchActive = false;
                curPage++;
            }).fail(function(data){
                fetchActive = false;
                if (data.status == 404)
                {
                    fullFetchFinished = true;
                    return;
                }
                if (data.status == 403)
                {
                    //alert('Request limit exceeded. Wait one minute and refresh page.');
                    // skip next n parse times
                    skipParseCounter = 20;
                    return;
                }
            });
        }
        
        function setFollowed(id)
        {
            id = Math.round(id);
            
            if (!waitForAllFetch && followers.indexOf(id) == -1)
            {
                followers.push(id);
            }
            
            if (localStorageAvailable)
            {
                addFollowerLocalStorage(id);
            }
        }
        
        function paintFollowed()
        {
            // clear previously set marks if any
            $('.us-follows-you').removeClass('us-follows-you');
            $('.us-follows-you-mark').remove();
            
            for (var i = 0; i < followers.length; i++)
            {
                var userBlock = $('.user-row-'+followers[i]);
                
                userBlock.addClass('us-follows-you');

                var title = userBlock.find('.hover-card-parent');
                title.html('<span title="Follows you" class="us-follows-you-mark" style="color:'+(!fullFetchFinished ? '#7d8e99' : '#2cff5a')+'">✓ </span>'+title.html());
            }
        }
        
        var interval = setInterval(function(){
            if (fullFetchFinished)
            {
                clearInterval(interval);
                if (localStorageAvailable)
                {
                    saveFinishedAmountOfFollowers();
                }
                return;
            }
            // skip and reduce counter
            if (skipParseCounter > 0)
            {
                skipParseCounter--;
                return;
            }
            
            if (!fetchActive)
            {
                getPage(curPage);
            }
        }, parseDelay);
        
        // paint then set repaint with delay
        paintFollowed();
        setInterval(paintFollowed, repaintDelay);
    });
})();