I'm trying to make a simple Chrome Extension where I will 'block' the content of the website Reddit by replacing it with a short text. This is what I have so far:
manifest.json
{
"manifest_version": 2,
"name": "BlockIt",
"description": "Block Reddit, increase productivity!",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_title": "BlockIt"
},
"permissions": [
"storage", "tabs",
"http://www.reddit.com/*"
],
"content_scripts": [
{
"matches": [ "*://reddit.com/*" ],
"js": ["content-script.js"]
}
]
}
popup.html
<!doctype html>
<html>
<head>
<style>
body {
font-family: Verdana, Arial, Helvetica, Tahoma, sans-serif;
background-color:#EFF7FF;
margin: 5px 5px 5px 5px;
width: 110px;
height: 100%;
text-align:center;
}
</style>
<!--Scripts-->
<script src="popup.js"></script>
</head>
<body>
<h2>BlockIt!</h2>
<div id="en"><label for="enable">Enable BlockIt</label> <input id="enable" type="checkbox" style="vertical-align:middle; position:relative; bottom: 1px;"/>
</div>
</body>
</html>
popup.js
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('#enable').addEventListener('change', changeHandler);
});
function changeHandler() {
if (enable.checked) {
chrome.storage.sync.set({ 'enable': true }, function () { });
}
}
content-script.js
var content = document.getElementsByTagName("body");
var text = "BlockIt enabled (to disable, click on the icon).";
//TODO: replace content with text
I'm having two major issues right now: I'm not sure how I should go about in modifying the content of the webpage and replace it with the text
above, and I'm not sure how to inject content-script.js
when the checkbox in popup.html
is checked. How do I go about in approaching this?
Edit: I've made the following change to my code:
content-script.js
chrome.storage.sync.get({ enable: false }, items=> {
if (items.enable) {
document.body.textContent = "BlockIt enabled (to disable, click on the icon).";
}
});
It successfully changes the body of the webpage, but the issue now is that the content of popup.html
changes to the same text as well. What seems to be the problem?
Edit 2:
I've removed content-scrip.js
in popup.html
. The state of the checkbox still doesn't persist. It seems like such a simple solution but I can't seem to fix it. Any help would be appreciated.