- commit
- e53547dfce3d5b7294514e039e58b39efb0dac18
- parent
- 5d3a24f58c1223ca30d073fd1a9c91c67ed170ec
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2022-07-30 18:25
shared namespace
Diffstat
| M | bg.js | 17 | +++++++++++------ |
| M | popup.js | 8 | ++++---- |
| M | shared.js | 13 | +++++-------- |
3 files changed, 20 insertions, 18 deletions
diff --git a/bg.js b/bg.js
@@ -1,8 +1,13 @@1 -1 /* global browser */-1 1 /* global browser shared */ 2 2 3 3 var rules = {}; 4 4 var requests = {}; 5 5 -1 6 var getHostname = function(url) { -1 7 var u = new URL(url); -1 8 return u.hostname; -1 9 }; -1 10 6 11 var setRule = function(context, hostname, type, rule) { 7 12 if (hostname === 'first-party') { 8 13 context = '*'; @@ -95,24 +100,24 @@ browser.webRequest.onBeforeRequest.addListener(details => { 95 100 context = getHostname(details.frameAncestors[last].url); 96 101 } 97 102 var hostname = getHostname(details.url);98 -1 var type = TYPE_MAP[details.type] || 'other';-1 103 var type = shared.TYPE_MAP[details.type] || 'other'; 99 104 100 105 pushRequest(details.tabId, hostname, type); 101 106102 -1 return {cancel: !shouldAllow(rules, context, hostname, type)};-1 107 return {cancel: !shared.shouldAllow(rules, context, hostname, type)}; 103 108 }, {urls: ['<all_urls>']}, ['blocking']); 104 109 105 110 browser.webRequest.onHeadersReceived.addListener(function(details) { 106 111 var context = getHostname(details.url); 107 112 var policy = []; 108 113109 -1 if (!shouldAllow(rules, context, 'inline', 'css')) {-1 114 if (!shared.shouldAllow(rules, context, 'inline', 'css')) { 110 115 policy.push("style-src 'self' *"); 111 116 }112 -1 if (!shouldAllow(rules, context, 'inline', 'script')) {-1 117 if (!shared.shouldAllow(rules, context, 'inline', 'script')) { 113 118 policy.push("script-src 'self' *"); 114 119 }115 -1 if (!shouldAllow(rules, context, 'inline', 'media')) {-1 120 if (!shared.shouldAllow(rules, context, 'inline', 'media')) { 116 121 policy.push("img-src 'self' *"); 117 122 } 118 123
diff --git a/popup.js b/popup.js
@@ -1,4 +1,4 @@1 -1 /* global browser */-1 1 /* global browser shared */ 2 2 3 3 var table = document.querySelector('table'); 4 4 @@ -45,7 +45,7 @@ sendMessage('get').then(data => { 45 45 selector += `[data-type="${type}"]`; 46 46 } 47 47 table.querySelectorAll(selector).forEach(input => {48 -1 input.classList.toggle('inherit-allow', shouldAllow(-1 48 input.classList.toggle('inherit-allow', shared.shouldAllow( 49 49 data.rules, 50 50 data.context, 51 51 input.dataset.hostname, @@ -76,7 +76,7 @@ sendMessage('get').then(data => { 76 76 77 77 tr.append(document.createElement('th')); 78 7879 -1 for (const type of TYPES) {-1 79 for (const type of shared.TYPES) { 80 80 let rule = rules['*'] ? rules['*'][type] : null; 81 81 82 82 let th = document.createElement('th'); @@ -103,7 +103,7 @@ sendMessage('get').then(data => { 103 103 span.textContent = hostname; 104 104 th.append(span); 105 105106 -1 for (const type of TYPES) {-1 106 for (const type of shared.TYPES) { 107 107 let count = data.requests[hostname] ? data.requests[hostname][type] : null; 108 108 let rule = rules[hostname] ? rules[hostname][type] : null; 109 109
diff --git a/shared.js b/shared.js
@@ -1,5 +1,7 @@1 -1 const TYPES = ['font', 'css', 'media', 'script', 'xhr', 'frame', 'other'];2 -1 const TYPE_MAP = {-1 1 var shared = {}; -1 2 -1 3 shared.TYPES = ['font', 'css', 'media', 'script', 'xhr', 'frame', 'other']; -1 4 shared.TYPE_MAP = { 3 5 'stylesheet': 'css', 4 6 'font': 'font', 5 7 'image': 'media', @@ -12,12 +14,7 @@ const TYPE_MAP = { 12 14 'sub_frame': 'frame', 13 15 }; 14 1615 -1 var getHostname = function(url) {16 -1 var u = new URL(url);17 -1 return u.hostname;18 -1 };19 -120 -1 var shouldAllow = function(rules, context, hostname, type) {-1 17 shared.shouldAllow = function(rules, context, hostname, type) { 21 18 var hostnames = ['*', hostname]; 22 19 if (context === hostname) { 23 20 hostnames.push('first-party');