code.js
code.js
(function() {
'use strict';
// Constants
const BUTTON_ID = 'darkModeToggle';
const UI_ID = 'darkModeToggleUI';
const TOGGLE_UI_BUTTON_ID = 'toggleDarkModeUIButton';
// SVG Icons
const moonIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-
linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21
12.79z"></path></svg>`;
const sunIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-
linejoin="round"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1"
x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22"
y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78"
y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12"
x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line
x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>`;
// Default Settings
const defaultSettings = {
position: 'bottom-right',
offsetX: 30,
offsetY: 30,
};
#${BUTTON_ID}:hover {
opacity: 1;
transform: scale(1.1);
transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275),
box-shadow 0.2s;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
#${BUTTON_ID}::before {
content: '';
width: 32px;
height: 32px;
border-radius: 50%;
transition-property: transform, background-color, -webkit-mask-image,
mask-image;
transition-duration: 0.3s;
transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55),
ease, ease, ease;
z-index: 1;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #333;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
background: none;
-webkit-mask-image: url('data:image/svg+xml;utf8,${moonIcon}');
mask-image: url('data:image/svg+xml;utf8,${moonIcon}');
-webkit-mask-size: cover;
mask-size: cover;
background-color: #333;
}
#${BUTTON_ID}.dark {
background-color: #000;
}
#${BUTTON_ID}.dark::before {
transform: translateX(40px);
color: #ffeb3b;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
background: none;
-webkit-mask-image: url('data:image/svg+xml;utf8,${sunIcon}');
mask-image: url('data:image/svg+xml;utf8,${sunIcon}');
-webkit-mask-size: cover;
mask-size: cover;
background-color: #fff;
}
/* UI Styles */
#${UI_ID} {
position: fixed;
top: 20px;
left: 20px;
background-color: #f7f7f7; /* Lighter background */
border: 1px solid #ddd; /* Lighter border */
padding: 15px; /* Slightly larger padding */
z-index: 1001;
border-radius: 8px; /* Rounded corners */
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* Softer shadow */
display: none; /* Initially hidden */
color: #444; /* Darker, more readable text */
font-family: sans-serif; /* Modern font */
}
#${UI_ID}.visible {
display: block; /* Show when visible class is added */
}
#${UI_ID} label {
display: block;
margin-bottom: 8px; /* More space below labels */
font-weight: 500; /* Slightly bolder labels */
}
#${TOGGLE_UI_BUTTON_ID}:hover {
background-color: #eee;
}
`);
button.style.bottom = '';
button.style.top = '';
button.style.left = '';
button.style.right = '';
switch (position) {
case 'top-left':
button.style.top = `${offsetY}px`;
button.style.left = `${offsetX}px`;
break;
case 'top-right':
button.style.top = `${offsetY}px`;
button.style.right = `${offsetX}px`;
break;
case 'bottom-left':
button.style.bottom = `${offsetY}px`;
button.style.left = `${offsetX}px`;
break;
case 'bottom-right':
default:
button.style.bottom = `${offsetY}px`;
button.style.right = `${offsetX}px`;
break;
}
}
if (isDark) {
DarkReader.disable();
await GM.setValue('darkMode', false);
button.classList.remove('dark');
} else {
DarkReader.enable({
brightness: 100,
contrast: 90,
sepia: 10
});
await GM.setValue('darkMode', true);
button.classList.add('dark');
}
}
// Create UI
function createUI() {
const ui = document.createElement('div');
ui.id = UI_ID;
// Position Selector
const positionLabel = document.createElement('label');
positionLabel.textContent = 'Position:';
const positionSelect = document.createElement('select');
const positions = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
positions.forEach(pos => {
const option = document.createElement('option');
option.value = pos;
option.textContent = pos;
option.selected = settings.position === pos;
positionSelect.appendChild(option);
});
positionSelect.addEventListener('change', (e) => {
settings.position = e.target.value;
saveSettings();
});
ui.appendChild(positionLabel);
ui.appendChild(positionSelect);
// Offset X
const offsetXLabel = document.createElement('label');
offsetXLabel.textContent = 'Offset X:';
const offsetXInput = document.createElement('input');
offsetXInput.type = 'number';
offsetXInput.value = settings.offsetX;
offsetXInput.addEventListener('change', (e) => {
settings.offsetX = parseInt(e.target.value);
saveSettings();
});
ui.appendChild(offsetXLabel);
ui.appendChild(offsetXInput);
// Offset Y
const offsetYLabel = document.createElement('label');
offsetYLabel.textContent = 'Offset Y:';
const offsetYInput = document.createElement('input');
offsetYInput.type = 'number';
offsetYInput.value = settings.offsetY;
offsetYInput.addEventListener('change', (e) => {
settings.offsetY = parseInt(e.target.value);
saveSettings();
});
ui.appendChild(offsetYLabel);
ui.appendChild(offsetYInput);
document.body.appendChild(ui);
}
if (isDark) {
DarkReader.enable({
brightness: 100,
contrast: 90,
sepia: 10
});
button.classList.add('dark');
}
}
init();
})();