I am making a Chrome Extension which basically has a button. The button changes color and text on click and alternates between "Enable" and "Disable". See this fiddle for explanation and to see what I am talking about.
Now what I want is that once I click the button and it goes to disable condition, it should remain disabled until clicked again.How can I do it?
I know that whenever the extension popup is opened ,it is like a new page, so I would have to save the previous state using chrome.storage and load this state every time popup is clicked. I get a slight idea by this ans here. But I cannot wrap my head around it fully.
My manifest:
{
"manifest_version": 2,
"name": "Parental Control",
"description": "This extension allows the user to change the background color of the current page.",
"version": "1.0",
"background":{
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html",
"run_at": "document_start"
},
"permissions": [
"tabs",
"activeTab",
"storage",
"webRequest",
"webRequestBlocking"
],
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["content_script.js"],
"run_at": "document_end"
}
]
}
popup.html:
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style type="text/css">
body {
width:200px;
height:300px;
white-space: nowrap;
background: linear-gradient(0deg, rgba(248, 246, 242, 0.8), rgba(248, 246, 242, 0.8)), url(back_main.jpg) repeat;
}
</style>
<link href="css/button.css" rel="stylesheet">
<link href="css/logo.css" rel="stylesheet">
<script type="text/javascript" src="popup.js"></script>
</head>
<body>
<img src="icon.png" alt="Control" class="center" style="width:80px;height:80px;">
<button class="button" style="vertical-align:middle" id="buttonclick">
<span>Enable Control</span></button>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>
popup.js:
document.addEventListener('DOMContentLoaded', function() {
var click = document.getElementById("buttonclick")
click.addEventListener("click", handler);
});
var count = 1;
function handler() {
var property = document.getElementById('buttonclick');
if (count == 0) {
property.style.backgroundColor = "#4f8ff7"
property.innerHTML = "<span>Enable control</span>"
count = 1;
}
else {
property.style.backgroundColor = "#a84237"
property.innerHTML = "<span>Disable control</span>"
count = 0;
}
}