Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

Commit

Permalink
fix uBlockOrigin/uMatrix-issues#59 and other unrelated quirks
Browse files Browse the repository at this point in the history
Unrelated quirks:

- missing icons for magnifier (because uBlockOrigin/uMatrix-issues#68)
- missing i18n string
- use separate file for CSS styles
  • Loading branch information
gorhill committed Oct 6, 2018
1 parent 495dddc commit 886664a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 150 deletions.
4 changes: 4 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,10 @@
"message": "Because of the following rule",
"description": "English: Because of the following rule"
},
"mainBlockedNoParamsPrompt": {
"message": "without parameters",
"description": "label to be used for the parameter-less URL: https://cloud.githubusercontent.com/assets/585534/9832014/bfb1b8f0-593b-11e5-8a27-fba472a5529a.png"
},
"mainBlockedBack" : {
"message": "Go back",
"description": "English: Go back"
Expand Down
2 changes: 2 additions & 0 deletions src/css/fa-icons.css
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
.fa-icon > .fa-icon_power-off,
.fa-icon > .fa-icon_question-circle,
.fa-icon > .fa-icon_reply,
.fa-icon > .fa-icon_search-minus,
.fa-icon > .fa-icon_search-plus,
.fa-icon > .fa-icon_spinner,
.fa-icon > .fa-icon_sync-alt,
.fa-icon > .fa-icon_th,
Expand Down
2 changes: 2 additions & 0 deletions src/img/fontawesome/fontawesome-defs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 40 additions & 38 deletions src/js/main-blocked.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
uMatrix - a browser extension to block requests.
Copyright (C) 2015-2017 Raymond Hill
Copyright (C) 2015-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,10 +29,10 @@

/******************************************************************************/

var details = {};
let details = {};

(function() {
var matches = /details=([^&]+)/.exec(window.location.search);
let matches = /details=([^&]+)/.exec(window.location.search);
if ( matches === null ) { return; }
try {
details = JSON.parse(atob(matches[1]));
Expand All @@ -52,25 +52,23 @@ uDom('.what').text(details.url);
// https://github.com/gorhill/uBlock/blob/master/src/js/document-blocked.js

(function() {
if ( typeof URL !== 'function' ) { return; }
let reURL = /^https?:\/\//;

var reURL = /^https?:\/\//;

var liFromParam = function(name, value) {
let liFromParam = function(name, value) {
if ( value === '' ) {
value = name;
name = '';
}
var li = document.createElement('li');
var span = document.createElement('span');
let li = document.createElement('li');
let span = document.createElement('span');
span.textContent = name;
li.appendChild(span);
if ( name !== '' && value !== '' ) {
li.appendChild(document.createTextNode(' = '));
}
span = document.createElement('span');
if ( reURL.test(value) ) {
var a = document.createElement('a');
let a = document.createElement('a');
a.href = a.textContent = value;
span.appendChild(a);
} else {
Expand All @@ -80,39 +78,38 @@ uDom('.what').text(details.url);
return li;
};

var safeDecodeURIComponent = function(s) {
let safeDecodeURIComponent = function(s) {
try {
s = decodeURIComponent(s);
} catch (ex) {
}
return s;
};

var renderParams = function(parentNode, rawURL) {
var a = document.createElement('a');
let renderParams = function(parentNode, rawURL) {
let a = document.createElement('a');
a.href = rawURL;
if ( a.search.length === 0 ) { return false; }

var pos = rawURL.indexOf('?');
var li = liFromParam(
vAPI.i18n('docblockedNoParamsPrompt'),
let pos = rawURL.indexOf('?');
let li = liFromParam(
vAPI.i18n('mainBlockedNoParamsPrompt'),
rawURL.slice(0, pos)
);
parentNode.appendChild(li);

var params = a.search.slice(1).split('&');
var param, name, value, ul;
let params = a.search.slice(1).split('&');
for ( var i = 0; i < params.length; i++ ) {
param = params[i];
pos = param.indexOf('=');
let param = params[i];
let pos = param.indexOf('=');
if ( pos === -1 ) {
pos = param.length;
}
name = safeDecodeURIComponent(param.slice(0, pos));
value = safeDecodeURIComponent(param.slice(pos + 1));
let name = safeDecodeURIComponent(param.slice(0, pos));
let value = safeDecodeURIComponent(param.slice(pos + 1));
li = liFromParam(name, value);
if ( reURL.test(value) ) {
ul = document.createElement('ul');
let ul = document.createElement('ul');
renderParams(ul, value);
li.appendChild(ul);
}
Expand All @@ -121,25 +118,30 @@ uDom('.what').text(details.url);
return true;
};

if ( renderParams(uDom.nodeFromId('parsed'), details.url) === false ) {
return;
}
let hasParams = renderParams(uDom.nodeFromId('parsed'), details.url);
if ( hasParams === false ) { return; }

var toggler = document.createElement('span');
toggler.className = 'fa';
uDom('#theURL > p').append(toggler);
let theURLNode = document.getElementById('theURL');
theURLNode.classList.add('hasParams');
theURLNode.classList.toggle(
'collapsed',
vAPI.localStorage.getItem('document-blocked-collapse-url') === 'true'
);

uDom(toggler).on('click', function() {
var collapsed = uDom.nodeFromId('theURL').classList.toggle('collapsed');
let toggleCollapse = function() {
vAPI.localStorage.setItem(
'document-blocked-collapse-url',
collapsed.toString()
theURLNode.classList.toggle('collapsed').toString()
);
});
};

uDom.nodeFromId('theURL').classList.toggle(
'collapsed',
vAPI.localStorage.getItem('document-blocked-collapse-url') === 'true'
theURLNode.querySelector('.collapse').addEventListener(
'click',
toggleCollapse
);
theURLNode.querySelector('.expand').addEventListener(
'click',
toggleCollapse
);
})();

Expand All @@ -161,8 +163,8 @@ vAPI.messaging.send('main-blocked.js', {
what: 'mustBlock',
scope: details.hn,
hostname: details.hn,
type: 'doc'
}, function(response) {
type: details.type
}, response => {
if ( response === false ) {
window.location.replace(details.url);
}
Expand Down
32 changes: 26 additions & 6 deletions src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var onBeforeRootFrameRequestHandler = function(details) {
let µm = µMatrix;
let desURL = details.url;
let desHn = µm.URI.hostnameFromURI(desURL);
let type = requestTypeNormalizer[details.type] || 'other';
let tabId = details.tabId;

µm.tabContextManager.push(tabId, desURL);
Expand All @@ -43,13 +44,13 @@ var onBeforeRootFrameRequestHandler = function(details) {
let srcHn = tabContext.rootHostname;

// Disallow request as per matrix?
let blocked = µm.mustBlock(srcHn, desHn, 'doc');
let blocked = µm.mustBlock(srcHn, desHn, type);

let pageStore = µm.pageStoreFromTabId(tabId);
pageStore.recordRequest('doc', desURL, blocked);
pageStore.recordRequest(type, desURL, blocked);
pageStore.perLoadAllowedRequestCount = 0;
pageStore.perLoadBlockedRequestCount = 0;
µm.logger.writeOne({ tabId, srcHn, desHn, desURL, type: 'doc', blocked });
µm.logger.writeOne({ tabId, srcHn, desHn, desURL, type, blocked });

// Not blocked
if ( !blocked ) {
Expand All @@ -62,7 +63,7 @@ var onBeforeRootFrameRequestHandler = function(details) {
}

// Blocked
let query = btoa(JSON.stringify({ url: desURL, hn: desHn, why: '?' }));
let query = btoa(JSON.stringify({ url: desURL, hn: desHn, type, why: '?' }));

vAPI.tabs.replace(tabId, vAPI.getURL('main-blocked.html?details=') + query);

Expand Down Expand Up @@ -343,12 +344,18 @@ var onHeadersReceived = function(details) {
let µm = µMatrix,
tabId = details.tabId,
requestURL = details.url,
requestType = requestTypeNormalizer[details.type] || 'other';
requestType = requestTypeNormalizer[details.type] || 'other',
headers = details.responseHeaders;

// https://github.com/gorhill/uMatrix/issues/145
// Check if the main_frame is a download
if ( requestType === 'doc' ) {
µm.tabContextManager.push(tabId, requestURL);
let contentType = typeFromHeaders(headers);
if ( contentType !== undefined ) {
details.type = contentType;
return onBeforeRootFrameRequestHandler(details);
}
}

let tabContext = µm.tabContextManager.lookup(tabId);
Expand Down Expand Up @@ -382,7 +389,6 @@ var onHeadersReceived = function(details) {
// if the current environment does not support merging headers:
// Firefox 58/webext and less can't merge CSP headers, so we will merge
// them here.
var headers = details.responseHeaders;

if ( csp.length !== 0 ) {
let cspRight = csp.join(', ');
Expand Down Expand Up @@ -465,6 +471,20 @@ var headerIndexFromName = function(headerName, headers) {

/******************************************************************************/

// Extract request type from content headers.

let typeFromHeaders = function(headers) {
let i = headerIndexFromName('content-type', headers);
if ( i === -1 ) { return; }
let mime = headers[i].value.toLowerCase();
if ( mime.startsWith('image/') ) { return 'image'; }
if ( mime.startsWith('video/') || mime.startsWith('audio/') ) {
return 'media';
}
};

/******************************************************************************/

var requestTypeNormalizer = {
'font' : 'css',
'image' : 'image',
Expand Down
Loading

0 comments on commit 886664a

Please sign in to comment.