To create a browser extension for Chrome, Opera, and Edge for www.blocksmc.com, follow these steps:
Step 1 - Create folder
Create a new folder on your computer for the project, to hold extension files.
Step 2 - manifest.json
Create a file called 'manifest.json' in the project folder and paste the following code:
{
"manifest_version": 2,
"name": "Blocksmc Theme Switcher",
"version": "1.0",
"description": "Switch the theme of www.blocksmc.com from light to dark and vice versa.",
"permissions": [
"activeTab",
"storage"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["*://*.blocksmc.com/*"],
"js": ["contentScript.js"],
"run_at": "document_end"
}
]
}
Step 3 - Create icon
Create a 48x48 pixel icon.png and save it in the project folder.
Step 4 - popup.html
Create a file called 'popup.html' in the project folder and paste the following code:
<!doctype html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
width: 200px;
height: 100px;
}
.slider-label {
display: block;
}
#theme-switcher {
display: inline-block;
}
</style>
<script src="popup.js"></script>
</head>
<body>
<label class="slider-label">Dark</label>
<label class="switch">
<input type="checkbox" id="theme-switcher">
<span class="slider"></span>
</label>
</body>
</html>
Step 5 - popup.js
Create a file called 'popup.js' in the project folder and paste the following code:
document.addEventListener('DOMContentLoaded', function () {
var checkbox = document.getElementById('theme-switcher');
chrome.storage.sync.get('dark', function (data) {
checkbox.checked = data.dark;
});
checkbox.addEventListener('change', function () {
chrome.storage.sync.set({ dark: this.checked }, function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.reload(tabs[0].id);
});
});
});
});
Step 6 - contentScript.js
Create a file called 'contentScript.js' in the project folder and paste the following code:
chrome.storage.sync.get('dark', function (data) {
var isDark = data.dark;
var elements = document.querySelectorAll('*[style]');
for (var i = 0; i < elements.length; i++) {
var currentStyle = elements[i].getAttribute('style');
if (isDark) {
elements[i].setAttribute('style', currentStyle.replace('#008cd3', '#292b2c'));
} else {
elements[i].setAttribute('style', currentStyle.replace('#292b2c', '#008cd3'));
}
}
});
Step 7 - Package and install
To install the extension locally:
- In Chrome, open the extensions page (chrome://extensions/).
- Enable 'Developer mode'.
- Click 'Load unpacked' and select the project folder.
- In Opera and Edge, follow similar steps to load the extension.
With these steps completed, your extension will be ready to switch the theme of www.blocksmc.com based on the user's preference.