4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ChromeでPDFを表示する(ダウンロードウィンドウを表示しない方法)

Posted at

PDFはChromeですぐに見たい

PDFのビューワはChromeに設定しています。
Ad●be ReaderよりChromeでPDFを見た方が軽い&早いと個人的には思っています。

WebでPDFを見ようとするとダウンロードウィンドウが表示されることがあります。
もぅマヂ無理。 リスカしょ・・・

レスポンスヘッダを書き換える

Chromeのオレオレ拡張で、ダウンロードを強制させるレスポンスヘッダを書き換えます。
PDFがそのままChromeで表示されて、うれしい!楽しい!大好き!抱いて!となります。

background.js
handler = function (details) {
	
	// Content-Disposition : attachmentを書き換える
	for (var i = 0, l = details.responseHeaders.length; i < l; ++i) {
		var header = details.responseHeaders[i];
		if (header.name === 'Content-Disposition' && header.value === 'attachment') {
			header.value = 'inline';
			break;
		}
	}
	
	return { responseHeaders: details.responseHeaders };
};

chrome.webRequest.onHeadersReceived.addListener(handler, {
	urls: [ "<all_urls>" ], 
	types: ["main_frame", "sub_frame"]
},  [ "responseHeaders", "blocking" ]);

注意

上記をそのままコピペするとPDF以外のContent-Dispositionもinlineになってしまいます。
使用の際はご自分にあわせた制限をおかけください。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?