5
5

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拡張でのユーザーエージェント書き換え実装

Last updated at Posted at 2013-11-30

バックグラウンドスクリプトで

chrome.webRequest.onBeforeSendHeaders.addListener(
	function(info) {
		var headers = info.requestHeaders;
		headers.forEach(function(header, i) {
			if(header.name.toLowerCase() == 'user-agent'){ 
				header.value = "書き換えたいユーザーエージェント";
			}
		});
		return {requestHeaders: headers};
	},{
		urls: [
			<対象サイトたちのマッチパターン列挙>
		],
		types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest"]
	},[
		"blocking",
		"requestHeaders"
	]
);

あとはmanifestのpermissionsに

"webRequest",
"webRequestBlocking",
<対象サイトたちのマッチパターン列挙>

を追加する

注意事項としては、event pages(manifestのbackground.persistent = falseの場合)では使えない
event pagesは同様の機能を実装するのにdeclarativeWebRequestを使う必要があるが、これが現在まだβ段階でこの機能を組み込んだ拡張を作ると警告が出る

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?