diff --git a/manifest.json b/manifest.json index 7422ba46c9d656e7c7b07f73b359fe0ed6ba58ef..c4ffae3f72ac63aef4c0413e2ea4bbbdf36c1906 100644 --- a/manifest.json +++ b/manifest.json @@ -20,7 +20,7 @@ "browser_action": { "default_icon": "icons/modify-32.png", "default_title": "Simple Modify Headers", - "default_popup": "popup/config.html" + "default_popup": "popup/menu.html" } } diff --git a/popup/config.html b/popup/config.html index ac24af8355f4cf6631c14941c3ce8ebee8513eb8..f0b21fc6acb79148bdbea5cc2e8dbd93b00a97c8 100644 --- a/popup/config.html +++ b/popup/config.html @@ -37,10 +37,14 @@ <input type="button" id="add_button" value="ADD NEW LINE" ></input> <input type="button" id="save_button" value="SAVE"></input> <input type="button" id="export_button" value="EXPORT"></input> -<input type="file" id="import_button" value="IMPORT"></input> +<input type="button" id="import_button" value="IMPORT"></input> + +<iframe id="download" width="0" height="0" frameBorder="0"> +</iframe> + </center> <br> -<a href="export_import.html" target="_blank"> Export/Import</a> + <script type="text/javascript" src="config.js"> </script> </body> diff --git a/popup/config.js b/popup/config.js index f2bcc8a0870d242576be7bfd6915e5dbd392af13..35f2a49c18ceddd1271614574268de1ff3802591 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,32 +81,74 @@ 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; - var reader = new FileReader(); - reader.addEventListener('load', function() { - console.log("files[0].name=" + files[0].name); - alert('Contenu du fichier : "' + files[0].name + '" :\n\n' + reader.result); - }); - reader.readAsText(files[0]); + // 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) diff --git a/popup/menu.html b/popup/menu.html new file mode 100644 index 0000000000000000000000000000000000000000..e89ad4ae9bbd8dd6523eb7feca09e72e2952bdcc --- /dev/null +++ b/popup/menu.html @@ -0,0 +1,20 @@ +<!DOCTYPE html> + +<html> + <head> + <meta charset="utf-8"> + + </head> + +<body> + + +<input type="button" id="start_stop" value="Start" ></input> +<input type="button" id="config" value="Configure"></input> + +</center> + <script type="text/javascript" src="menu.js"> </script> + +</body> + +</html> diff --git a/popup/menu.js b/popup/menu.js new file mode 100644 index 0000000000000000000000000000000000000000..dcb4a1710a4016fe56222c38003725a8ff4e309e --- /dev/null +++ b/popup/menu.js @@ -0,0 +1,46 @@ + + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * @author didierfred@gmail.com + * @version 0.1 + */ + + +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"; +} ; + + + +function start_modify() + { + if (started=="off") + { + localStorage.setItem("started","on"); + browser.runtime.sendMessage("on"); + started = "on"; + document.getElementById("start_stop").value = "Stop"; + } + else + { + localStorage.setItem("started","off"); + browser.runtime.sendMessage("off"); + started = "off"; + document.getElementById("start_stop").value = "Start"; + } + + } + +function start_config() + { + var strWindowFeatures = "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=800,height=600"; + window.open("config.html","Simple Modify Headers",strWindowFeatures); + }