1
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?

Chrome拡張機能で通信をブロックしたかったが、問題が多すぎた。

Posted at

どうも、g1を嫌うTNTSuperManです。
g1が嫌いすぎてそれらの通信をブロックする拡張機能を作ろうとしましたが、
問題が多すぎました。

環境

  • Windows 10 22H2
  • Microsoft Edge Version 135.0.3179.54 (Official build) (64-bit)

コード1. declarativeNetRequest

declarative_net_requestで静的に通信ブロックを制限する方法です。

manifest.json
{
    "manifest_version": 3,
    "name": "g_blocker",
    "version": "2.9.1",
    "description": "Block google's watching and networking",
    "permissions": [
        "declarativeNetRequest",
        "declarativeNetRequestWithHostAccess",
        "declarativeNetRequestFeedback"
    ],
    "host_permissions": [
        "<all_urls>"
    ],
    "declarative_net_request": {
        "rule_resources": [{
            "enabled": true,
            "id": "block",
            "path": "block.json"
        }]
    }
}
block.json
[{
    "id": 1,
    "priority": 1,
    "action": {"type": "block"},
    "condition": {
        "urlFilter": "https://google.com/*",
        "initiatorDomains": [
            "<all_urls>"
        ],
        "resourceTypes": [
            "main_frame",
            "sub_frame",
            "stylesheet",
            "script",
            "image",
            "font",
            "object",
            "xmlhttprequest",
            "ping",
            "csp_report",
            "media",
            "websocket",
            "webtransport",
            "webbundle"
        ]
    }
}]

問題

ブロックされませんでした。動作しませんでした。
しかし理由が全く分かりません。
ブラウザは全くエラー内容を表示しませんでした。
理由が分かった人は教えてください。

コード2. webRequestBlocking

JavaScriptで動的にリクエストを破棄できる機能ですが、
残念、webRequestBlockingはmanifest v2で営業終了しました。

コード3. 最終手段 onBeforeRequest+tabs.remove

最終手段です。onBeforeRequestでg1の匂いがしたらそのタブを強制的に消します。

manifest.json
{
    "manifest_version": 3,
    "name": "g_blocker",
    "version": "29.12.91",
    "description": "Block google's watching and networking",
    "permissions": [
        "webRequest",
        "tabs"
    ],
    "host_permissions": [
        "<all_urls>"
    ],
    "background": {
        "service_worker": "main.js"
    }
}
main.js
const g_reg = /^https?:\/\/(\w+\.)*[\w\d-]*(google|chrome|gstatic|youtube|ytimg)[\w\d-]*\.\w+\/?/;

chrome.webRequest.onBeforeRequest.addListener((d) => {
  if (g_reg.test(d.initiator ?? "") || g_reg.test(d.url)) {
    chrome.tabs.remove(d.tabId);
    return {
        redirectUrl: "data:application/octet-stream,",
        cancel: true
    }
  }
}, { urls: ["*://*/*"] });

問題: あまりにもg1の匂いを放つサイトが多すぎた

動作しましたが動作に問題大有りでした。

g1はGoogle analyticsというトラップや広告を大量のサイトに仕掛けて設置されています。このQiitaも例外ではありません。それら全てのサイトのタブを強制的に消すのはあまりにも強すぎました。
だからといってそれを容認したらもう直で監視されてしまいます。

言いたいこと

特定の通信をブロックする機能を作るのが困難になるように設計されていると感じます。広告ブロッカーが関わっているとの意見も。
なのでそういうものを作る際はお気を付けください。
firefoxだと大丈夫になってるかもしれません。

そして、拡張機能はデバッグが大変です。

以上です。

  1. google 2 3 4 5

1
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
1
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?