我如何编辑Chrome MIME类型映射? [英] How can I edit Chrome MIME type mappings?

查看:206
本文介绍了我如何编辑Chrome MIME类型映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在新标签中打开了一个 image / x-png 类型的图片,但Chrome只是下载了它。
我搜索了它,发现Chrome不会将 image / x-png 解释为图像文件。

所以,我想要编辑我的Chrome的MIME类型映射。是否有可能?

解决方案

太糟糕了,您还没有回答我的评论,所以我忘记了您的问题并失去了奖励。我想我的答案对你来说更容易一些,因为你不需要点击任何上下文菜单来直接在浏览器中看到PNG:



Chrome扩展程序可以配置重定向器以替换HTTP响应标头。我创建了一个规则来替换任何以.png结尾的URL的Content-Type标题:



将这个JSON代码粘贴到文本编辑器中,另存为 png_rule .json ,然后在重定向器设置中转到规则管理器,单击文件以打开im- / export选项并导入规则版本1: / strong>


$ b

  [{name:x-png  - > png, 匹配:{ STR: \\.png $, 类型:0, 作案:假}, 子:{ STR: 内容类型, 类型 :4,modi:true,modg:true},repl:{str:image / png,decode:false},enabled:1}] 

从现在起,所有的PNG文件应该都可以。

更新:用作者或重定向器建议的更严格的正则表达式匹配取代了简单模式匹配。






更新2:有一些系统,例如 Trac ,可为PNG附件以.png结尾,但实际上只是将PNG作为页面的一部分嵌入的HTML页面。在这种情况下,我们需要将主机列入黑名单,否则HTML页面的内容类型将被设置为 image / png ,即使它应该保留为 text / html 。由于重定向器的限制,只能为匹配的网址和部分替换覆盖标头,例如只有 png 而不是 x-png 是不可能的,所以我们需要在正则表达式匹配中明确排除某些主机。例如

  ^ https?://(?!([^ /] + \。)?(?:trac\\ \\.edgewall\.org | freetz\.org)/)。* \.png 

将排除包含主机名 trac.edgewall.org freetz.org 的网址。要导入到 Redirector 中的完整JSON是规则版本2:
$ b

  [{name:x-png  - > png,match:{str:^ https?://(?!([^ /] + \\\)(?: trac\\.edgewall\\.org | freetz\\.org)/)* \\.png $, 类型:?0, MODI:真}, 分:{ STR: 内容类型, 类型:4 MODI:真 modg:真}, REPL:{ STR: image / png,decode:false},enabled:1}] 

测试网址失败(即显示空的图像而不是HTML页面)与规则的以前版本没有列入黑名单,现在与包括黑名单在内的新规则一起运行:





这是很容易添加其他URL,如果你知道一个正则表达式或只是玩耍。顺便说一句:对于Trac URL,一个好的选择是使用公共部分/附件/票据/列入黑名单。通过这种方式,您可以将所有Trac安装在全球进行黑名单,而不是固定的主机列表所以这是规则第3版: $

[{name :x-png - > png,match:{str:^ https https://(?!([^ /] +)?/ attachment / ticket /)。* \\ .PNG $, 类型:0, MODI:真}, 分:{ STR: 内容类型, 类型:4 MODI:真 modg:真},repl:{str:image / png,decode:false},enabled:1}]






更新3 :Chrome商店中不再提供重定向器,但可以从旧版< a href =https://code.google.com/p/chrome-redirector/downloads/list =nofollow noreferrer>重定向器主页。


I opened a image/x-png type image in new tab, but Chrome just downloaded it. I googled it and found that Chrome doesn't interpret image/x-png as image file.

So, I want to edit my Chrome's MIME type mappings. Is it possible?

解决方案

Too bad you have not answered my comment, so I forgot about your question and lost the bounty. I guess my answer is even a little easier to use for you because you do not need to click any context menus in order to see the PNGs directly in the browser:

Chrome extension Redirector can be configured to replace HTTP response headers. I created a rule replacing the Content-Type header for any URL ending with ".png":

Paste this JSON code into a text editor, save as png_rule.json and then in Redirector settings go to Rules Manager, click Files to open im-/export options and import the rule version 1:

[{"name":"x-png -> png","match":{"str":"\\.png$","type":0,"modi":false},"sub":{"str":"Content-Type","type":4,"modi":true,"modg":true},"repl":{"str":"image/png","decode":false},"enabled":1}]

From now on all your PNG files should be fine.

Update: replaced the simple pattern match by a stricter regex match as suggested by the author or Redirector.


Update 2: There are systems like Trac which produce URLs for PNG attachments ending in ".png", but really being HTML pages which only embed the PNG as part of the page. In this case we need to blacklist the hosts because otherwise the HTML page's content type would be set to image/png even though it should remain text/html. Because of Redirector's limitation that headers can just be overwritten for matched URLs and partial replacement, e.g. only png instead of x-png, is impossible, we need to use a rather ugly approach of explicitly excluding certain hosts in the regex match. E.g.

^https?://(?!([^/]+\.)?(?:trac\.edgewall\.org|freetz\.org)/).*\.png$

would exclude URLs containing either host name trac.edgewall.org or freetz.org. The full JSON to be imported into Redirector is rule version 2:

[{"name":"x-png -> png","match":{"str":"^https?://(?!([^/]+\\.)?(?:trac\\.edgewall\\.org|freetz\\.org)/).*\\.png$","type":0,"modi":true},"sub":{"str":"Content-Type","type":4,"modi":true,"modg":true},"repl":{"str":"image/png","decode":false},"enabled":1}]

Two test URLs failing (i.e. showing empty "images" instead of HTML pages) with the previous version of the rule without blacklisting and now functioning with the new rule including blacklisting are:

It is easy to add other URLs if you know a little regex or just play around.

By the way: For Trac URLs a good alternative would be to use the common part "/attachment/ticket/" for blacklisting. This way you could blacklist all Trac installations worldwide instead of a fixed hosts list. So this is rule version 3:

[{"name":"x-png -> png","match":{"str":"^https?://(?!([^/]+)?/attachment/ticket/).*\\.png$","type":0,"modi":true},"sub":{"str":"Content-Type","type":4,"modi":true,"modg":true},"repl":{"str":"image/png","decode":false},"enabled":1}]


Update 3: Redirector is no longer available in Chrome store, but historic versions can be downloaded from the old Redirector home page.

这篇关于我如何编辑Chrome MIME类型映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆