Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I edit Chrome MIME type mappings?

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?

like image 580
Ohgyun Ahn Avatar asked Nov 30 '11 09:11

Ohgyun Ahn


People also ask

How does browser determine MIME type?

A browser usually identifies a resource's MIME type by observing the Content-Type response header in an HTTP response. Sometimes, developers set values for Content-Type headers that are not appropriate for the response's content.

How do I view Content-Type in Chrome?

Once open, select the Network tab, select the name of the resource on the lefthand side, then hit the Headers tab. Under Response Headers find Content-Type which is your mime type.


2 Answers

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:

  • http://trac.edgewall.org/attachment/ticket/10804/changeset_show_files_3.png
  • http://freetz.org/attachment/ticket/1908/arris1.png

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.

like image 83
kriegaex Avatar answered Oct 04 '22 05:10

kriegaex


It seems that you can't edit your MIME type mappings, see "View As MIME Type" in Chrome or Firefox

This answer https://stackoverflow.com/a/8724758/962091 is of particular interest. The author has made and open sourced a Chrome extension to open files as different MIME types via an added menu item on right-click, though it does not provide a global handling mechanism.

EDIT:

The extension by default only has the options to open as text/html and text/plain, but you can add any extensions you like, including image/png, in the extensions settings. Here's a test image.

This bug was moved to Untriaged on August 13, 2012 in the Chromium bug tracker, so hopefully this will be fixed soon in Chrome as well.

like image 35
zackdever Avatar answered Oct 04 '22 05:10

zackdever