chuter / HaierUv

// ==UserScript==
// @name        HaierUv
// @version     1.0.0
// @author      chuter
// @namespace   http://www.chuter.com
// @description Get all weibo mid with specific keywords
// @require     http://libs.baidu.com/jquery/1.9.1/jquery.min.js
// @require     https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js
// @include     http://bbs.haier.com/forum/food/*
// @grant       GM_xmlhttpRequest
// @run-at      document-end
// ==/UserScript==

(function($) {
    xlog = function(message) {
        if (console) {
            console.log(message);
        }
    };

    /**
     * 只有等到条件满足之后执行callback,如果设置了最长等待时间,在等待时间超出该上限后会进行超时处理:
     * 如果超时回调设置,那么直接调用该回调方法,否则打印超时错误信息
     *
     * @param conditionCheckFunc 进行条件判定的方法
     * @param callback 条件成立时执行的回调方法
     * @param maxWaitTime 最长等待时间
     * @param timeoutCallBack 等待超时后执行的回调方法
     * @param interval 检查条件的时间间隔
     */
    waitfor = function(conditionCheckFunc, callback, maxWaitTime, timeoutCallBack, interval) {
        if (interval == null || typeof(interval) == 'undefined') {
            interval = 100;
        }

        while (! conditionCheckFunc()) {
            if (maxWaitTime == null || typeof(maxWaitTime) == 'undefined') {
                setTimeout(function() {
                    waitfor(conditionCheckFunc, callback, maxWaitTime, timeoutCallBack, interval);
                }, interval);
            } else if (maxWaitTime && maxWaitTime >= 0) {
                setTimeout(function() {
                    waitfor(conditionCheckFunc, callback, maxWaitTime, timeoutCallBack, interval);
                }, interval);

                maxWaitTime -= interval;
            } else {
                if (timeoutCallBack) {
                    console.log(typeof(timeoutCallBack));
                    timeoutCallBack();
                } else {
                    console.error('等待时间过长');
                }
            }

            return;
        }

        // Condition finally met. callback() can be executed.
        callback();
    };

    parseQueryString = function(url) {
        var urlkey2values = {};

        url.replace(
            new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
            function( $0, $1, $2, $3 ){
                urlkey2values[ $1 ] = $3;
            }
        );
        return urlkey2values;
    };

    getCurUrl = function() {
        var href = null;
        try {
            href = document.location.href;
        } catch (e) {
            xlog("exception where get weibo's url: " + e.name + ", " + e.message);
        }

        return href;
    };

    var appendScript = function(url, onload) {
        var script = document.createElement("script");
        script.setAttribute("src", url);
        if (onload) {
            script.addEventListener('load', onload, false);
        }
        document.body.appendChild(script);
    };

    var appendStyleSheet = function(url, onload) {
        var styleSheet = document.createElement("link");
        styleSheet.setAttribute('rel', 'stylesheet');
        styleSheet.setAttribute("href", url);
        if (onload) {
            styleSheet.addEventListener('load', onload, false);
        }
        document.head.insertBefore(styleSheet, document.head.firstChild);
    };

    var simulateClick = function(element) {
        if(document.createEvent) {
            var click = document.createEvent("MouseEvents");
            click.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0, false, false, false, false, 0, null);
            element.dispatchEvent(click);
            element.focus();
        } else if(document.documentElement.fireEvent) {
            element.fireEvent("onclick");
            element.focus();
        }
    };

    var uidCookieName = '_smt_uid';

    var showCookie = function() {
        var cookie = $.cookie(uidCookieName);
        var cookieHtml = '<p style="width:100%; font-size:36px; text-align:center; line-height:1.5;">' + cookie + '</p>'
        $('#globlePanel').append($(cookieHtml));
    };

    var addGloblePanel = function() {
        $('body').append($('<div id="globlePanel" class="panel global-sidePanel global-sidePanel--left u-textAlignCenter" style="color:#f0f0f0; background-color:rgba(0,0,0,0.6);"></div>'));

        showCookie();

        var height = $(document.body).height();
        var ratio = 0.1;

        function scrollToBottom() {
            if (ratio >= 1) {
                $.removeCookie(uidCookieName, {path: "/", domain: ".bbs.haier.com"});

                showCookie();

                var firstPage = $('a[page]')[0];
                simulateClick(firstPage);

                window.location.reload();
            }

            console.log(height+'--'+ratio);
            $(document).scrollTop(height * ratio);
            ratio += 0.1;
            setTimeout(scrollToBottom, 500);            
        }

        setTimeout(scrollToBottom, 500);
    };

    var extractPageWeiboIds = function() {
        var weiboIdsArr = [];

        $("div[action-type='feed_list_item']").each(function(index, ele) {
            $(ele).css('border', '2px solid red');
            weiboIdsArr.push($(ele).attr('mid'));
        });

        return weiboIdsArr;
    };

    var renderWeiboIds = function() {
        var weiboIdsArr = extractPageWeiboIds();

        var weiboIdsListHtml = '<div class="panel-title"><h3>抽取的微博ID</h3></div><div class="panel-body"><ul>';
        $(weiboIdsArr).each(function(index, elem) {
            weiboIdsListHtml += '<li>'+elem+'</li>';
        });
        weiboIdsListHtml += '</ul></div>';

        $('#globlePanel').append($(weiboIdsListHtml));
    };

    var createWorkbench = function() {
        addGloblePanel();

    };

    $(document).ready(function() {
        appendStyleSheet('http://123.56.85.253/cdn/css/global.css');
        appendStyleSheet('http://123.56.85.253/cdn/css/user_layout.css');

        createWorkbench();
    });
})(jQuery);