TweetDeck Image Assistant

Download/Share Images Faster

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         TweetDeck Image Assistant
// @namespace    http://ejew.in/
// @version      0.1
// @description  Download/Share Images Faster
// @author       EntranceJew
// @match        https://tweetdeck.twitter.com/*
// @require      https://cdn.rawgit.com/eligrey/FileSaver.js/5ed507ef8aa53d8ecfea96d96bc7214cd2476fd2/FileSaver.min.js
// @noframes
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var tool_icon = '<li class="tweet-action-item pull-left margin-r--13 margin-l--1">';
    tool_icon += '<a class="js-show-tip tweet-action position-rel" href="#" rel="spice" title="" data-original-title="Spice It Up">';
    tool_icon += '<i class="icon icon-home icon-home-toggle txt-center"></i> <span class="is-vishidden"> Spice </span>';
    tool_icon += '</a> </li>';

    var mime_db = {
        jpeg: "image/jpeg",
        jpg: "image/jpeg",
        gif: "image/gif",
        webp: "image/webp",
        undefined: "text/plain"
    };

    function detect_mime(url){
        return mime_db[ /(?:\.([^.]+))?$/.exec(url)[1] ];
    }

    function get_img_data( url, on_load ) {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url);
        xhr.responseType = "blob";
        xhr.onload = on_load;
        xhr.send();
    }

    function download_now( url ){
        get_img_data( url, function( e ){
            var img_name = url.substring( url.lastIndexOf('/')+1 );
            var the_blob = new Blob([this.response], {type: detect_mime(url)});
            saveAs( the_blob, img_name.replace(/:orig$/, "") );
        });
    }

    function nice_url( url, replacement ){
        replacement = replacement || ":orig";
        var bg = url;
        bg = bg.replace('url(','').replace(')','').replace(/\"/gi, "");
        bg = bg.replace(/:thumb$/, "");
        bg = bg.replace(/:small$/, "");
        bg = bg.replace(/:medium$/, "");
        bg = bg.replace(/:large$/, "");
        return bg;
    }

    setInterval(function(){
        $('.stream-item:not([data-ejew])').each(function(){
            var grand_dad = $( this );

            /*
            // for appending to the dropdown menu if we wanted that
            var tool_bar = grand_dad.find('.js-dropdown-content > ul');
            tool_bar.prepend('<li class="is-selectable"><a href="#" data-action="ejew">Spice it up</a></li>');
            */

            // find all the images and store their links in data
            var images = [];
            grand_dad.find('.js-media-image-link, .js-media .media-image').each( function(i, el){
                images.push( nice_url( $( el ).css('background-image'), ":orig" ) );
            });
            grand_dad.data('ejew-imgs', images);

            // make an instance of the toolbar button
            var new_tool = $( tool_icon );
            new_tool.on('click', function(){
                console.log("yes!");
                var imgs = grand_dad.data('ejew-imgs');
                console.log( 'loading', imgs );
                for( var i = 0; i < imgs.length; i++ ){
                    download_now( imgs[i] );
                }
            });
            // attach
            grand_dad.find('ul.tweet-actions > li:nth-last-child(2)').before( new_tool );

            // prevent loading up this element again
            grand_dad.attr('data-ejew', 'in');
        });

        // make it so that you can copy image source from previews
        $('img.media-img:not([data-ejew])').each(function(){
            $( this ).attr('src', nice_url( $( this ).attr('src') ) );
            $( this ).attr('data-ejew', 'in');
        });
    }, 300);
})();