Skip to content
Snippets Groups Projects
Commit 6d9f7e9a authored by didierfred's avatar didierfred
Browse files

refactoring for test

parent 3b3d36d4
No related branches found
No related tags found
No related merge requests found
...@@ -9,21 +9,40 @@ ...@@ -9,21 +9,40 @@
*/ */
let line_number = 1; let line_number;
let started = "off"; let started;
let show_comments = true; let show_comments;
let use_url_contains = false; let use_url_contains;
let url_contains_field_size= 18; let url_contains_field_size;
let header_name_field_size= 20; let header_name_field_size;
let header_value_field_size= 28; let header_value_field_size;
let comments_field_size= 28; let comments_field_size;
let input_field_style;
let input_field_style="form_control input_field_small";
window.onload = function() { window.onload = function() {
initConfigurationPage();
}
function initGlobalValue()
{
line_number = 1;
started = "off";
show_comments = true;
use_url_contains = false;
url_contains_field_size= 18;
header_name_field_size= 20;
header_value_field_size= 28;
comments_field_size= 28;
input_field_style="form_control input_field_small"
}
function initConfigurationPage() {
initGlobalValue();
// load configuration from local storage // load configuration from local storage
let config = JSON.parse(localStorage.getItem("config")); let config = JSON.parse(localStorage.getItem("config"));
if (config.debug_mode) document.getElementById("debug_mode").checked = true; if (config.debug_mode) document.getElementById("debug_mode").checked = true;
...@@ -295,9 +314,9 @@ function importData(evt) { ...@@ -295,9 +314,9 @@ function importData(evt) {
} }
/** /**
* Import data from a file * Import configuration from a file
* *
* If format is not recognized , try modify header add-an file format *
* *
**/ **/
...@@ -305,69 +324,73 @@ function readSingleFile(e) { ...@@ -305,69 +324,73 @@ function readSingleFile(e) {
let file = e.target.files[0]; let file = e.target.files[0];
if (!file) return; if (!file) return;
let reader = new FileReader(); let reader = new FileReader();
reader.onload = function(e) { reader.onload = function(e) {
let contents = e.target.result; loadConfiguration(e.target.result);
let config=""; }
try { reader.readAsText(file);
config = JSON.parse(contents); }
// check file format
if (config.format_version) {
// if format file is 1.0 , need to add the apply_on and url_contains value to translate in format 1.2
if (config.format_version==="1.0") {
config.format_version="1.2";
for (let line of config.headers) {
line.apply_on="req";
line.url_contains="";
}
config.debug_mode=false;
config.show_comments=true;
config.use_url_contains=false;
}
// if format file is 1.1 , need to add url_contains value to translate in format 1.2 /**
if (config.format_version==="1.1") { * Load configuration from a string
config.format_version="1.2"; * If format is not recognized , try modify header add-an file format
for (let line of config.headers) line.url_contains=""; **/
config.show_comments=true; function loadConfiguration(configuration) {
config.use_url_contains=false; let config="";
try {
config = JSON.parse(configuration);
// check file format
if (config.format_version) {
// if format file is 1.0 , need to add the apply_on and url_contains value to translate in format 1.2
if (config.format_version==="1.0") {
config.format_version="1.2";
for (let line of config.headers) {
line.apply_on="req";
line.url_contains="";
} }
config.debug_mode=false;
// store the conf in the local storage config.show_comments=true;
localStorage.setItem("config",JSON.stringify(config)); config.use_url_contains=false;
// load the new conf
chrome.runtime.sendMessage("reload");
// reload the configuration page with the new conf
document.location.href="config.html";
} }
else { // if format file is 1.1 , need to add url_contains value to translate in format 1.2
// try modify header add-on file format : array of {action,name,value,comment,enabled} if (config.format_version==="1.1") {
if (config[0].action) { config.format_version="1.2";
let headers = []; for (let line of config.headers) line.url_contains="";
for (let line_to_load of config) { config.show_comments=true;
var enabled = "off"; config.use_url_contains=false;
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});
}
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
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";
}
else alert("invalid file format");
} }
} }
catch(error) { else {
console.log(error); // try modify header add-on file format : array of {action,name,value,comment,enabled}
alert("Invalid file format"); if (config[0].action) {
let headers = [];
for (let 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({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});
}
config = {format_version:"1.2",target_page:"",headers:headers,debug_mode:false,show_comments:true,use_url_contains:false};
}
else {
alert("invalid file format");
return;
}
} }
}; }
reader.readAsText(file); catch(error) {
console.log(error);
alert("Invalid file format");
return;
}
// store the conf in the local storage
localStorage.setItem("config",JSON.stringify(config));
// load the new conf
chrome.runtime.sendMessage("reload");
// reload the configuration page with the new conf
document.location.href="config.html";
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment