commit fda9a00d00476fee3543db6a27597a291b238484
Author: minerva-jupiter <ryouturn@gmail.com>
Date: Sat, 9 May 2026 21:48:37 +0900
feat: add auto-mute functionality for YouTube ads
- Implement content script to detect and mute YouTube video ads
- Add MutationObserver for real-time monitoring of ad-related classes
- Include periodic checks to handle SPA navigation and state changes
- Add manifest.json for extension configuration
Diffstat:
2 files changed, 59 insertions(+), 0 deletions(-)
diff --git a/content.js b/content.js
@@ -0,0 +1,46 @@
+// ミュート制御の本体
+const applyMuteLogic = () => {
+ const video = document.querySelector('video');
+ const player = document.querySelector('#movie_player');
+
+ if (!video || !player) return;
+
+ // 広告中かどうかを判定する複数のフラグ
+ // 1. YouTube公式のプレイヤーが持つクラス
+ // 2. 「広告詳細」ボタンの存在
+ // 3. 「スキップ」関連の要素の出現
+ const isAd = player.classList.contains('ad-showing') ||
+ player.classList.contains('ad-interrupting') ||
+ document.querySelector('.ytp-ad-player-overlay') !== null;
+
+ if (isAd) {
+ if (!video.muted) {
+ console.log("[Ad Muter] 広告を検知: ミュートします");
+ video.muted = true;
+ }
+ } else {
+ if (video.muted) {
+ // 広告が終わった瞬間にミュート解除
+ console.log("[Ad Muter] 広告終了: ミュートを解除します");
+ video.muted = false;
+ }
+ }
+};
+
+// 監視の設定
+// 属性の変化(クラス名の追加など)と、子要素(広告オーバーレイ)の追加を監視
+const observer = new MutationObserver((mutations) => {
+ applyMuteLogic();
+});
+
+// YouTubeのメインプレイヤー要素をターゲットにする(bodyより効率的)
+const target = document.body;
+observer.observe(target, {
+ childList: true,
+ subtree: true,
+ attributes: true,
+ attributeFilter: ['class']
+});
+
+// ページ遷移(SPA対策)時にも対応できるよう、定期的な生存確認も念のため入れる
+setInterval(applyMuteLogic, 500);
diff --git a/manifest.json b/manifest.json
@@ -0,0 +1,13 @@
+
+{
+ "manifest_version": 3,
+ "name": "Ad Muter",
+ "version": "1.0",
+ "permissions": ["storage"],
+ "content_scripts": [
+ {
+ "matches": ["https://www.youtube.com/*"],
+ "js": ["content.js"]
+ }
+ ]
+}