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
ce1c947016a57daaa9adc793d695cb955ed840a4
parent
1f23b413c8ccbc52a9709b09e7928b001ed9e037
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-02-25 06:33
refactor: add getRules()

Diffstat

M src/bg.js 48 ++++++++++++++++++++++++------------------------

1 files changed, 24 insertions, 24 deletions


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

@@ -58,11 +58,13 @@ var setRule = function(context, hostname, type, rule) {
   58    58     });
   59    59 };
   60    60 
   61    -1 var restrictRules = function(rules, context) {
   62    -1     var restricted = {};
   63    -1     restricted['*'] = rules['*'] || {};
   64    -1     restricted[context] = rules[context] || {};
   65    -1     return restricted;
   -1    61 var getRules = function(context) {
   -1    62     return storageGet('rules').then(rules => {
   -1    63         var restricted = {};
   -1    64         restricted['*'] = rules['*'] || {};
   -1    65         restricted[context] = rules[context] || {};
   -1    66         return restricted;
   -1    67     });
   66    68 };
   67    69 
   68    70 var pushRequest = function(tabId, hostname, type) {
@@ -103,19 +105,20 @@ var getCurrentTab = function() {
  103   105 
  104   106 browser.runtime.onMessage.addListener((msg, sender) => {
  105   107     if (msg.type === 'get') {
  106    -1         return Promise.all([
  107    -1             getCurrentTab(),
  108    -1             storageGet('rules'),
  109    -1             storageGet('requests'),
  110    -1             storageGet('recording'),
  111    -1         ]).then(([tab, rules, requests, recording]) => {
   -1   108         return getCurrentTab().then(tab => {
  112   109             var context = getHostname(tab.url);
  113    -1             return {
  114    -1                 context: context,
  115    -1                 rules: restrictRules(rules, context),
  116    -1                 requests: requests[tab.id] || {},
  117    -1                 recording: recording,
  118    -1             };
   -1   110             return Promise.all([
   -1   111                 getRules(context),
   -1   112                 storageGet('requests'),
   -1   113                 storageGet('recording'),
   -1   114             ]).then(([rules, requests, recording]) => {
   -1   115                 return {
   -1   116                     context: context,
   -1   117                     rules: rules,
   -1   118                     requests: requests[tab.id] || {},
   -1   119                     recording: recording,
   -1   120                 };
   -1   121             });
  119   122         });
  120   123     } else if (msg.type === 'setRule') {
  121   124         return setRule(
@@ -123,9 +126,7 @@ browser.runtime.onMessage.addListener((msg, sender) => {
  123   126             msg.data.hostname,
  124   127             msg.data.type,
  125   128             msg.data.value,
  126    -1         ).then(() => storageGet('rules')).then(rules => {
  127    -1             return restrictRules(rules, msg.data.context);
  128    -1         });
   -1   129         ).then(() => getRules(msg.data.context));
  129   130     } else if (msg.type === 'securitypolicyviolation') {
  130   131         return pushRequest(sender.tab.id, 'inline', msg.data);
  131   132     } else if (msg.type === 'toggleRecording') {
@@ -155,7 +156,7 @@ browser.webRequest.onBeforeRequest.addListener(details => {
  155   156 
  156   157     return Promise.all([
  157   158         pushRequest(details.tabId, hostname, type),
  158    -1         storageGet('rules'),
   -1   159         getRules(context),
  159   160     ]).then(([_, rules]) => {
  160   161         if (!shared.shouldAllow(rules, context, hostname, type)) {
  161   162             if (details.type === 'sub_frame') {
@@ -169,12 +170,11 @@ browser.webRequest.onBeforeRequest.addListener(details => {
  169   170 }, {urls: ['<all_urls>']}, ['blocking']);
  170   171 
  171   172 browser.webRequest.onHeadersReceived.addListener(function(details) {
   -1   173     var context = getHostname(details.url);
  172   174     return Promise.all([
  173    -1         storageGet('rules'),
   -1   175         getRules(context),
  174   176         storageGet('recording'),
  175   177     ]).then(([rules, recording]) => {
  176    -1         var context = getHostname(details.url);
  177    -1 
  178   178         var csp = (type, value) => {
  179   179             var name = 'Content-Security-Policy';
  180   180             if (shared.shouldAllow(rules, context, 'inline', type)) {