From 5f31b7ef41b6d84aacbc365d3f75e520c07f1c50 Mon Sep 17 00:00:00 2001 From: didierfred <didierfred@gmail.com> Date: Sat, 3 Nov 2018 19:43:35 +0100 Subject: [PATCH] add filter per rules --- background.js | 6 +++--- popup/config.html | 4 ++-- popup/config.js | 14 +++++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/background.js b/background.js index 32e72cb..c4b361d 100644 --- a/background.js +++ b/background.js @@ -54,7 +54,7 @@ else { else { console.log("Load default config"); let headers = []; - headers.push({action:"add",header_name:"test-header-name",header_value:"test-header-value",comment:"test",apply_on:"req",status:"on"}); + headers.push({url_contains:"",action:"add",header_name:"test-header-name",header_value:"test-header-value",comment:"test",apply_on:"req",status:"on"}); config = {format_version:"1.1",target_page:"https://httpbin.org/*",headers:headers,debug_mode:false}; // save configuration localStorage.setItem("config",JSON.stringify(config)); @@ -92,7 +92,7 @@ function log(message) { function rewriteRequestHeader(e) { if (config.debug_mode) log("Start modify request headers for url " + e.url); for (let to_modify of config.headers) { - if ((to_modify.status==="on")&&(to_modify.apply_on==="req")) { + if ((to_modify.status==="on")&&(to_modify.apply_on==="req")&& (!config.use_url_contains || (config.use_url_contains && e.url.includes(to_modify.url_contains)))) { if (to_modify.action==="add"){ let new_header = {"name" :to_modify.header_name,"value":to_modify.header_value}; e.requestHeaders.push(new_header); @@ -130,7 +130,7 @@ function rewriteRequestHeader(e) { function rewriteResponseHeader(e) { if (config.debug_mode) log("Start modify response headers for url " + e.url); for (let to_modify of config.headers) { - if ((to_modify.status==="on")&&(to_modify.apply_on==="res")) { + if ((to_modify.status==="on")&&(to_modify.apply_on==="res")&& (!config.use_url_contains || (config.use_url_contains && e.url.includes(to_modify.url_contains)))) { if (to_modify.action==="add") { let new_header = {"name" :to_modify.header_name,"value":to_modify.header_value}; e.responseHeaders.push(new_header); diff --git a/popup/config.html b/popup/config.html index c65651f..7d63a5b 100644 --- a/popup/config.html +++ b/popup/config.html @@ -59,8 +59,8 @@ <a href="#" id="import_button" class="btn btn-primary btn-sm" > <span class="glyphicon glyphicon-import"></span> Import </a> - <a href="#" id="parameters_button" class="btn btn-primary btn-sm"> - <span class="glyphicon glyphicon-cog"></span> + <a href="#" id="parameters_button" class="btn btn-primary btn-sm"> + <span class="glyphicon glyphicon-cog"></span> Parameters </a> </div> </div> diff --git a/popup/config.js b/popup/config.js index 806ca57..43a20a4 100644 --- a/popup/config.js +++ b/popup/config.js @@ -33,13 +33,14 @@ window.onload = function() { } for (let to_add of config.headers) appendLine(to_add.url_contains,to_add.action,to_add.header_name,to_add.header_value,to_add.comment,to_add.apply_on,to_add.status); - document.getElementById('save_button').addEventListener('click',function (e) {saveData();}); + document.getElementById('save_button').addEventListener('click',function (e) {saveDataWithWarning();}); document.getElementById('export_button').addEventListener('click',function (e) {exportData();}); document.getElementById('import_button').addEventListener('click',function (e) {importData(e);}); document.getElementById('parameters_button').addEventListener('click',function (e) {showParametersScreen();}); document.getElementById('add_button').addEventListener('click',function (e) {appendLine("","add","-","-","","req","on");}); document.getElementById('start_img').addEventListener('click',function (e) {startModify();}); document.getElementById('targetPage').value=config.target_page; + checkTargetPageField(); document.getElementById('targetPage').addEventListener('keyup',function (e) {checkTargetPageField();}); document.getElementById('exit_parameters_screen_button').addEventListener('click',function (e) {hideParametersScreen();}); @@ -62,6 +63,7 @@ function showParametersScreen() { function hideParametersScreen() { document.getElementById('main_screen').hidden=false; document.getElementById('parameters_screen').hidden=true; + saveData(); } function showCommentsClick() { @@ -245,11 +247,13 @@ function isTargetValid(target) { * If url patterns are valid save the data to the local storage and restart modify header **/ +function saveDataWithWarning() { + if (!isTargetValid(document.getElementById('targetPage').value)) alert("Warning: Url patterns are invalid"); + saveData(); +} + + function saveData() { - if (!isTargetValid(document.getElementById('targetPage').value)) { - alert("Can not save configuration: Url patterns are invalid"); - return false; - } localStorage.setItem("config",create_configuration_data()); browser.runtime.sendMessage("reload"); return true; -- GitLab