From a78289ae9135678cf996d966704b63dd8aab89a5 Mon Sep 17 00:00:00 2001 From: Guus van der Meer Date: Mon, 25 Dec 2023 02:30:04 +0100 Subject: [PATCH] Workflow for manifest v3 block rules (#10555) * Created a workflow that creates a PR for the block-rules for manifest V3 --- .github/workflows/block_rules.yml | 44 ++++++++++++++++++++ .github/workflows/generateBlockRules.js | 54 +++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 .github/workflows/block_rules.yml create mode 100644 .github/workflows/generateBlockRules.js diff --git a/.github/workflows/block_rules.yml b/.github/workflows/block_rules.yml new file mode 100644 index 00000000..e0a8581f --- /dev/null +++ b/.github/workflows/block_rules.yml @@ -0,0 +1,44 @@ +name: Generate manifest V3 block rules + +on: + push: + branches: + - master + +jobs: + generate-block-rules: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: 14 + + - name: Install dependencies + run: npm install + + - name: Generate block rules + run: node .github\workflows\generateBlockRules.js > src\rules.json + + - name: Run Prettier + run: npx prettier --write .\src\rules.json + + - name: Check for changes + id: git-diff + run: echo ::set-output name=files-changed::$(git diff --name-only) + + - name: Create pull request + uses: peter-evans/create-pull-request@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: Update block rules for Manifest V3 + branch: update-block-rules + title: Update manifest V3 block rules + body: | + This pull request updates the block rules based on the latest changes. + Files changed: + ${{ steps.git-diff.outputs.files-changed }} diff --git a/.github/workflows/generateBlockRules.js b/.github/workflows/generateBlockRules.js new file mode 100644 index 00000000..f7dec926 --- /dev/null +++ b/.github/workflows/generateBlockRules.js @@ -0,0 +1,54 @@ +// This is used to generate the block rules for Manifest V3 from the block rules in Manifest V2. + +import { blockUrls } from "../../src/data/rules.js"; + +function generateDeclarativeNetRules() { + const result = []; + let lastId = 1; + + const addRule = (blockRule) => { + const newRule = { + id: lastId++, + priority: 1, + action: { type: "block" }, + condition: { + urlFilter: blockRule.r, + resourceTypes: ["script", "stylesheet", "xmlhttprequest", "image"], + }, + }; + + if (blockRule.e) { + newRule.condition.excludedInitiatorDomains = blockRule.e.slice(); + } + + result.push(newRule); + }; + + for (const blockRule of blockUrls.common) { + addRule(blockRule); + } + + for (const blockRules of Object.values(blockUrls.common_groups)) { + for (const blockRule of blockRules) { + addRule(blockRule); + } + } + + for (const [domain, url] of Object.entries(blockUrls.specific)) { + const newRule = { + id: lastId++, + priority: 1, + action: { type: "block" }, + condition: { + urlFilter: url[0], + resourceTypes: ["script", "stylesheet", "xmlhttprequest", "image"], + initiatorDomains: [domain], + }, + }; + result.push(newRule); + } + + console.log(JSON.stringify(result, null, "\t")); +} + +generateDeclarativeNetRules();