Greasy Fork

Greasy Fork is available in English.

新浪微博相册采集

自动获取用户的微博相册,只能在用户主页使用有效

当前为 2019-04-26 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         新浪微博相册采集
// @namespace    http://qqoq.net/
// @version      0.1
// @description  自动获取用户的微博相册,只能在用户主页使用有效
// @author       老萨
// @require      https://cdn.bootcss.com/jquery/3.4.0/jquery.min.js
// @require      https://cdn.bootcss.com/viewerjs/1.3.3/viewer.min.js
// @include        http://weibo.com/*
// @include        https://weibo.com/*
// @include        http://www.weibo.com/*
// @include        https://www.weibo.com/*
// @grant GM_notification
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// ==/UserScript==

(function() {
    'use strict';
    // console.log($CONFIG)
    var uid = $CONFIG['oid']; // 用户ID
    var album_id = ''; // 相册ID
    var page = 1; // 当前页码
    var count = 30; // 每页显示数量(这里有点迷糊,有的微博设置100也有效,有的93、60等等,所以为了确保稳定还是按照官方设置为30)
    var total_page = null; // 总页数
    var type = 3; // 获取微博配图相册
    var viewer = null; // 图片预览
    // 插入预览图插件样式
    $("head").prepend('<link href=//cdn.bootcss.com/viewerjs/1.3.3/viewer.min.css rel=stylesheet>');
    // 插入样式
    $("body").prepend('<div id="laosa"><div class="ls_open">点我</div><div class="ls_main"><div class="ls_node_main"></div><div class="ls_page"><span class="ls_total_page"></span> <span class="ls_total"></span> <span class="ls_current_page"></span> <div class="ls_next_page W_btn_c btn_34px">下一页</div></div></div></div>');
    GM_addStyle('#laosa{width: 1000px;background: #fff;position: fixed;right: -1000px;bottom: 0;top: 0;z-index: 99999;}'+
                '.ls_open{position: absolute;left: -40px;width: 40px;height: 40px;background: #ff8140;color:#fff;top: 100px;line-height: 40px;text-align: center;font-size: 14px;border-radius: 5px 0 0 5px;}'+
               '.ls_main{overflow-y: auto;height: 100%;}'+
               '.ls_node_main{padding:20px;}'+
               '.ls_node{overflow: hidden;margin-bottom: 20px;}'+
               '.ls_h1{font-weight: bold;font-size: 14px;}'+
               '.ls_li{float: left;width: 150px;padding: 5px;height: 150px;overflow: hidden;}'+
               '.ls_li img{width: 100%;height: auto;}'+
               '.ls_page{padding:20px;}'+
               '.ls_disable,.ls_disable:hover{background:#ddd;}'+
               '.viewer-backdrop{background-color: rgba(0,0,0,.8);}')
    function open_photo(imgs){
        //
    }
    // 打开预览
    $(".ls_node_main").on("click",".ls_li",function(){
        var index = $(this).index();
        viewer = new Viewer($(this).parents('.ls_ul').get(0),{
            initialViewIndex: index,
            interval: 2000,
            loop: false,
            zIndex: 9999999,
            url: 'data-original'
        });
        viewer.show();
    })
    // 监听图片预览事件
    $(".ls_node_main").on('hidden', viewer,function () {
        // 销毁上一次打开对象
        viewer.destroy();
    });
    // 展开面板
    $(".ls_open").click(function(){
        if($("#laosa").css("right") == '0px'){
            $("body").css("overflow","visible");
            $("#laosa").animate({right:-1000});
        }else{
            $("body").css("overflow","hidden");
            $("#laosa").animate({right:0});
        }
    })
    get_weibo_album(uid);
    // 获取微博配图的相册ID(type=3)
    function get_weibo_album(uid){
        var all_album_url = 'http://photo.weibo.com/albums/get_all?uid='+uid+'&page=1&count=100'; // 根据用户id获取所有相册
        GM_xmlhttpRequest({
            method: 'GET',
            url: all_album_url,
            onload: response => {
                if (response.status == 200) {
                    var data = JSON.parse(response.responseText);
                    for (var i=0;i<data.data.album_list.length;i++){
                        // type=3为微博配图相册
                        if(data.data.album_list[i].type == 3){
                            album_id = data.data.album_list[i].album_id;
                        }
                        // console.log(data.data.album_list[i])
                    }
                    get_album();
                }
            }
        });
    }
    // 获取相册图片
    function get_album(page=1){
        $(".ls_next_page").text('加载中...').addClass("ls_disable");
        var album_url = 'http://photo.weibo.com/photos/get_all?uid='+uid+'&album_id='+album_id+'&count='+count+'&page='+page+'&type='+type; // 根据相册ID获取相册图片
        if(uid && album_id){
            GM_xmlhttpRequest({
                method: 'GET',
                url: album_url,
                onload: response => {
                    if (response.status == 200) {
                        $(".ls_next_page").text('下一页').removeClass("ls_disable");
                        var data = JSON.parse(response.responseText);
                        // 分页
                        insert_page(data.data.total);
                        //console.log(data.data.photo_list)
                        sort_data(data.data.photo_list,function(data){
                            insert_dom(data)
                        });
                    }
                }
            });
        }
    }
    // 插入分页数据
    function insert_page(total){
        // 计算总页数
        total_page = Math.ceil(total/count);
        $(".ls_total").html('图片总数:'+total);
        $(".ls_total_page").html('总页数:'+total_page);
        $(".ls_current_page").html('当前页数:'+page);
    }
    // 下一页
    $(".ls_page").on("click",".ls_next_page",function(){
        if($(this).hasClass("ls_disable"))
            return !1;
        if(page >= total_page)
            return !1;
        page+=1;
        get_album(page);
    })
    // 数据重新组装
    function sort_data(data,callback){
        //console.log(data)
        var new_data = [];
        var k = 0;
        var len = data.length;
        for (var i=0;i<len;i++){
            new_data[k] = new_data[k]?new_data[k]:{};
            new_data[k].title = new_data[k].title?new_data[k].title:'';
            new_data[k].data = new_data[k].data?new_data[k].data:[];
            var j = i-1;
            // 判断当前图片是否与上一张图片为同集
            if(i>0 && data[j].feed_id == data[i].feed_id){
                new_data[k-1].data.push(data[i].pic_host+'/large/'+data[i].pic_name)
            }else{
                new_data[k].title = data[i].caption_render;
                new_data[k].data = [data[i].pic_host+'/large/'+data[i].pic_name];
                k++;
            }
        }
        callback(new_data);
    }
    // 插入面板
    function insert_dom(data){
        //console.log(data)
        var html = '';
        for (var i=0;i<data.length;i++){
            html += '<div class="ls_node">';
            html += '<div class="ls_h1">'+data[i].title+'</div>';
            html += '<div class="ls_ul">';
            for (var j=0;j<data[i].data.length;j++){
                // 去除http:
                var url = data[i].data[j].split("http:").join("");
                // 使用小图预览
                var nurl = url.split("/large/").join("/thumb150/");// mw690
                html += '<div class="ls_li"><img alt="'+data[i].title+'" data-original="'+url+'" src="'+nurl+'"></div>';
            }
            html += '</div>';
            html += '</div>';
        }
        $(".ls_node_main").append(html);
    }
})();