From 8b822f50f435c2db0443692cb4110982b2e01173 Mon Sep 17 00:00:00 2001 From: didierfred <didierfred@gmail.com> Date: Wed, 25 Jul 2018 10:09:14 +0200 Subject: [PATCH] add multiple url pattern support and debug mode --- README.md | 4 +-- background.js | 70 ++++++++++++++++++++++++++++++----------------- popup/config.html | 16 ++++++++--- popup/config.js | 29 ++++++++++++++------ 4 files changed, 79 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index a755442..49c22ff 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# SimpleModifyHeaders V 1.4 +# SimpleModifyHeaders V 1.5 Extension for firefox and chrome. @@ -12,7 +12,7 @@ The rules table contains lines with the following parameters : - apply on : "request" if the modification apply on the request headers or "response" if the modification apply on the response headers - status : on if the modification is active , off otherwise -We can choose the urls on which the modifications applies by modifying the url pattern. The url pattern must follow the syntaxe define by https://developer.chrome.com/extensions/match_patterns . Putting an empty string on the field will select all urls. +We can choose the urls on which the modifications applies by modifying the url pattern. The url pattern must follow the syntaxe define by https://developer.chrome.com/extensions/match_patterns . Putting an empty string on the field will select all urls. It's possible to select mutliple url patterns using semicolon(;) separator To save and apply the modification , you need to click on the save button diff --git a/background.js b/background.js index b424d12..3b4bbf1 100644 --- a/background.js +++ b/background.js @@ -4,7 +4,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * @author didierfred@gmail.com - * @version 0.3 + * @version 0.4 */ @@ -12,6 +12,7 @@ var config ; var started = "off"; +var debug_mode = false; // if configuration exist if (localStorage.getItem('config')) @@ -24,6 +25,7 @@ if (localStorage.getItem('config')) { config.format_version="1.1"; for (var line of config.headers) line.apply_on="req"; + config.debug_mode=false; console.log("save new config"+JSON.stringify(config)); localStorage.setItem("config",JSON.stringify(config)); } @@ -40,7 +42,7 @@ else { headers.push({action:to_modify[0],header_name:to_modify[1],header_value:to_modify[2],comment:"",apply_on:"req",status:to_modify[3]}); } - config = {format_version:"1.1",target_page:localStorage.getItem('targetPage'),headers:headers}; + config = {format_version:"1.1",target_page:localStorage.getItem('targetPage'),headers:headers,debug_mode:false}; // save old config in new format localStorage.setItem("config",JSON.stringify(config)); } @@ -50,7 +52,7 @@ else console.log("Load default config"); var headers = []; headers.push({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}; + config = {format_version:"1.1",target_page:"https://httpbin.org/*",headers:headers,debug_mode:false}; // save configuration localStorage.setItem("config",JSON.stringify(config)); } @@ -61,6 +63,7 @@ else if (!localStorage.getItem('started')) localStorage.setItem('started',started); else started = localStorage.getItem('started'); + if (started=="on") { addListener(); @@ -71,13 +74,23 @@ if (started=="on") browser.runtime.onMessage.addListener(notify); +/* +* Standard function to log messages +* +*/ + +function log(message) +{ +console.log(new Date() + " SimpleModifyHeader : " + message); +} + /* * Rewrite the request header (add , modify or delete) * */ function rewriteRequestHeader(e) { - + if (config.debug_mode) log("Start modify request headers for url " + e.url); for (var to_modify of config.headers) { if ((to_modify.status=="on")&&(to_modify.apply_on=="req")) @@ -86,12 +99,17 @@ function rewriteRequestHeader(e) { var new_header = {"name" :to_modify.header_name,"value":to_modify.header_value}; e.requestHeaders.push(new_header); + if (config.debug_mode) log("Add request header : name=" + to_modify.header_name + ",value=" + to_modify.header_value + " for url " + e.url); } else if (to_modify.action=="modify") { for (var header of e.requestHeaders) { - if (header.name.toLowerCase() == to_modify.header_name.toLowerCase()) header.value = to_modify.header_value; + if (header.name.toLowerCase() == to_modify.header_name.toLowerCase()) + { + if (config.debug_mode) log("Modify request header : name= " + to_modify.header_name + ",old value=" + header.value + ",new value=" + to_modify.header_value + " for url " + e.url); + header.value = to_modify.header_value; + } } } else if (to_modify.action=="delete") @@ -105,11 +123,12 @@ function rewriteRequestHeader(e) if (index!=-1) { e.requestHeaders.splice(index,1); + if (config.debug_mode) log("Delete request header : name=" + to_modify.header_name.toLowerCase() + " for url " + e.url); } } } } - + if (config.debug_mode) log("End modify request headers for url " + e.url); return {requestHeaders: e.requestHeaders}; } @@ -120,6 +139,7 @@ function rewriteRequestHeader(e) */ function rewriteResponseHeader(e) { + if (config.debug_mode) log("Start modify response headers for url " + e.url); for (var to_modify of config.headers) { if ((to_modify.status=="on")&&(to_modify.apply_on=="res")) @@ -128,12 +148,17 @@ function rewriteResponseHeader(e) { var new_header = {"name" :to_modify.header_name,"value":to_modify.header_value}; e.responseHeaders.push(new_header); + if (config.debug_mode) log("Add response header : name=" + to_modify.header_name + ",value=" + to_modify.header_value + " for url " + e.url); } else if (to_modify.action=="modify") { for (var header of e.responseHeaders) { - if (header.name.toLowerCase() == to_modify.header_name.toLowerCase()) header.value = to_modify.header_value; + if (header.name.toLowerCase() == to_modify.header_name.toLowerCase()) + { + if (config.debug_mode) log("Modify response header : name= " + to_modify.header_name + ",old value=" + header.value + ",new value=" + to_modify.header_value + " for url " + e.url); + header.value = to_modify.header_value; + } } } else if (to_modify.action=="delete") @@ -146,13 +171,14 @@ function rewriteResponseHeader(e) } if (index!=-1) { - e.responseHeaders.splice(index,1); + e.responseHeaders.splice(index,1); + if (config.debug_mode) log("Delete response header : name=" + to_modify.header_name.toLowerCase() + " for url " + e.url); } } } } - + if (config.debug_mode) log("End modify response headers for url " + e.url); return {responseHeaders: e.responseHeaders}; } @@ -168,6 +194,7 @@ function notify(message) { if (message=="reload") { + if (config.debug_mode) log("Reload configuration"); config=JSON.parse(localStorage.getItem("config")); if (started=="on") { @@ -181,6 +208,7 @@ function notify(message) removeListener(); browser.browserAction.setIcon({ path: "icons/modify-32.png"}); started="off"; + if (config.debug_mode) log("Stop modifying headers"); } else if (message=="on") @@ -188,38 +216,31 @@ function notify(message) addListener(); browser.browserAction.setIcon({ path: "icons/modify-green-32.png"}); started="on"; + if (config.debug_mode) log("Start modifying headers"); } } /* -* Add rewriteRequestHeader as a listener to onBeforeSendHeaders, only for the target page. -* Add rewriteResponseHeader as a listener to onHeadersReceived, only for the target page. +* Add rewriteRequestHeader as a listener to onBeforeSendHeaders, only for the target pages. +* Add rewriteResponseHeader as a listener to onHeadersReceived, only for the target pages. * Make it "blocking" so we can modify the headers. */ function addListener() { var target = config.target_page; + if ((target=="*")||(target=="")||(target==" ")) target="<all_urls>"; browser.webRequest.onBeforeSendHeaders.addListener(rewriteRequestHeader, - {urls: [target]}, + {urls: target.split(";")}, ["blocking", "requestHeaders"]); browser.webRequest.onHeadersReceived.addListener(rewriteResponseHeader, - {urls: [target]}, + {urls: target.split(";")}, ["blocking", "responseHeaders"]); -// for debug only -// browser.webRequest.onCompleted.addListener(log_headers, -// {urls: [target]}, -// ["responseHeaders"]); - } - -function log_headers(e) -{ -console.log("response=" +JSON.stringify(e.responseHeaders)); -} + } /* @@ -230,8 +251,7 @@ function removeListener() { browser.webRequest.onBeforeSendHeaders.removeListener(rewriteRequestHeader); browser.webRequest.onHeadersReceived.removeListener(rewriteResponseHeader); -// for debug only -// browser.webRequest.onCompleted.removeListener(log_headers); + } diff --git a/popup/config.html b/popup/config.html index ec95afa..afd70ba 100644 --- a/popup/config.html +++ b/popup/config.html @@ -46,7 +46,7 @@ <br/> <div class="row"> <div class="col-sm-8"> - <b> Url Pattern* : </b> <input size="50" id="targetPage" class="form_control input_url" type="text" value=""> + <b> Url Patterns* : </b> <input size="75" id="targetPage" class="form_control input_url" type="text" value=""> </div> <div class="col-sm-4" align="right"> <a href="#" id="export_button" class="btn btn-primary btn-sm" style="width:100px"> @@ -91,15 +91,23 @@ </a> -<br> + </center> + + <br> <div class="row"> <div class="col-sm-10"> - <i style="font-size:12pt"> * Informations on url pattern can be found <a href="https://developer.chrome.com/extensions/match_patterns" target="_blank"> here </a>   (An empty string on the field will select all urls.) </i> + <input type="checkbox" id="debug_mode"> Debug mode </input> + <br/><br/> + <i style="font-size:10pt"> * Informations on url pattern can be found <a href="https://developer.chrome.com/extensions/match_patterns" target="_blank"> here.</a> <br> An empty string on the field will select all urls. It's possible to select mutliple url patterns using semicolon(;) separator </i> + + </div> - <div class="col-sm-2" align="right"> + <div class="col-sm-2" align="right" > + <br/> + <br/> <a href="https://github.com/didierfred/SimpleModifyHeaders/tree/V1.4" target="_blank"> <span class="glyphicon glyphicon-question-sign"></span> About </a> </div> </div> diff --git a/popup/config.js b/popup/config.js index 0e97d76..52d755c 100644 --- a/popup/config.js +++ b/popup/config.js @@ -5,7 +5,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * @author didierfred@gmail.com - * @version 0.3 + * @version 0.4 */ @@ -21,12 +21,15 @@ window.onload = function() { document.getElementById('save_button').addEventListener('click',function (e) {save_data();}); document.getElementById('export_button').addEventListener('click',function (e) {export_data();}); document.getElementById('import_button').addEventListener('click',function (e) {import_data(e);}); - document.getElementById('add_button').addEventListener('click',function (e) {appendLine("add","-","-","","req","off");}); + document.getElementById('add_button').addEventListener('click',function (e) {appendLine("add","-","-","","req","on");}); document.getElementById('start_img').addEventListener('click',function (e) {start_modify();}); document.getElementById('targetPage').value=config.target_page; document.getElementById('targetPage').addEventListener('keyup',function (e) {checkTargetPageField();}); + started = localStorage.getItem("started"); if (started=="on") document.getElementById("start_img").src = "img/stop.png"; + + if (config.debug_mode) document.getElementById("debug_mode").checked = true; } ; /** @@ -70,6 +73,7 @@ function create_configuration_data() { var tr_elements = document.querySelectorAll("#config_tab tr"); var headers = []; + var debug_mode=false; for (i=0;i<tr_elements.length;i++) { @@ -81,7 +85,8 @@ function create_configuration_data() var status = tr_elements[i].childNodes[5].childNodes[0].value; headers.push({action:action,header_name:header_name,header_value:header_value,comment:comment,apply_on:apply_on,status:status}); } - var to_export = {format_version:"1.1",target_page:document.getElementById('targetPage').value,headers:headers}; + if (document.getElementById("debug_mode").checked) debug_mode=true ; + var to_export = {format_version:"1.1",target_page:document.getElementById('targetPage').value,headers:headers,debug_mode:debug_mode}; return JSON.stringify(to_export); } @@ -95,24 +100,29 @@ else document.getElementById('targetPage').style.color="red"; } /** -* check if url pattern is valid +* check if url patterns are valid **/ function isTargetValid(target) { if (target=="") return true; if (target==" ") return true; if (target=="*") return true; - return target.match("(http|https|[\*]):\/\/([\*][\.][^\*]*|[^\*]*|[\*])\/"); + targets=target.split(";"); + for (i in targets) + { + if (!targets[i].match("(http|https|[\*]):\/\/([\*][\.][^\*]*|[^\*]*|[\*])\/")) return false; + } + return true; } /** -* If url pattern is valid save the data to the local storage and restart modify header +* If url patterns are valid save the data to the local storage and restart modify header **/ function save_data() { if (!isTargetValid(document.getElementById('targetPage').value)) { - alert("Can not save configuration: Url pattern is invalid"); + alert("Can not save configuration: Url patterns are invalid"); return false; } localStorage.setItem("config",create_configuration_data()); @@ -201,6 +211,7 @@ function readSingleFile(e) { config.format_version="1.1"; for (var line of config.headers) line.apply_on="req"; + config.debug_mode=false; } // store the conf in the local storage @@ -223,7 +234,7 @@ function readSingleFile(e) if (line_to_load.action=="Filter") line_to_load.action="delete"; headers.push({action:line_to_load.action.toLowerCase(),header_name:line_to_load.name,header_value:line_to_load.value,comment:line_to_load.comment,apply_on:"req",status:enabled}); } - var to_load = {format_version:"1.1",target_page:"",headers:headers}; + var to_load = {format_version:"1.1",target_page:"",headers:headers,debug_mode:false}; // store the conf in the local storage localStorage.setItem("config",JSON.stringify(to_load)); @@ -264,7 +275,7 @@ function delete_line(line_number_to_delete) } } var Node_to_delete = document.getElementById("line"+(line_number-1)); - Node_to_delete.parentNode.removeChild(Node_to_delete); + Node_to_delete.parentNode.removeChild(Node_to_delete); line_number--; } -- GitLab