I'm making a extension for Google Chrome, the extension deletes all browsing data when the user clicks it, the extension works fine but now im trying to add a options panel where the user can choose which data to delete, my problem is that the options made by the user in the options panel are not being saved (or im not being able to retrieve the data stored in localStorage properly) because when i access the options panel again after saving and closing the checkboxes are blank again.
Manifest [manifest.json]
{
"name": "TEST TITLE",
"version": "1.0",
"manifest_version": 2,
"description": "TEST DESCRIPTION",
"options_page": "options.html",
"icons": { "16": "icon-small.png",
"48": "icon-medium.png",
"128": "icon-big.png" },
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"storage",
"browsingData"
]
}
HTML [popup.html]
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
HTML [options.html]
<!DOCTYPE html>
<html>
<head>
<script src="script-options.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body onLoad="loadOptions()">
<div id="wrapper-options">
<div id="header-options">OPTIONS</div>
<div id="content-options">
<div id="checkbox-options">
<input name="" id="protectedWeb-checkbox" type="checkbox" value="">Websites that have been installed as hosted applications
</div>
<div id="checkbox">
<input name="" id="unprotectedWeb-checkbox" type="checkbox" value="">Normal websites
</div>
<div id="checkbox">
<input name="" id="extension-checkbox" type="checkbox" value="">Extensions and packaged applications a user has installed
</div>
<div id="checkbox">
<input name="" id="appcache-checkbox" type="checkbox" value="">Websites appcaches
</div>
<div id="checkbox">
<input name="" id="cache-checkbox" type="checkbox" value="">Browser's cache
</div>
<div id="checkbox">
<input name="" id="cookies-checkbox" type="checkbox" value="">Browser's cookies
</div>
<div id="checkbox">
<input name="" id="downloads-checkbox" type="checkbox" value="">Browser's download
</div>
<div id="checkbox">
<input name="" id="fileSystems-checkbox" type="checkbox" value="">Websites file systems
</div>
<div id="checkbox">
<input name="" id="formData-checkbox" type="checkbox" value="">Browser's stored form data
</div>
<div id="checkbox">
<input name="" id="history-checkbox" type="checkbox" value="">Browser's history
</div>
<div id="checkbox">
<input name="" id="indexedDB-checkbox" type="checkbox" value="">Websites IndexedDB data
</div>
<div id="checkbox">
<input name="" id="localStorage-checkbox" type="checkbox" value="">Websites local storage data
</div>
<div id="checkbox">
<input name="" id="pluginData-checkbox" type="checkbox" value="">Plugins data
</div>
<div id="checkbox">
<input name="" id="passwords-checkbox" type="checkbox" value="">Stored passwords
</div>
<div id="checkbox">
<input name="" id="webSQL-checkbox" type="checkbox" value="">Websites WebSQL data
</div>
<div id="button-option">
<input name="" type="button" value="SAVE" onClick="saveOptions()">
<input name="" type="button" value="CANCEL" onClick="">
</div>
</div>
</div>
</body>
</html>
JavaScript [script.js]
// JavaScript Document
function browsingdata(){
chrome.browsingData.remove({
"originTypes": {
"protectedWeb": true, // Set to true or false as per your requirement / Websites that have been installed as hosted applications
"unprotectedWeb":true, // Set to true or false as per your requirement / Normal websites
"extension":true // Set to true or false as per your requirement / Extensions and packaged applications a user has installed
}
}, {
"appcache": true, // Set to true or false as per your requirement / Websites appcaches
"cache": true, // Set to true or false as per your requirement / Browser's cache
"cookies": true, // Set to true or false as per your requirement / Browser's cookies
"downloads": true, // Set to true or false as per your requirement / Browser's download
"fileSystems": true, // Set to true or false as per your requirement / Websites file systems
"formData": true, // Set to true or false as per your requirement / Browser's stored form data
"history": true, // Set to true or false as per your requirement / Browser's history
"indexedDB": true, // Set to true or false as per your requirement / Websites IndexedDB data
"localStorage": true, // Set to true or false as per your requirement / Websites local storage data
"pluginData": true, // Set to true or false as per your requirement / Plugins data
"passwords": true, // Set to true or false as per your requirement / Stored passwords
"webSQL": true // Set to true or false as per your requirement / Websites WebSQL data
}, function (){
console.log("All data Deleted");
});
}
window.onload=browsingdata;
JavaScript [script-options.js]
// JavaScript Document
function saveOptions() {
if (document.getElementById('protectedWeb-checkbox').checked) {
localStorage.setItem('protectedWeb-checkbox', "true");
} else {
localStorage.setItem('protectedWeb-checkbox', "false");
}
}
function loadOptions() {
if (localStorage.getItem('protectedWeb-checkbox') == "true") {
console.log("its checked");
document.getElementById("protectedWeb-checkbox").checked=true;
} else {
console.log("its not checked");
}
}
In addition to the main question i also would like to know which is the best way in your opinion to add the other 14 options to JavaScript [script-options.js] and how would you recommend i change the value (true/false) on JavaScript [script.js] using also JavaScript, and ANY advice or recommendation about the whole extension code also would be very appreciated.
As always thanks in advance for taking the time to read this, the below list are all the StackOverflow questions and sites i've been reading so far.
https://stackoverflow.com/questions/3033829/google-chrome-extension-local-storage
https://stackoverflow.com/questions/2153070/do-chrome-extensions-have-access-to-local-storage
https://stackoverflow.com/questions/11679967/remember-checkbox-with-localstorage-onclick
http://julip.co/2010/01/how-to-build-a-chrome-extension-part-2-options-and-localstorage/
https://developer.chrome.com/stable/extensions/storage.html