0
0

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 3 years have passed since last update.

Googleの検索結果をURLでフィルターするChrome(またはEdge)の拡張機能を作った

Last updated at Posted at 2021-11-06

広告収入が主目的のページとか対話形式の記事とか、いちいち読んでられない。

この拡張機能は、下記のソースに検索結果より削除したい項目のURLを書き込んで使う。

content_script.js
'use strict';
(function () {

function main() {
	//URLの比較は前方一致で行う
	const urlBlackList = Object.freeze([
		'https://techacademy.jp/',
		'https://sasasorato.com/',
	]);

	const rso = document.querySelector('div#rso');
	if(rso == null) {
		return;
	}

	for(const child of rso.children) {
		if ( child.tagName === 'DIV' ) {
			const anchors = child.getElementsByTagName('A');
			for (const a of anchors) {
				if (a.href != null && urlBlackList.some(url => a.href.startsWith(url))) {
					if(child.classList.contains('g') || child.querySelector(':scope > div.g')) {
						//とりあえず透明にした。非表示にしたければ child.style.display = 'none'; を有効にする
						child.style.opacity = '0.25';
						//child.style.display = 'none';
						continue;
					}
				}
			}
		}
	}
}

main();
})();
manifest.json
{
	"name": "Google Search Filter",
	"description": "Google Search Filter",
	"version": "1.0",
	"manifest_version": 3,
	"content_scripts": [
		{
			"matches": [
				"https://www.google.co.jp/search?*",
				"https://www.google.com/search?*"
			],
			"js": ["content_script.js"]
		}
	]
}

インストール方法

1. ファイルの保存
上のふたつのコードをコピーして自分のローカル端末に保存する。
ファイル名は manifest.json, content_script.js にして、ファイルのエンコードはUTF-8で保存する(BOM付き、なし、どちらも可)。エンコードは、だいたいのどのエディタも「名前を付けて保存」で指定できる。
ふたつのファイルは同じフォルダに格納する。フォルダ名は任意でOK。

2. ブラウザにインストールする

Chromeなら
(1) 画面右上の隅のジグソーパズルのピースをクリックして「拡張機能の管理」を開く。
  (または、右上の隅の [ ⁝ ]を押し「その他のツール」→「拡張機能」を開く)
(2) 拡張機能の設定画面が開かれたら、右上の「デベロッパーモード」をオンにする。
(3)「パッケージ化されていない拡張機能を読み込む」をクリックする。
(4) スクリプトが格納されたフォルダーを選択する。

Microsoft Edgeなら
(1) 画面右上の隅の[・・・]を押し「拡張機能」を開く。
(2) 拡張機能の設定画面が開かれたら、左下の「開発者モード」をオンにする。
(3)「展開して読み込み」をクリックする。
(4) スクリプトが格納されたフォルダーを選択する。

Firefoxなら
下記の手順で一時的にインストールできる。
パッケージ化していないので永続的なインストールは不可。
(1) 画面右上の隅の[≡]を押し「アドオンとテーマ」を開く。
(2) アドオンマネージャ―が開かれたら、歯車ボタンを押し「アドオンをデバッグ」をクリックする。
(3)「一時的なアドオンを読み込む...」をクリックする。
(4) ファイルダイアログで manifest.json を選択する。
※「Unsupported manifest version: 3」のエラーが出るなら、上の manifest.json の "manifest_version" を 2 に書き換える。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?