Skip to content
Snippets Groups Projects
Commit 58ef8ce5 authored by didierfred's avatar didierfred
Browse files

Finalise export function and begin coding import fonction

parent 356325d4
No related branches found
No related tags found
No related merge requests found
......@@ -38,6 +38,9 @@
<input type="button" id="save_button" value="SAVE"></input>
<input type="button" id="export_button" value="EXPORT"></input>
<input type="button" id="import_button" value="IMPORT"></input>
<iframe id="download" width="0" height="0" frameBorder="0">
</iframe>
</center>
<script type="text/javascript" src="config.js"> </script>
......
......@@ -48,8 +48,6 @@ line_number++;
}
function save_data ()
{
var tr_elements = document.querySelectorAll("#config_tab tr");
......@@ -83,23 +81,72 @@ function export_data()
headers.push({action:action,header_name:header_name,header_value:header_value,status:status});
}
var to_export = {format_version:"1.0",targetPage:document.getElementById('targetPage').value,headers:headers};
var to_export = {format_version:"1.0",target_page:document.getElementById('targetPage').value,headers:headers};
console.log(JSON.stringify(to_export));
// Create file to save
var a = document.createElement('a');
a.href = 'data:attachment/json,' + encodeURIComponent(JSON.stringify(to_export));
a.target = '_blank';
a.target = 'download';
a.download = 'SimpleModifyHeader.conf';
document.body.appendChild(a);
// use iframe "download" to put the link (in order not to be redirect in the parent frame)
var myf = document.getElementById("download");
myf = myf.contentWindow.document || myf.contentDocument;
myf.body.appendChild(a);
a.click();
}
function import_data(evt)
{
var files = evt.target.files;
// create an input field in the iframe
var input = document.createElement("input");
input.type="file";
input.addEventListener('change', readSingleFile, false);
var myf = document.getElementById("download");
myf = myf.contentWindow.document || myf.contentDocument;
myf.body.appendChild(input);
input.click();
}
function readSingleFile(e)
{
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e)
{
var contents = e.target.result;
var config="";
try
{
config = JSON.parse(contents);
if (config.format_version && config.target_page)
{
alert("ok");
// store the conf in the local storage
localStorage.setItem("config",contents);
// 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);
}
function delete_line(line_number_to_delete)
{
if (line_number_to_delete != line_number)
......
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