Skip to content

Commit

Permalink
Merge pull request #109 from gbaptista/gb-advanced-options
Browse files Browse the repository at this point in the history
Advanced Options
  • Loading branch information
gbaptista authored Mar 23, 2019
2 parents 8be92e5 + 9908b29 commit af9abae
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 6 deletions.
3 changes: 3 additions & 0 deletions _locales/en_US/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"settingsCodeInjectionTitle": { "message": "code injection" },
"settingsPopupTitle": { "message": "popup" },
"settingsBadgeCounterTitle": { "message": "badge counter" },
"settingsAdvancedTitle": { "message": "advanced" },
"settingsStoredDataLocalTitle": { "message": "locally stored data" },
"settingsStoredDataSyncTitle": { "message": "synced stored data" },
"settingsReadMoreText": { "message": "read more about" },
Expand Down Expand Up @@ -79,6 +80,8 @@
"settingsNotRecommendedText": { "message": "not recommended" },
"settingsDefinitelyNotRecommendedText": { "message": "definitely not recommended" },

"settingsFilterResponseDataTitle": { "message": "use filterResponseData API (if supported)" },
"settingsInjectionTitle": { "message": "injection" },
"settingsReportsTitle": { "message": "reports" },
"settingsIndexedDbMdnHref": { "message": "https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API" },
"settingsRecordsText": { "message": "records" },
Expand Down
3 changes: 3 additions & 0 deletions _locales/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"settingsCodeInjectionTitle": { "message": "inyecciones de código" },
"settingsPopupTitle": { "message": "ventana emergente" },
"settingsBadgeCounterTitle": { "message": "contador del icono" },
"settingsAdvancedTitle": { "message": "avanzado" },
"settingsStoredDataLocalTitle": { "message": "datos locales" },
"settingsStoredDataSyncTitle": { "message": "datos sincronizados" },
"settingsReadMoreText": { "message": "lea más sobre" },
Expand Down Expand Up @@ -79,6 +80,8 @@
"settingsNotRecommendedText": { "message": "no recomendado" },
"settingsDefinitelyNotRecommendedText": { "message": "definitivamente no recomendado" },

"settingsFilterResponseDataTitle": { "message": "use la API filterResponseData (si es compatible)" },
"settingsInjectionTitle": { "message": "inyección" },
"settingsReportsTitle": { "message": "informes" },
"settingsIndexedDbMdnHref": { "message": "https://developer.mozilla.org/es/docs/Web/API/IndexedDB_API" },
"settingsRecordsText": { "message": "registros" },
Expand Down
3 changes: 3 additions & 0 deletions _locales/pt_BR/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"settingsCodeInjectionTitle": { "message": "injeção de códigos" },
"settingsPopupTitle": { "message": "popup" },
"settingsBadgeCounterTitle": { "message": "contador do ícone" },
"settingsAdvancedTitle": { "message": "avançado" },
"settingsStoredDataLocalTitle": { "message": "dados locais" },
"settingsStoredDataSyncTitle": { "message": "dados sincronizados" },
"settingsReadMoreText": { "message": "leia mais sobre" },
Expand Down Expand Up @@ -79,6 +80,8 @@
"settingsNotRecommendedText": { "message": "não recomendado" },
"settingsDefinitelyNotRecommendedText": { "message": "definitivamente não recomendado" },

"settingsFilterResponseDataTitle": { "message": "utilizar a API filterResponseData (se suportada)" },
"settingsInjectionTitle": { "message": "injeção" },
"settingsReportsTitle": { "message": "relatórios" },
"settingsIndexedDbMdnHref": { "message": "https://developer.mozilla.org/pt-BR/docs/Web/API/IndexedDB_API" },
"settingsRecordsText": { "message": "registros" },
Expand Down
31 changes: 31 additions & 0 deletions html/settings/advanced/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<title></title>

<link rel="stylesheet" type="text/css" href="../../../vendor/bootstrap/bootstrap.min.css">

<link rel="stylesheet" type="text/css" href="../../../css/settings/settings.css">
<script src="../../../vendor/jquery/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="../../../vendor/mustachejs/mustache.min.js" type="text/javascript"></script>
<script src="../../../js/utils/load_template.js" type="text/javascript"></script>
<script src="../../../js/settings/settings.js" type="text/javascript"></script>
<script src="../../../js/settings/advanced/options.js" type="text/javascript"></script>
</head>
<body>
<div id="loading"></div>
<div class="container-fluid">
<div class="row">
<div class="col-3">
<nav class="navbar"></nav>
</div>
<div class="col-8 settings-content">
<div id="form"></div>
</div>
</div>
</div>
</body>
</html>
22 changes: 22 additions & 0 deletions html/settings/templates/advanced/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<form>
{{#advanced_options}}
{{#injection}}
<div class="card">
<div class="card-header">
{{title}}
</div>
<div class="card-body">
{{#strategy}}
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" data-reverse="false" data-namespace="advanced" name="try_filter_response_data" value="{{value}}" {{#checked}}checked{{/checked}} disabled>
{{label}}
</label>
</div>
{{/strategy}}
</div>
</div>
{{/injection}}
{{/advanced_options}}
<br>
</form>
53 changes: 51 additions & 2 deletions js/background/injections/content/filter_response_data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
if(injection_strategy != 'cookie') {

var should_intercept_content_type = function(content_type) {
var should_intercept = false;

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
var splitted_content_type = content_type.toLowerCase().split(
';'
)[0].toLowerCase().split('/');

var type = splitted_content_type[0];
var subtype = splitted_content_type[1];

// TODO: Improve content_type check to fix issue #107:
// https://github.com/gbaptista/luminous/issues/107
if (['text', 'application'].includes(type)) {
should_intercept = /html/.test(subtype);
}

return should_intercept;
}

var interceptor_html_string = undefined;
var data_html_string = undefined;

// TODO remove cache for development enviroment
// TODO: remove cache for development enviroment
load_interceptor_element('filterResponseData', function(element) {
interceptor_html_string = element_to_html_string(element);
});
Expand All @@ -14,9 +36,27 @@ if(injection_strategy != 'cookie') {
var encoder = new TextEncoder();
var decoder = new TextDecoder('utf-8');

var requests_content_types = {};

var detect_request_type = function(request_details) {
requests_content_types[request_details.requestId] = undefined;

var url = url_for_request(request_details);

if(should_intercept_request(url)) {
for(let header of request_details.responseHeaders) {
if(header.name.toLowerCase() == 'content-type') {
requests_content_types[request_details.requestId] = header.value;
}
}
}
}

var inject_interceptor_and_settings = function(request_details) {
var url = url_for_request(request_details);

// TODO: Improve should_intercept_request to fix issue #107:
// https://github.com/gbaptista/luminous/issues/107
if(should_intercept_request(url)) {
var options = full_options_for_url(url);

Expand All @@ -32,12 +72,14 @@ if(injection_strategy != 'cookie') {
var content_to_write = options_html_string + data_html_string + interceptor_html_string;

filter.ondata = event => {
var content_type = requests_content_types[request_details.requestId];

var content = decoder.decode(event.data, {stream: true});

// binary content? Image, PDF...
var binary = /\0/.exec(content);

if(!binary) {
if(!binary && should_intercept_content_type(content_type)) {
var match = /DOCTYPE(.|[\r\n])*?>/i.exec(content);

if(match && match.index < 10 && match[0].length < 200) {
Expand All @@ -61,6 +103,13 @@ if(injection_strategy != 'cookie') {
return {};
}

chrome.webRequest.onHeadersReceived.addListener(
detect_request_type,
{ urls: ['<all_urls>'], types: ['main_frame', 'sub_frame'] },
['responseHeaders', 'blocking']
);


chrome.webRequest.onBeforeRequest.addListener(
inject_interceptor_and_settings,
{ urls: ['<all_urls>'], types: ['main_frame', 'sub_frame'] },
Expand Down
8 changes: 8 additions & 0 deletions js/background/setup/default_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ var set_default_settings = function() {
sync_data['auto_settings'] = {};
}

if(!sync_data['advanced']) {
sync_data['advanced'] = {};
}

if(sync_data['advanced']['try_filter_response_data'] == undefined) {
sync_data['advanced']['try_filter_response_data'] = true;
}

if(!sync_data['auto_settings']['domains']) {
sync_data['auto_settings']['domains'] = {};
}
Expand Down
3 changes: 3 additions & 0 deletions js/background/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ if(
&&
browser.webRequest.filterResponseData
) {
// TODO: Use sync_data['advanced']['try_filter_response_data']
// if true: 'filterResponseData'
// else: 'cookie'

// has filterResponseData support (Firefox 57+ only)
injection_strategy = 'filterResponseData';
Expand Down
19 changes: 17 additions & 2 deletions js/content/initializer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
var should_inject_this_document = function() {
// thanks to: https://github.com/EFForg/privacybadger/pull/1954
if (
document instanceof HTMLDocument === false &&
(
document instanceof XMLDocument === false ||
document.createElement('div') instanceof HTMLDivElement === false
)
) {
return false;
}

return true;
}

var intialize_luminous_injections = function(from) {
if(from == 'cookie') {
var injection_enabled = Cookies.get('le');
Expand All @@ -10,7 +25,7 @@ var intialize_luminous_injections = function(from) {

if(from == 'cookie') {
if(injection_enabled != 'f') {
fn(true);
fn(should_inject_this_document());
}
} else {
fn(false);
Expand All @@ -33,5 +48,5 @@ if(injection_strategy == 'cookie') {
intialize_luminous_injections('cookie');
} else {
// TODO Firefox 59+?
intialize_luminous_injections('injection_strategy');
intialize_luminous_injections('filterResponseData');
}
8 changes: 6 additions & 2 deletions js/content/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ if(typeof browser !== 'undefined') {
// Firefox without filterResponseData support...
injection_strategy = 'cookie';
} else {
// Let's hope it's a Firefox 57+ or
// something with filterResponseData support.
// TODO: Use sync_data['advanced']['try_filter_response_data']
// if true: 'filterResponseData'
// else: 'cookie'

// Let's hope it's a Firefox 57+ or something
// with filterResponseData support.
injection_strategy = 'filterResponseData';
}
}
35 changes: 35 additions & 0 deletions js/settings/advanced/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
$(document).ready(function() {
load_template('html/settings/templates/advanced/form.html', function(template) {
var load_sync_data = function() {
chrome.storage.sync.get(null, function(sync_data) {
$('#form').html(
Mustache.render(template, {
advanced_options: {
injection: {
title: chrome.i18n.getMessage('settingsInjectionTitle'),
strategy: {
label: chrome.i18n.getMessage('settingsFilterResponseDataTitle'),
value: sync_data['advanced']['try_filter_response_data'],
checked: sync_data['advanced']['try_filter_response_data']
}
},
}
})
);

observe_form();

loaded();
});
}

chrome.storage.onChanged.addListener(function(changes, namespace) {
if(namespace == 'sync' && changes) {
loading();
load_sync_data();
}
});

load_sync_data();
});
});
5 changes: 5 additions & 0 deletions js/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ $(document).ready(function() {
url: chrome.extension.getURL('html/settings/stored-data/local.html'),
active: (document.location.pathname == '/html/settings/stored-data/local.html')
},
{
title: chrome.i18n.getMessage('settingsAdvancedTitle'),
url: chrome.extension.getURL('html/settings/advanced/options.html'),
active: (document.location.pathname == '/html/settings/advanced/options.html')
},
]
})
);
Expand Down

0 comments on commit af9abae

Please sign in to comment.