Skip to content
Snippets Groups Projects
Commit 970feefd authored by didierfred's avatar didierfred
Browse files

use chrome.storage.local to correct history bug

parent 891d9bdf
No related branches found
No related tags found
No related merge requests found
......@@ -11,71 +11,84 @@
"use strict";
let config;
let started = "off";
let started = 'off';
let debug_mode = false;
loadFromBrowserStorage(['config','started'],function(result) {
config = result.config;
// if old storage method
if (config===undefined) loadConfigurationFromLocalStorage();
else started = result.started;
// if configuration exist
if (localStorage.getItem('config')) {
console.log("Load standard config");
config= JSON.parse(localStorage.getItem('config'));
// If config 1.0 (Simple Modify headers V1.2) , save to format 1.1
if (config.format_version==="1.0") {
config.format_version="1.1";
for (let line of config.headers) line.apply_on="req";
config.debug_mode=false;
console.log("save new config"+JSON.stringify(config));
localStorage.setItem("config",JSON.stringify(config));
if (started==='on') {
addListener();
chrome.browserAction.setIcon({ path: 'icons/modify-green-32.png'});
}
// If config 1.1 (Simple Modify headers V1.3 to version 1.5) , save to format 1.2
if (config.format_version==="1.1") {
config.format_version="1.2";
for (let line of config.headers) line.url_contains="";
config.use_url_contains=false;
console.log("save new config"+JSON.stringify(config));
localStorage.setItem("config",JSON.stringify(config));
else if (started !== 'off') {
started = 'off';
storeInBrowserStorage({started:'off'});
}
}
else {
// else check if old config exist (Simple Modify headers V1.1)
if (localStorage.getItem('targetPage')&& localStorage.getItem('modifyTable')) {
console.log("Load old config");
let headers = [];
let modifyTable=JSON.parse(localStorage.getItem("modifyTable"));
for (const to_modify of modifyTable) {
headers.push({action:to_modify[0],url_contains:"",header_name:to_modify[1],header_value:to_modify[2],comment:"",apply_on:"req",status:to_modify[3]});
// listen for change in configuration or start/stop
chrome.runtime.onMessage.addListener(notify);
});
function loadConfigurationFromLocalStorage() {
// if configuration exist
if (localStorage.getItem('config')) {
console.log("Load standard config");
config= JSON.parse(localStorage.getItem('config'));
// If config 1.0 (Simple Modify headers V1.2) , save to format 1.1
if (config.format_version==="1.0") {
config.format_version="1.2";
for (let line of config.headers) line.apply_on="req";
config.debug_mode=false;
console.log("save new config"+JSON.stringify(config));
}
// If config 1.1 (Simple Modify headers V1.3 to version 1.5) , save to format 1.2
if (config.format_version==="1.1") {
config.format_version="1.2";
for (let line of config.headers) line.url_contains="";
config.use_url_contains=false;
console.log("save new config"+JSON.stringify(config));
}
config = {format_version:"1.1",target_page:localStorage.getItem('targetPage'),headers:headers,debug_mode:false};
// save old config in new format
localStorage.setItem("config",JSON.stringify(config));
}
//else no config exists, create a default one
else {
console.log("Load default config");
let headers = [];
headers.push({url_contains:"",action:"add",header_name:"test-header-name",header_value:"test-header-value",comment:"test",apply_on:"req",status:"on"});
config = {format_version:"1.1",target_page:"https://httpbin.org/*",headers:headers,debug_mode:false};
// save configuration
localStorage.setItem("config",JSON.stringify(config));
// else check if old config exist (Simple Modify headers V1.1)
if (localStorage.getItem('targetPage')&& localStorage.getItem('modifyTable')) {
console.log("Load old config");
let headers = [];
let modifyTable=JSON.parse(localStorage.getItem("modifyTable"));
for (const to_modify of modifyTable) {
headers.push({action:to_modify[0],url_contains:"",header_name:to_modify[1],header_value:to_modify[2],comment:"",apply_on:"req",status:to_modify[3]});
}
config = {format_version:"1.1",target_page:localStorage.getItem('targetPage'),headers:headers,debug_mode:false};
}
//else no config exists, create a default one
else {
console.log("Load default config");
let headers = [];
headers.push({url_contains:"",action:"add",header_name:"test-header-name",header_value:"test-header-value",comment:"test",apply_on:"req",status:"on"});
config = {format_version:"1.1",target_page:"https://httpbin.org/*",headers:headers,debug_mode:false};
}
}
}
// If no started value stored , use a default one
if (!localStorage.getItem('started')) localStorage.setItem('started',started);
else started = localStorage.getItem('started');
storeInBrowserStorage({config:JSON.stringify(config)});
started=localStorage.getItem('started');
if (started!==undefined) storeInBrowserStorage({started:started});
}
if (started==="on") {
addListener();
chrome.browserAction.setIcon({ path: "icons/modify-green-32.png"});
function loadFromBrowserStorage(item,callback_function) {
chrome.storage.local.get(item, callback_function);
}
// listen for change in configuration or start/stop
chrome.runtime.onMessage.addListener(notify);
function storeInBrowserStorage(item,callback_function) {
chrome.storage.local.set(item,callback_function);
}
/*
......@@ -180,11 +193,13 @@ function rewriteResponseHeader(e) {
function notify(message) {
if (message==="reload") {
if (config.debug_mode) log("Reload configuration");
config=JSON.parse(localStorage.getItem("config"));
if (started==="on") {
removeListener();
addListener();
}
loadFromBrowserStorage(['config'],function (result) {
config=JSON.parse(result.config);
if (started==="on") {
removeListener();
addListener();
}
});
}
else if (message==="off") {
removeListener();
......
......@@ -26,6 +26,21 @@ window.onload = function() {
}
function loadFromBrowserStorage(item,callback_function) {
chrome.storage.local.get(item, callback_function);
}
function storeInBrowserStorage(item,callback_function) {
chrome.storage.local.set(item,callback_function);
}
function log(message) {
console.log(new Date() + " SimpleModifyHeader : " + message);
}
function initGlobalValue()
{
line_number = 1;
......@@ -44,36 +59,41 @@ function initConfigurationPage() {
initGlobalValue();
// load configuration from local storage
let config = JSON.parse(localStorage.getItem("config"));
if (config.debug_mode) document.getElementById("debug_mode").checked = true;
if (typeof config.show_comments === 'undefined') document.getElementById("show_comments").checked = true;
else if (config.show_comments) document.getElementById("show_comments").checked = true;
else show_comments=false;
if (config.use_url_contains) {
document.getElementById("use_url_contains").checked = true;
use_url_contains=true;
}
loadFromBrowserStorage(['config'],function (result) {
config = JSON.parse(result.config);
if (config.debug_mode) document.getElementById("debug_mode").checked = true;
if (typeof config.show_comments === 'undefined') document.getElementById("show_comments").checked = true;
else if (config.show_comments) document.getElementById("show_comments").checked = true;
else show_comments=false;
if (config.use_url_contains) {
document.getElementById("use_url_contains").checked = true;
use_url_contains=true;
}
for (let to_add of config.headers) appendLine(to_add.url_contains,to_add.action,to_add.header_name,to_add.header_value,to_add.comment,to_add.apply_on,to_add.status);
document.getElementById('save_button').addEventListener('click',function (e) {saveData();});
document.getElementById('export_button').addEventListener('click',function (e) {exportData();});
document.getElementById('import_button').addEventListener('click',function (e) {importData(e);});
document.getElementById('parameters_button').addEventListener('click',function (e) {showParametersScreen();});
document.getElementById('add_button').addEventListener('click',function (e) {appendLine("","add","-","-","","req","on");});
document.getElementById('start_img').addEventListener('click',function (e) {startModify();});
document.getElementById('targetPage').value=config.target_page;
checkTargetPageField();
document.getElementById('targetPage').addEventListener('keyup',function (e) {checkTargetPageField();});
document.getElementById('exit_parameters_screen_button').addEventListener('click',function (e) {hideParametersScreen();});
for (let to_add of config.headers) appendLine(to_add.url_contains,to_add.action,to_add.header_name,to_add.header_value,to_add.comment,to_add.apply_on,to_add.status);
document.getElementById('save_button').addEventListener('click',function (e) {saveData();});
document.getElementById('export_button').addEventListener('click',function (e) {exportData();});
document.getElementById('import_button').addEventListener('click',function (e) {importData(e);});
document.getElementById('parameters_button').addEventListener('click',function (e) {showParametersScreen();});
document.getElementById('add_button').addEventListener('click',function (e) {appendLine("","add","-","-","","req","on");});
document.getElementById('start_img').addEventListener('click',function (e) {startModify();});
document.getElementById('targetPage').value=config.target_page;
checkTargetPageField();
document.getElementById('targetPage').addEventListener('keyup',function (e) {checkTargetPageField();});
document.getElementById('exit_parameters_screen_button').addEventListener('click',function (e) {hideParametersScreen();});
loadFromBrowserStorage(['started'], function (result) {
started = result.started;
if (started==="on") document.getElementById("start_img").src = "img/stop.png";
});
started = localStorage.getItem("started");
if (started==="on") document.getElementById("start_img").src = "img/stop.png";
document.getElementById('show_comments').addEventListener('click',function (e) {showCommentsClick();});
document.getElementById('use_url_contains').addEventListener('click',function (e) {useUrlContainsClick();});
reshapeTable();
document.getElementById('show_comments').addEventListener('click',function (e) {showCommentsClick();});
document.getElementById('use_url_contains').addEventListener('click',function (e) {useUrlContainsClick();});
reshapeTable();
});
}
......@@ -268,8 +288,9 @@ function isTargetValid(target) {
function saveData() {
if (!isTargetValid(document.getElementById('targetPage').value)) alert("Warning: Url patterns are invalid");
localStorage.setItem("config",create_configuration_data());
chrome.runtime.sendMessage("reload");
storeInBrowserStorage({config:create_configuration_data()},function() {
chrome.runtime.sendMessage("reload");
});
return true;
}
......@@ -369,10 +390,12 @@ function loadConfiguration(configuration) {
}
// store the conf in the local storage
localStorage.setItem("config",JSON.stringify(config));
storeInBrowserStorage({config:JSON.stringify(config)},function() {
// load the new conf
reloadConfigPage();
});
// load the new conf
reloadConfigPage();
}
function convertConfigurationFormat1dot0ToCurrentFormat(config) {
......@@ -469,15 +492,17 @@ function invertLine(line1, line2) {
function startModify() {
if (started==="off") {
saveData();
localStorage.setItem("started","on");
chrome.runtime.sendMessage("on");
started = "on";
document.getElementById("start_img").src = "img/stop.png";
storeInBrowserStorage({started:'on'},function() {
chrome.runtime.sendMessage("on");
started = "on";
document.getElementById("start_img").src = "img/stop.png";
});
}
else {
localStorage.setItem("started","off");
chrome.runtime.sendMessage("off");
started = "off";
document.getElementById("start_img").src = "img/start.png";
storeInBrowserStorage({started:'off'},function() {
chrome.runtime.sendMessage("off");
started = "off";
document.getElementById("start_img").src = "img/start.png";
});
}
}
......@@ -11,35 +11,53 @@
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";
loadFromBrowserStorage(['started'],function(result) {
started =result.started;
if (started==="on") document.getElementById("start_stop").value = "Stop";
});
}
function start_modify()
{
if (started==="off")
{
localStorage.setItem("started","on");
chrome.runtime.sendMessage("on");
started = "on";
document.getElementById("start_stop").value = "Stop";
}
else
{
localStorage.setItem("started","off");
chrome.runtime.sendMessage("off");
started = "off";
document.getElementById("start_stop").value = "Start";
}
function loadFromBrowserStorage(item,callback_function) {
chrome.storage.local.get(item, callback_function);
}
// if exists reload config tab , to get the start/stop information correct
chrome.tabs.query({currentWindow: true},reloadConfigTab);
function storeInBrowserStorage(item,callback_function) {
chrome.storage.local.set(item,callback_function);
}
function start_modify() {
if (started==="off") {
storeInBrowserStorage({started:'on'},function() {
chrome.runtime.sendMessage("on");
started = "on";
document.getElementById("start_stop").value = "Stop";
// if exists reload config tab , to get the start/stop information correct
chrome.tabs.query({currentWindow: true},reloadConfigTab);
});
}
else {
storeInBrowserStorage({started:'off'},function() {
chrome.runtime.sendMessage("off");
started = "off";
document.getElementById("start_stop").value = "Start";
// if exists reload config tab , to get the start/stop information correct
chrome.tabs.query({currentWindow: true},reloadConfigTab);
});
}
}
function reloadConfigTab(tabs)
......
// Mock the Storage as the chrome.storage is not available outside of webextension
function loadFromBrowserStorage(item,callback_function) {
var result = new Object;
Object.defineProperty(result, item, {
value: localStorage.getItem(item[0]),
writable: true
});
callback_function.call(this,result);
}
function storeInBrowserStorage(item,callback_function) {
localStorage.setItem(Object.entries(item)[0][0],Object.entries(item)[0][1]);
callback_function.call();
}
describe("Config", function() {
function cleanConfigTableForTest() {
......@@ -41,6 +59,7 @@ describe("Config", function() {
});
});
describe("#function create_configuration_data", function() {
beforeEach(function() {
......@@ -69,7 +88,6 @@ describe("Config", function() {
});
});
describe("#function loadConfiguration", function() {
// mock
......@@ -422,4 +440,5 @@ describe("#function invertLine", function() {
expect(document.getElementById('targetPage').style.color).toEqual("red");
});
});
});
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