xiMatrix

filter net requests according to source, destination and type  https://addons.mozilla.org/firefox/addon/ximatrix/
git clone https://git.ce9e.org/xiMatrix.git

commit
28599984dab45e2c748e41182cd8e80b15dc6f78
parent
4f35ff721229fa9af7eb5e556ccc5dcdd897138f
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-10-04 18:03
display number of blocked requests in badge

Diffstat

M README.md 1 -
M src/bg.js 53 +++++++++++++++++++++++++++++++++++++++--------------
M src/storage.js 2 ++

3 files changed, 41 insertions, 15 deletions


diff --git a/README.md b/README.md

@@ -29,7 +29,6 @@ Available for Firefox here: https://addons.mozilla.org/firefox/addon/ximatrix/
   29    29 			detail.
   30    30 	-	the popup is not updated while it is open. You have to close and open it
   31    31 		again to refresh the data.
   32    -1 	-	the icon does not show the amount of blocked requests
   33    32 	-	blocked images are not replaced by a placeholder
   34    33 
   35    34 ## Known issues

diff --git a/src/bg.js b/src/bg.js

@@ -64,7 +64,18 @@ var getRules = async function(context) {
   64    64     return restricted;
   65    65 };
   66    66 
   67    -1 var pushRequest = async function(tabId, hostname, type) {
   -1    67 var increaseTotals = async function(tabId) {
   -1    68     var value = 0;
   -1    69     await storage.change('totals', totals => {
   -1    70         value = (totals[tabId] || 0) + 1;
   -1    71         totals[tabId] = value;
   -1    72         return totals;
   -1    73     });
   -1    74     await browser.action.setBadgeBackgroundColor({color: '#6b6b6b', tabId: tabId});
   -1    75     await browser.action.setBadgeText({text: '' + value, tabId: tabId});
   -1    76 };
   -1    77 
   -1    78 var pushRequest = async function(tabId, hostname, type, allowed) {
   68    79     await storage.change('requests', requests => {
   69    80         if (!requests[tabId]) {
   70    81             requests[tabId] = {};
@@ -78,15 +89,26 @@ var pushRequest = async function(tabId, hostname, type) {
   78    89         requests[tabId][hostname][type] += 1;
   79    90         return requests;
   80    91     });
   -1    92     if (!allowed) {
   -1    93         await increaseTotals(tabId);
   -1    94     }
   81    95 };
   82    96 
   83    97 var clearRequests = async function(tabId) {
   84    -1     await storage.change('requests', requests => {
   85    -1         if (requests[tabId]) {
   86    -1             delete requests[tabId];
   87    -1         }
   88    -1         return requests;
   89    -1     });
   -1    98     await Promise.all([
   -1    99         storage.change('requests', requests => {
   -1   100             if (requests[tabId]) {
   -1   101                 delete requests[tabId];
   -1   102             }
   -1   103             return requests;
   -1   104         }),
   -1   105         storage.change('totals', totals => {
   -1   106             if (totals[tabId]) {
   -1   107                 delete totals[tabId];
   -1   108             }
   -1   109             return totals;
   -1   110         }),
   -1   111     ]);
   90   112 };
   91   113 
   92   114 var getCurrentTab = async function() {
@@ -142,7 +164,11 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {
  142   164             return rules;
  143   165         });
  144   166     } else if (msg.type === 'securitypolicyviolation') {
  145    -1         await pushRequest(sender.tab.id, 'inline', msg.data);
   -1   167         var patterns = await getPatterns();
   -1   168         var context = getHostname(sender.tab.url, patterns);
   -1   169         var rules = await getRules(context);
   -1   170         var allowed = shared.shouldAllow(rules, context, 'inline', msg.data);
   -1   171         await pushRequest(sender.tab.id, 'inline', msg.data, allowed);
  146   172     }
  147   173 });
  148   174 
@@ -163,20 +189,19 @@ browser.webRequest.onBeforeSendHeaders.addListener(async details => {
  163   189     var hostname = getHostname(details.url, patterns);
  164   190     var type = shared.TYPE_MAP[details.type] || 'other';
  165   191 
  166    -1     var promises = [
  167    -1         getRules(context),
  168    -1     ];
   -1   192     var rules = await getRules(context);
  169   193 
  170   194     if (details.type !== 'main_frame') {
  171    -1         promises.push(pushRequest(details.tabId, hostname, type));
   -1   195         var allowed = shared.shouldAllow(rules, context, hostname, type);
   -1   196         await pushRequest(details.tabId, hostname, type, allowed);
  172   197     }
  173   198 
  174   199     var isCookie = h => h.name.toLowerCase() === 'cookie';
  175   200     if (details.requestHeaders.some(isCookie)) {
  176    -1         promises.push(pushRequest(details.tabId, hostname, 'cookie'));
   -1   201         var allowed = shared.shouldAllow(rules, context, hostname, 'cookie');
   -1   202         await pushRequest(details.tabId, hostname, 'cookie', allowed);
  177   203     }
  178   204 
  179    -1     var [rules, ..._rest] = await Promise.all(promises);
  180   205     if (
  181   206         details.type !== 'main_frame'
  182   207         && !shared.shouldAllow(rules, context, hostname, type)

diff --git a/src/storage.js b/src/storage.js

@@ -4,11 +4,13 @@ var STORAGE_DEFAULTS = {
    4     4     'rules': {},
    5     5     'savedRules': {},
    6     6     'requests': {},
   -1     7     'totals': {},
    7     8 };
    8     9 var STORAGE_AREAS = {
    9    10     'rules': browser.storage.local,
   10    11     'savedRules': browser.storage.local,
   11    12     'requests': browser.storage.session,
   -1    13     'totals': browser.storage.session,
   12    14 };
   13    15 var lock = Promise.resolve();
   14    16 var cache = {};