From 807422d575c480e71116eed9b710e59d70e083d4 Mon Sep 17 00:00:00 2001
From: didierfred <didierfred@gmail.com>
Date: Wed, 7 Feb 2018 19:00:54 +0100
Subject: [PATCH] add modify header format support and check url pattern

---
 background.js     |  5 ++++-
 popup/config.html |  8 +++++---
 popup/config.js   | 50 +++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/background.js b/background.js
index e910acb..f5b552e 100644
--- a/background.js
+++ b/background.js
@@ -148,8 +148,11 @@ function notify(message)
 */
 function addListener()
 	{
+	var target = config.target_page;
+	if ((target=="*")||(target=="")||(target==" "))target="<all_urls>";
+	
 	browser.webRequest.onBeforeSendHeaders.addListener(rewriteHeader,
-                                          {urls: [config.target_page]},
+                                          {urls: [target]},
                                           ["blocking", "requestHeaders"]);
 	}
 
diff --git a/popup/config.html b/popup/config.html
index 58fc168..bd2224c 100644
--- a/popup/config.html
+++ b/popup/config.html
@@ -53,7 +53,7 @@ color: #008CBA;
 
 <td width="450">
    
-  Url Pattern :  <input size="50" id="targetPage"  type="text" value=""></input> 
+  Url Pattern* :  <input size="50" id="targetPage"  type="text" value=""></input> 
 </td>
 
 <td width="270" align="right">
@@ -78,12 +78,14 @@ color: #008CBA;
 <br>
 <input type="button" class="button" style="width:100px" id="add_button" value="Add new line" ></input>
 <input type="button" class="button" style="width:100px" id="save_button" value="Save"></input>
-
+<br>
+<br>
+</center>
+</i> * Informations on url pattern can be found <a href="https://developer.chrome.com/extensions/match_patterns" target="_blank"> here  </a>  </i>
 
 <iframe id="download" width="0" height="0" frameBorder="0"> 
 </iframe>
 
-</center>
 <br>
 
  <script type="text/javascript" src="config.js"> </script>
diff --git a/popup/config.js b/popup/config.js
index 5f5a075..9d9c60b 100644
--- a/popup/config.js
+++ b/popup/config.js
@@ -69,8 +69,23 @@ function create_configuration_data()
 	return JSON.stringify(to_export);
 }
 
+// check if url pattern is valid
+function isTargetValid(target)
+	{
+		if (target=="") return true;
+		if (target==" ") return true;
+		if (target=="*") return true;
+		return target.match("(http|https):\/\/.[^\*]*\/");
+	}
+
+
 function save_data () 
 	{
+	if (!isTargetValid(document.getElementById('targetPage').value))
+		{
+			alert("Url pattern  is invalid");
+			return;
+		}
 	localStorage.setItem("config",create_configuration_data());
 	browser.runtime.sendMessage("reload");
 	}
@@ -113,18 +128,19 @@ function import_data(evt)
 
 function readSingleFile(e) 
 	{
-	  var file = e.target.files[0];
-	  if (!file) {
+	var file = e.target.files[0];
+	if (!file) {
     		return;
   		}
-  	  var reader = new FileReader();
-  	  reader.onload = function(e) 
+  	var reader = new FileReader();
+  	reader.onload = function(e) 
 		{
     	var contents = e.target.result;
 		var config="";	
 		try
 			{
 			config = JSON.parse(contents);
+			// check file format
 			if (config.format_version && config.target_page)
 				{
 				// store the conf in the local storage 
@@ -134,14 +150,36 @@ function readSingleFile(e)
 				// reload the configuration page with the new conf
 				document.location.href="config.html";
 				}
-			else alert("invalid file format");
+			else 
+				{
+				// try modify header add-on file format  : array of {action,name,value,comment,enabled}
+				if (config[0].action)
+					{
+					var headers = [];
+					for (var line_to_load of config)
+						{
+						var enabled = "off"; 
+						if (line_to_load.enabled) enabled = "on"
+						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,status:enabled});
+						}
+					var to_load = {format_version:"1.0",target_page:"",headers:headers};
+					
+					// store the conf in the local storage 
+					localStorage.setItem("config",JSON.stringify(to_load));
+					// load the new conf 
+					browser.runtime.sendMessage("reload");
+					// reload the configuration page with the new conf
+					document.location.href="config.html";	
+					}
+				else  alert("invalid file format");
+				}
 			}
 		catch(error) 
 			{
   			console.log(error);
 			alert("Invalid file format");
 			}
-		alert(contents);
   		};
   	reader.readAsText(file);
 	}
-- 
GitLab