From b612da1f820cb29ce4cb22e5969f6946ec29e080 Mon Sep 17 00:00:00 2001
From: didierfred <didierfred@gmail.com>
Date: Fri, 23 Nov 2018 16:05:56 +0100
Subject: [PATCH] resolve sonar issues

---
 background.js   | 43 +++++++++++++++++-------------
 popup/config.js | 69 ++++++++++++++++++++++++++++---------------------
 popup/menu.js   | 26 ++++++++-----------
 3 files changed, 75 insertions(+), 63 deletions(-)

diff --git a/background.js b/background.js
index 4632e84..39a47d0 100644
--- a/background.js
+++ b/background.js
@@ -10,7 +10,7 @@
 
 "use strict";
 
-let config ;
+let config;
 let started = "off";
 let debug_mode = false;
 
@@ -19,9 +19,9 @@ let debug_mode = false;
 // if configuration exist 
 if (localStorage.getItem('config')) {
   console.log("Load standard config");
-  config= JSON.parse(localStorage.getItem('config'));	
+  config= JSON.parse(localStorage.getItem('config'));
   
- // If config 1.0 (Simple Modify headers V1.2) , save to format 1.1	
+ // If config 1.0 (Simple Modify headers V1.2) , save to format 1.1
   if (config.format_version==="1.0") {
     config.format_version="1.1";
     for (let line of config.headers) line.apply_on="req";
@@ -49,7 +49,7 @@ else {
       headers.push({action:to_modify[0],url_contains:"",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,debug_mode:false};
-    // save old config in new format 
+    // save old config in new format
     localStorage.setItem("config",JSON.stringify(config));
   }
   //else no config exists, create a default one
@@ -58,13 +58,13 @@ else {
     let headers = [];
     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 
+    // save configuration
     localStorage.setItem("config",JSON.stringify(config));
   }
 }
 		
 		
-// If no started value stored , use a default one 
+// If no started value stored , use a default one
 if (!localStorage.getItem('started')) localStorage.setItem('started',started);
 else started = localStorage.getItem('started');
 
@@ -74,12 +74,12 @@ if (started==="on") {
   chrome.browserAction.setIcon({ path: "icons/modify-green-32.png"});
 }
 
-// listen for change in configuration or start/stop 
+// listen for change in configuration or start/stop
 chrome.runtime.onMessage.addListener(notify);
 
 
 /*
-* Standard function to log messages 
+* Standard function to log messages
 *
 */
 
@@ -98,12 +98,15 @@ function rewriteRequestHeader(e) {
       if (to_modify.action==="add"){
         let 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);
+		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 (let header of e.requestHeaders) {
           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);
+            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;
           }
         }
@@ -115,7 +118,8 @@ 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("Delete request header :  name=" + to_modify.header_name.toLowerCase() +
+		    " for url " + e.url);
         }
       }
     }
@@ -136,12 +140,14 @@ function rewriteResponseHeader(e) {
       if (to_modify.action==="add") {
         let 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);
+	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 (let header of e.responseHeaders) {
           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);
+            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;
           }
         }
@@ -153,7 +159,8 @@ function rewriteResponseHeader(e) {
 	}
         if (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("Delete response header :  name=" + to_modify.header_name.toLowerCase()
+									+ " for url " + e.url);		
         }
       }
     }
@@ -165,8 +172,8 @@ function rewriteResponseHeader(e) {
 
 /*
 * Listen for message form config.js
-* if message is reload : reload the configuration 
-* if message is on : start the modify header 
+* if message is reload : reload the configuration
+* if message is on : start the modify header
 * if message is off : stop the modify header
 *
 **/
@@ -174,7 +181,7 @@ function notify(message) {
   if (message==="reload") {
     if (config.debug_mode) log("Reload configuration");
     config=JSON.parse(localStorage.getItem("config"));
-    if (started==="on") {		
+    if (started==="on") {
       removeListener();
       addListener();
     }
@@ -212,7 +219,7 @@ function addListener() {
 
 
 /*
-* Remove the two listener 
+* Remove the two listener
 *
 */
 function removeListener() {
diff --git a/popup/config.js b/popup/config.js
index ed6f1e5..35428a0 100644
--- a/popup/config.js
+++ b/popup/config.js
@@ -28,8 +28,8 @@ window.onload = function() {
 	let config = JSON.parse(localStorage.getItem("config"));
 	if (config.debug_mode) document.getElementById("debug_mode").checked = true;
 
-	if (typeof config.show_comments === 'undefined') document.getElementById("show_comments").checked = true;	
-	else if (config.show_comments) document.getElementById("show_comments").checked = true;	
+	if (typeof config.show_comments === 'undefined') document.getElementById("show_comments").checked = true;
+	else if (config.show_comments) document.getElementById("show_comments").checked = true;
 	else show_comments=false;
 
 	if (config.use_url_contains) {
@@ -50,11 +50,11 @@ window.onload = function() {
 	document.getElementById('exit_parameters_screen_button').addEventListener('click',function (e) {hideParametersScreen();});
 	
 	started = localStorage.getItem("started");
-	if (started==="on") document.getElementById("start_img").src = "img/stop.png";	
+	if (started==="on") document.getElementById("start_img").src = "img/stop.png";
 	
 	document.getElementById('show_comments').addEventListener('click',function (e) {showCommentsClick();});
 	document.getElementById('use_url_contains').addEventListener('click',function (e) {useUrlContainsClick();});
-	reshapeTable();	
+	reshapeTable();
 } 
 
 
@@ -87,23 +87,29 @@ function useUrlContainsClick() {
 
 
 /**
-* Add a new configuration line on the UI 
+* Add a new configuration line on the UI
 **/
 function appendLine(url_contains,action,header_name,header_value,comment,apply_on,status) {
   let html = "<td";
   if (!use_url_contains) html=html+" hidden";
   html = html + "><input class=\""+ input_field_style+ "\" id=\"url_contains"+ line_number + "\"></input></td>";
-  html =  html + "<td><select class=\"form_control select_field\" id=\"select_action" + line_number + "\" disable=false><option value=\"add\">Add</option><option value=\"modify\">Modify</option><option value=\"delete\">Delete</option></select></td>";
+  html =  html + "<td><select class=\"form_control select_field\" id=\"select_action" + line_number 
+			   + "\" disable=false><option value=\"add\">Add</option><option value=\"modify\">Modify</option><option value=\"delete\">Delete</option></select></td>";
   html = html + "<td><input class=\"" + input_field_style + "\" id=\"header_name"+ line_number + "\"></input></td>";
   html = html + "<td><input class=\"" + input_field_style + "\" id=\"header_value"+ line_number + "\"></input></td>";
   html = html + "<td";
   if (!show_comments) html=html+" hidden";
   html = html + "><input class=\""+ input_field_style + "\" id=\"comment"+ line_number + "\"></input></td>";
-  html = html + "<td><select class=\"form_control select_field\" id=\"apply_on" + line_number + "\"><option value=\"req\"> Request </option><option value=\"res\">Response</option></select></td>";
-  html = html +  "<td><a href=\"#\" title=\"Activate/Descativate rule\" id=\"activate_button" + line_number + "\" class=\"btn btn-primary btn-sm\">ON  <span class=\"glyphicon glyphicon-ok\"></span></a></td>";
-  html = html +  "<td><a href=\"#\" title=\"Move line up\" id=\"up_button" + line_number + "\" class=\"btn btn-default btn-sm\"> <span class=\"glyphicon glyphicon-arrow-up\"></span></a></td>"; 
-  html = html +  "<td><a href=\"#\" title=\"Move line down\" id=\"down_button" + line_number + "\" class=\"btn btn-default btn-sm\"> <span class=\"glyphicon glyphicon-arrow-down\"></span></a></td>"; 
-  html = html +  "<td><a href=\"#\" title=\"Delete line\" id=\"delete_button" + line_number + "\" class=\"btn btn-primary btn-sm\"> <span class=\"glyphicon glyphicon-trash\"></span></a></td>"; 
+  html = html + "<td><select class=\"form_control select_field\" id=\"apply_on"
+			  + line_number + "\"><option value=\"req\"> Request </option><option value=\"res\">Response</option></select></td>";
+  html = html +  "<td><a href=\"#\" title=\"Activate/Descativate rule\" id=\"activate_button" 
+			  + line_number + "\" class=\"btn btn-primary btn-sm\">ON  <span class=\"glyphicon glyphicon-ok\"></span></a></td>";
+  html = html +  "<td><a href=\"#\" title=\"Move line up\" id=\"up_button"
+			  + line_number + "\" class=\"btn btn-default btn-sm\"> <span class=\"glyphicon glyphicon-arrow-up\"></span></a></td>";
+  html = html +  "<td><a href=\"#\" title=\"Move line down\" id=\"down_button" 
+		      + line_number + "\" class=\"btn btn-default btn-sm\"> <span class=\"glyphicon glyphicon-arrow-down\"></span></a></td>";
+  html = html +  "<td><a href=\"#\" title=\"Delete line\" id=\"delete_button"
+			  + line_number + "\" class=\"btn btn-primary btn-sm\"> <span class=\"glyphicon glyphicon-trash\"></span></a></td>";
 
   let newTR = document.createElement("tr");
   newTR.id="line" + line_number;
@@ -147,7 +153,7 @@ function getButtonStatus(button) {
 
 function switchActivateButton(button_number) {
   const activate_button = document.getElementById("activate_button"+button_number);
-  // Button is ON 
+  // Button is ON
   if (getButtonStatus(activate_button)==="on") setButtonStatus(activate_button,"off");
   // Button is OFF
   else setButtonStatus(activate_button,"on");
@@ -173,7 +179,7 @@ function reshapeTable() {
 
 
   for (let i=0;i<tr_elements.length;i++) { 
-    tr_elements[i].childNodes[4].childNodes[0].className=input_field_style; 
+    tr_elements[i].childNodes[4].childNodes[0].className=input_field_style;
     tr_elements[i].childNodes[4].hidden = (!show_comments);
     tr_elements[i].childNodes[3].childNodes[0].className=input_field_style;
     tr_elements[i].childNodes[2].childNodes[0].className=input_field_style;
@@ -181,7 +187,7 @@ function reshapeTable() {
     tr_elements[i].childNodes[0].hidden = (!use_url_contains);
   }
   th_elements[4].hidden = (!show_comments);
-  th_elements[0].hidden = (!use_url_contains);	
+  th_elements[0].hidden = (!use_url_contains);
 }
 
 
@@ -205,10 +211,11 @@ function create_configuration_data() {
     const status = getButtonStatus(tr_elements[i].childNodes[6].childNodes[0]);
     headers.push({url_contains:url_contains,action:action,header_name:header_name,header_value:header_value,comment:comment,apply_on:apply_on,status:status});
   }
-  if (document.getElementById("debug_mode").checked) debug_mode=true ;
-  if (document.getElementById("show_comments").checked) show_comments=true ;	
-  if (document.getElementById("use_url_contains").checked) use_url_contains=true ;
-  let to_export = {format_version:"1.2",target_page:document.getElementById('targetPage').value,headers:headers,debug_mode:debug_mode,show_comments:show_comments,use_url_contains:use_url_contains};
+  if (document.getElementById("debug_mode").checked) debug_mode=true;
+  if (document.getElementById("show_comments").checked) show_comments=true;	
+  if (document.getElementById("use_url_contains").checked) use_url_contains=true;
+  let to_export = {format_version:"1.2",target_page:document.getElementById('targetPage').value,headers:headers,
+				  debug_mode:debug_mode,show_comments:show_comments,use_url_contains:use_url_contains};
   return JSON.stringify(to_export);
 }
 
@@ -228,7 +235,7 @@ function isTargetValid(target) {
   if (target===" ") return true;
   if (target==="*") return true;
   let targets=target.split(";");
-  for (i in targets) {
+  for (let i in targets) {
     if (!targets[i].match("(http|https|[\*]):\/\/([\*][\.][^\*]*|[^\*]*|[\*])\/")) return false;
   }
   return true;
@@ -242,7 +249,7 @@ function isTargetValid(target) {
 
 function saveData() {
   if (!isTargetValid(document.getElementById('targetPage').value)) alert("Warning: Url patterns are invalid");
-  localStorage.setItem("config",create_configuration_data()); 
+  localStorage.setItem("config",create_configuration_data());
   chrome.runtime.sendMessage("reload");
   return true;
 }
@@ -256,7 +263,7 @@ function exportData() {
   // Create file data
   let to_export= create_configuration_data();
 	
-  // Create file to save 
+  // Create file to save
   let a         = document.createElement('a');
   a.href        = 'data:attachment/json,' +  encodeURIComponent(to_export);
   a.target      = 'download';
@@ -300,7 +307,7 @@ function readSingleFile(e) {
   let reader = new FileReader();
   reader.onload = function(e) {
     let contents = e.target.result;
-    let config="";	
+    let config="";
     try {
       config = JSON.parse(contents);
       // check file format
@@ -326,9 +333,9 @@ function readSingleFile(e) {
 	      config.use_url_contains=false;
         }
 
-        // store the conf in the local storage 
+        // store the conf in the local storage
         localStorage.setItem("config",JSON.stringify(config));
-        // load the new conf 
+        // load the new conf
         chrome.runtime.sendMessage("reload");
         // reload the configuration page with the new conf
         document.location.href="config.html";
@@ -338,17 +345,19 @@ function readSingleFile(e) {
         if (config[0].action) {
 	  let headers = [];
 	  for (let line_to_load of config) {
-            var enabled = "off"; 
+            var enabled = "off";
             if (line_to_load.enabled) enabled = "on";
-	    if (line_to_load.action==="Filter") line_to_load.action="delete";			   headers.push({url_contains:"",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});
+	    if (line_to_load.action==="Filter") line_to_load.action="delete";			   
+		headers.push({url_contains:"",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});
 	  }
 	  let to_load = {format_version:"1.2",target_page:"",headers:headers,debug_mode:false,show_comments:true,use_url_contains:false};
-          // store the conf in the local storage 
+          // store the conf in the local storage
           localStorage.setItem("config",JSON.stringify(to_load));
           // load the new conf 
 	  chrome.runtime.sendMessage("reload");
           // reload the configuration page with the new conf
-	  document.location.href="config.html";	
+	  document.location.href="config.html";
         }
         else  alert("invalid file format");
       }
@@ -394,7 +403,7 @@ function invertLine(line1, line2) {
   // if a line does not exist , do nothing
   if ((line1===0)||(line2===0)||(line1>=line_number)||(line2>=line_number)) return;
 
-  // Save data for line 1 
+  // Save data for line 1
   const select_action1= document.getElementById("select_action"+line1).value;
   const url_contains1 = document.getElementById("url_contains"+line1).value;
   const header_name1 = document.getElementById("header_name"+line1).value;
@@ -431,7 +440,7 @@ function startModify() {
       localStorage.setItem("started","on");
       chrome.runtime.sendMessage("on");
       started = "on";
-      document.getElementById("start_img").src = "img/stop.png";		
+      document.getElementById("start_img").src = "img/stop.png";	
   }
   else {
     localStorage.setItem("started","off");
diff --git a/popup/menu.js b/popup/menu.js
index f7ab89d..b678a74 100644
--- a/popup/menu.js
+++ b/popup/menu.js
@@ -11,24 +11,22 @@
 
 var started = "off";
 
-
 window.onload = function() {
 	document.getElementById('config').addEventListener('click',function (e) {start_config();});
 	document.getElementById('start_stop').addEventListener('click',function (e) {start_modify();});
 	started = localStorage.getItem("started");
-	if (started=="on") document.getElementById("start_stop").value = "Stop";	
-} ;
-
+	if (started==="on") document.getElementById("start_stop").value = "Stop";
+}
 
 
 function start_modify()
 	{
-	if (started=="off") 
+	if (started==="off")
 		{
 		localStorage.setItem("started","on");
 		chrome.runtime.sendMessage("on");
 		started = "on";
-		document.getElementById("start_stop").value = "Stop";		
+		document.getElementById("start_stop").value = "Stop";
 		}
 	else 
 		{
@@ -38,7 +36,7 @@ function start_modify()
 		document.getElementById("start_stop").value = "Start";
 		}
 
-	// if exists reload config tab , to get the start/stop information correct 
+	// if exists reload config tab , to get the start/stop information correct
       chrome.tabs.query({currentWindow: true},reloadConfigTab);
 	
 	}
@@ -48,15 +46,13 @@ function reloadConfigTab(tabs)
 	{
 	var config_tab;
 	
-	// search for config tab 
-	for (let tab of tabs) 
+	// search for config tab
+	for (let tab of tabs)
 		{
 			if (tab.url.startsWith(chrome.extension.getURL(""))) config_tab = tab;
 		}
-		
-	// config tab exists , reload it 
+	// config tab exists , reload it
         if (config_tab) chrome.tabs.reload(config_tab.id);
-
 	}
 
 	
@@ -71,13 +67,13 @@ function loadConfigTab(tabs)
 	{
 	var config_tab;
 	
-	// search for config tab 
-	for (let tab of tabs) 
+	// search for config tab
+	for (let tab of tabs)
 		{
 			if (tab.url.startsWith(chrome.extension.getURL(""))) config_tab = tab;
 		}
 		
-	// config tab exits , put the focus on it 
+	// config tab exits , put the focus on it
         if (config_tab) chrome.tabs.update(config_tab.id,{active:true})
 
 	// else create a new tab
-- 
GitLab