7

I am using Google Chrome, and I want to open .c and .cpp files as text in the browser itself. Is there any way to do that?

studiohack
  • 13,468
  • 19
  • 88
  • 118
Lazer
  • 17,227
  • 43
  • 116
  • 141

6 Answers6

5

After a bit of investigation i found that at least for local files you can override this by defining your preferred mime types in ~/.local/share/mime/globs2 as follows:

    > cat ~/.local/share/mime/globs2
    10:text/plain:*.c
    10:text/plain:*.cc
    10:text/plain:*.c++
    10:text/plain:*.cpp
    10:text/plain:*.h

The first field is the priority (smaller number means more important), then follows the mime type, then the pattern that you are matching. The default rules (/usr/local/share/ ...) have a priority of 50 for .c, .h and so on.

For remotely-served files, the browser relies on the MIME Type supplied by the server and the trick above does not work.

Looking at the Chromium sources

chromium-courgette-redacted-18.0.1025.162/net/base/mime_util.cc

it seems that a partial fix can be achieved by arring the list of types we want to display to the array

 static const char* const supported_non_image_types[] = {
    ...
+       "text/x-csrc",
+       "text/x-chdr",
    ...
 }

Maybe we can have some optional patch to the FreeBSD port, although i'd rather find a way to override the server-supplied mime type in a way that does not require rebuilding Chrome.

Anyways, at least for local browsing, this seems a significant improvement.

Luigi Rizzo
  • 51
  • 1
  • 1
  • Unfortunately with changes to the mime database, this will no longer work. I've edited the comment to include the updated solution – Aster Feb 14 '21 at 16:15
2

How are you getting the URL of the .c or .cpp file?

If it's via a link on another page the Chrome should just open them as plain text files. Well it did for me when I just tried it with a file from my hard drive.

If you want to use Chrome as the default viewer for .c or .cpp files on your hard drive, then we'll need to know which operating you are using.

ChrisF
  • 41,278
  • 17
  • 101
  • 154
  • I am using Windows Vista. urls - they are attachments to my mails... – Lazer May 02 '10 at 21:31
  • Also, the default viewer is set to Notepad++, which is okay. What happens when I click on those files is that they get downloaded and then I have to open them. Actually I do not really need to download them. I just need to have a quick view, so it would be really helpful if chrome can open them as text files for me. – Lazer May 02 '10 at 21:33
  • @eSKay - which e-mail client are you using? – ChrisF May 02 '10 at 21:36
  • @ChrisF: internet based: gmail, which is already opened in one tab of chrome. I just want the attached file to open in a new tab as text. – Lazer May 02 '10 at 21:40
  • @eSKay - I don't know gmail, sorry. – ChrisF May 02 '10 at 21:44
  • 1
    @ChrisF: you dont have to know gmail for this. Is there a way to open `.c` files in chrome? – Lazer May 02 '10 at 21:49
  • @eSKay - I don't know how gmail handles links normally, whether it uses it's own list (in which case there'll be an option to set it) or whether it uses Window's file associations. If it's the latter then just change the file association in Vista. – ChrisF May 02 '10 at 22:10
  • @eSKay It all depends on how they send the headers for the file. If they send it as text Chrome should just open it. If they send it as something else then it is up in the air. – Unfundednut May 02 '10 at 22:38
  • 1
    *> Well it did for me when I just tried it with a file from my hard drive.*   Yes, local source files open as text, but remote source files initiate a download.     *> It all depends on how they send the headers for the file.*   @MrStatic, that’s what I thought, but I just tested by putting a line in `.htaccess` to treat `.cpp` files as `text/plain`, and they *still* download instead of open as text. – Synetech Mar 14 '12 at 04:10
2

This method is kind of long and tedious and it still involves downloading the source files, but I can't come up with better right now.

If you click on a link it will automatically download, then if you open the downloads tab (clicking on show all downloads or Ctrl + J) and click Copy link address in the right click menu of the file you downloaded and paste that link in the address bar it will display the file as plain text, but this only works for local files afaik.

Or you can set your file associations for c files to chrome and when you click on a downloaded file it will open up in the browser itself.

sekmet64
  • 165
  • 2
  • 9
0

Google Chrome has fixed it, at least it's ok with my version (61.0.3163.91 64 bits).

But Firefox did not. So I found an easy and tricky manner for our foxy-favorite, and I described it here, in this similar question: Getting chrome to open “text” files in a tab.

Google Chrome

Now its possible to open source code directly from browser. I tested on version 61.0.3163.91 64 bits.

Firefox

Here it isn't yet. To avoid this, do the following:

  1. Right-click on .c or other source code file;
  2. Copy link option
  3. Press CRTL-T to open new tab;
  4. Type "view-source:"
  5. Paste the link copied before.

Done! Firefox will show the source code of the file, in this case, the file itself.

0

Not sure if anyone still cares about this, but I found the "Force Media-Type" Chrome extension via this post. I right click the link to the file, and choose "Open as Media Type > Text/Plain". These files used to download when I clicked them, now I can view them in a new tab right inside of Chrome! No more little text files littered all over my downloads folder, or launching other apps. I use it to open .txt files, but I could see it working just as well for .c or .cpp files also.

0

I'm afraid this isn't possible. As the fies are attachments in a Gmail message, there would have to a a viewer in the Gmail web application to view them. It doesn't have a viewer so the only way to see the file is to download it.

Why?

  • The files and the e-mail are on the Gmail servers. When you view the e-mail in the browser the server creates a web page with the content of the e-mail. Your browser displays the content of the web page for you. All that's been transferred to your PC is what you see on the screen.
  • The web page includes links to the attachments. These just point to the attachments so that clicking on them will start a download. The actual attachment file is stored on the Gmail server.
  • Gmail provides viewers for some types of files. When you click on the View link, the Gmail server builds a web page with the content of the attachment. For other types of files (most of the file types in the world) Gmail doesn't do this. It could, but the functionality isn't there right now. So, the only option left to you is to download the file from the Gmail server to your PC so that you can view it.
Wayne Johnston
  • 3,538
  • 19
  • 15
  • Did the original question say something about Gmail? In any case, this seems like an [absence-of-evidence/evidence-of-absense](https://en.wikipedia.org/wiki/Evidence_of_absence)-type answer. Unless there's something fundamentally different between Chrome and Firefox (or something has changed since your 2010 answer), can one exclude that what OP is asking is possible, considering that Firefox's [Open in Browser extension](https://addons.mozilla.org/en-US/firefox/addon/open-in-browser/) does it? – Robert Fleming Mar 15 '17 at 04:35