如何停止 chrome/firefox 下载 gif?

msh*_*yem 8 animated-gif browser-addons google-chrome-extensions firefox-extensions

当我访问任何包含一些动画gif的页面(Google+、9gag 等)时,所有这些似乎都开始加载。我的带宽有限,除非我真的想查看,否则我不希望下载它们。我见过一些插件只是停止动画,但无论如何还是下载了图像本身。9gag.com 最初有这个功能,它只显示一个静态图像(如果它是一个动画 gif),并且只有在我点击它后才开始下载(他们现在已经删除了它)。

那么,有没有可以停止下载gif的插件?或者,我需要写一个扩展吗?有什么建议吗?

msh*_*yem 1

我还没有任何合适的扩展/插件。我尝试在 Chrome 中将以下用户脚本与TamperMonkey一起使用。它工作得很好。阻止 9gag.com 站点中的所有 gif(包括 ajax gif)。由于某种原因,google+ 中的 ajax gif 不会被阻止(对此进行调查)。非常感谢Synetec的帮助、努力和代码。这是用户脚本(大部分脚本是从Synetec 的用户脚本复制的):

// ==UserScript==
// @name       gifBlock
// @namespace  http://i.have.no.homepage/
// @version    0.1
// @description  Stops downloading gif images (including ajax gifs) in 9gag.com (or any page if you just fix the @match rule)
// @match      http://*.9gag.com
// @copyright  2012+, Nobody
// ==/UserScript==

function tamperMonkeyWrap()
{   
    function log(m)
    {
        console.log(m);
    }
    function jQWrap($)
    {
        log("Extension execution begins...");

        function blockGifs()
        {        
            $('img').each(function() {
                var $img = $(this),
                    src = $img.attr('src'),
                    w = $img.width(),
                    h = $img.height(),
                    cursor = $img.css('cursor'),
                    parts = src.split('.'),
                    ext = parts[parts.length-1];

                if ($.trim(ext.toLowerCase()) != "gif")
                    return;            

                $img.attr('data-imgurl', src);
                $img.data('cursor', cursor);
                $img.css('cursor', 'pointer');
                $img.addClass('gif-blocked');                
                h = h > 100? h : 100;
                $img.attr('src', '//ipsumimage.appspot.com/'+w+'x'+h+'?l=Gif (Click)');
            }); 
        }

        function interceptAjax () {
            $('body').ajaxComplete(
                function (event, requestData)
                {
                    log("Blocking GIF [Ajax] ...");                
                    blockGifs();
                }
            );
        }

        $(document).ready(function() {
            log("Blocking GIF [Ready]....");
            blockGifs();
            interceptAjax();        
            $(document).on('click', 'img.gif-blocked', function(ev) {            
                var $img = $(this),
                    url = $img.attr('data-imgurl'),
                    cursor = $img.data('cursor');

                $img.attr('src', url);
                $img.css('cursor', cursor);
                $img.removeClass('gif-blocked');
                ev.preventDefault();
                return false;
            });  
        });

        log("Document is not ready yet. trying block just in case it takes time to be _ready_ (google+).");
        blockGifs();
    }

    if (window.jQuery == undefined)
    {
        log("Loading jQuery...");
        var scriptTag = document.createElement('script');
        scriptTag.src = "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js";
        scriptTag.onload = function(){
            log("jQuery loaded.");
            window.jQuery = jQuery; 
            jQWrap(jQuery);
        };
        document.getElementsByTagName('head')[0].appendChild(scriptTag);
    }
    else
    {
        log("jQuery already included in the page");
        jQWrap(window.jQuery);
    }   
}

var scriptTag = document.createElement('script');
scriptTag.text = '(' + tamperMonkeyWrap.toString() + ')();';
document.getElementsByTagName('head')[0].appendChild(scriptTag);
Run Code Online (Sandbox Code Playgroud)

现在:

  1. 安装 TamperMonkey
  2. 转到仪表板
  3. 单击“新脚本”
  4. 粘贴上面的代码
  5. 保存看看是否有效。(现在仅适用于 9gag.com。但是您可以更改@match指令以匹配您想要的任何网站。使用@match http://*/*适用于所有网站 (http)。更改https为适用于任何安全的 http 网站,例如 google+)