From 58ef8ce5b391a6d8e154333aeca678b0752fca2f Mon Sep 17 00:00:00 2001 From: didierfred <didierfred@gmail.com> Date: Sun, 4 Feb 2018 22:06:00 +0100 Subject: [PATCH] Finalise export function and begin coding import fonction --- popup/config.html | 3 +++ popup/config.js | 59 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/popup/config.html b/popup/config.html index 07bbd7a..9ce6595 100644 --- a/popup/config.html +++ b/popup/config.html @@ -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> diff --git a/popup/config.js b/popup/config.js index d924a4e..e197843 100644 --- a/popup/config.js +++ b/popup/config.js @@ -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) -- GitLab