Skip to content

Commit

Permalink
more functionality initing
Browse files Browse the repository at this point in the history
  • Loading branch information
Notplayingallday383 authored Feb 27, 2024
1 parent a05d660 commit d86cd20
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 132 deletions.
1 change: 1 addition & 0 deletions components/modules/inject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO
306 changes: 174 additions & 132 deletions components/modules/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,147 +3,189 @@ export default async function loadSettings() {
}

export async function saveSettings() {

const titleInput = document.getElementById('title-input');
const title = titleInput.value.trim();
const iconInput = document.getElementById('icon-input');
const icon = iconInput.value.trim();
const searchEngineSelect = document.getElementById('search-engine-select');
const searchEngine = searchEngineSelect.value;
const proxySelect = document.getElementById('proxySelect');
const selectedOption = proxySelect.options[proxySelect.selectedIndex].value;
const use24HourTimeCheckbox = document.getElementById('use-24hour-checkbox');
const blobwindow = document.getElementById('open-blob-window');
const toggleBeta = document.getElementById('toggle-beta');
const titlerandomizer = document.getElementById('title-randomizer');
const emergencyHotkeyInput = document.getElementById('emergency-switch-hotkey');
const emergencyURLInput = document.getElementById('emergency-url-input');
const fallbackUrlInput = document.getElementById('fallback-url-input');
const useSecondsCheckbox = document.getElementById('use-seconds-checkbox');
const clockCheckbox = document.getElementById('disable-clock');
const metaThemeColor = document.getElementById('meta-theme-color').value.trim();
const bareServerInput = document.getElementById('custom-bare-server-input');

localStorage.setItem('websiteTitle', title);
console.log('Title saved:', title);
localStorage.setItem('websiteIcon', icon);
console.log('Icon saved:', icon);
if (searchEngine === 'custom') {
const customSearchUrl = document.getElementById('custom-search-engine-input').value.trim();
if (customSearchUrl !== '') {
localStorage.setItem('searchEngine', customSearchUrl);
console.log('Search engine saved:', customSearchUrl);
}
} else {
localStorage.setItem('searchEngine', searchEngine);
console.log('Search engine saved:', searchEngine);
}
localStorage.setItem('proxyOption', selectedOption);
console.log('Default Proxy Saved:', selectedOption);
if (toggleBeta.checked) {
localStorage.setItem('betaMode', 'true');
} else {
localStorage.removeItem('betaMode');
}
if (blobwindow.checked) {
localStorage.setItem('openblobwindow', 'true');
localStorage.setItem('usingnewtab', 'true');
} else {
localStorage.removeItem('openblobwindow');
localStorage.removeItem('usingnewtab');
}
if (titlerandomizer.checked) {
localStorage.setItem('useRandomizer', true);
console.log('Use Tab Randomizer: true')
} else {
localStorage.removeItem('useRandomizer');
console.log('Use Tab Randomizer: false')
}
if (emergencyHotkeyInput) {
const emergencyHotkey = emergencyHotkeyInput.value.trim().toLowerCase();
localStorage.setItem('emergencyHotkey', emergencyHotkey);
console.log('Emergency hotkey saved:', emergencyHotkey);
}
if (emergencyURLInput) {
const emergencyURL = emergencyURLInput.value.trim();
localStorage.setItem('emergencyURL', emergencyURL);
console.log('Emergency URL saved:', emergencyURL);
}
if (fallbackUrlInput) {
const fallbackUrl = fallbackUrlInput.value.trim();
localStorage.setItem('fallbackUrl', fallbackUrl);
console.log('Fallback URL saved:', fallbackUrl);
}
if (bareServerInput) {
const bareServer = bareServerInput.value.trim();
caches.open('bareServerCache').then(cache => {
cache.put('bareServerKey', new Response(bareServer));
});

localforage.config({
driver: localforage.INDEXEDDB,
name: 'Sodium',
version: 1.0,
storeName: 'sodium_config',
description: 'Sodiums Config for IndexedDB'
})
localforage.setItem('bare', bareServer);
navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (let registration of registrations) {
registration.update();
console.log("Service Workers Updated");
}
});
localStorage.setItem('bareServer', bareServer);
console.log('BareServer URL saved:', bareServer);
}
if (use24HourTimeCheckbox) {
localStorage.setItem('use24HourTime', use24HourTimeCheckbox.checked.toString());
console.log('Use 24 Hour Time Saved:', use24HourTimeCheckbox.checked);
}
if (includeDateCheckbox) {
localStorage.setItem('showDate', includeDateCheckbox.checked.toString());
console.log('Show Date Saved:', includeDateCheckbox.checked);
}
if (useSecondsCheckbox) {
localStorage.setItem('useSeconds', useSecondsCheckbox.checked);
console.log('Include Seconds in TimeBar:', useSecondsCheckbox.checked);
}
if (clockCheckbox) {
localStorage.setItem('noClock', clockCheckbox.checked);
console.log('Disable Clock:', clockCheckbox.checked);
}
if (metaThemeColor) {
localStorage.setItem('metaThemeColor', metaThemeColor);
console.log('Meta Theme Color:', metaThemeColor);
}

setTimeout(function () {
const notification = document.getElementById('notification');
notification.textContent = 'Settings Saved!';
notification.classList.remove('hidden');

setTimeout(() => {
notification.style.top = '40px';
}, 10);

setTimeout(() => {
notification.style.top = '-50px';
setTimeout(() => {
notification.classList.add('hidden');
//location.reload();
}, 500);
}, 3000);
}, 100);
}

export async function exportSettings() {

const cacheData = {};
Object.keys(localStorage).forEach((key) => {
cacheData[key] = localStorage.getItem(key);
});
const exportedData = JSON.stringify(cacheData);
const blob = new Blob([exportedData], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'sodium_settings.json';
link.click();
URL.revokeObjectURL(url);
}

export async function importSettings() {
const fileInput = document.getElementById('importFile');
fileInput.addEventListener('change', impSets(), false);
fileInput.click();
const impSets = () => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function (e) {
try {
const importedData = JSON.parse(e.target.result);
Object.keys(importedData).forEach((key) => {
localStorage.setItem(key, importedData[key]);
});
const notification = document.getElementById('notification');
notification.textContent = 'Settings Imported Successfully';
notification.classList.remove('hidden');
setTimeout(() => {
notification.style.top = '40px';
}, 10);

setTimeout(() => {
notification.style.top = '-50px';
setTimeout(() => {
notification.classList.add('hidden');
}, 500);
}, 3000);
location.reload();
} catch (error) {
alert('Error importing cache data. Please check the file format.');
}
};
reader.readAsText(file);
}
}

export async function getSettings() {
const titleInput = document.getElementById('title-input');
const title = titleInput.value.trim();
const iconInput = document.getElementById('icon-input');
const icon = iconInput.value.trim();
const searchEngineSelect = document.getElementById('search-engine-select');
const searchEngine = searchEngineSelect.value;
const proxySelect = document.getElementById('proxySelect');
const selectedOption = proxySelect.options[proxySelect.selectedIndex].value;
const use24HourTimeCheckbox = document.getElementById('use-24hour-checkbox');
const blobwindow = document.getElementById('open-blob-window');
const toggleBeta = document.getElementById('toggle-beta');
const titlerandomizer = document.getElementById('title-randomizer');
const emergencyHotkeyInput = document.getElementById('emergency-switch-hotkey');
const emergencyURLInput = document.getElementById('emergency-url-input');
const fallbackUrlInput = document.getElementById('fallback-url-input');
const useSecondsCheckbox = document.getElementById('use-seconds-checkbox');
const clockCheckbox = document.getElementById('disable-clock');
const metaThemeColor = document.getElementById('meta-theme-color').value.trim();
const bareServerInput = document.getElementById('custom-bare-server-input');

localStorage.setItem('websiteTitle', title);
console.log('Title saved:', title);
localStorage.setItem('websiteIcon', icon);
console.log('Icon saved:', icon);
if (searchEngine === 'custom') {
const customSearchUrl = document.getElementById('custom-search-engine-input').value.trim();
if (customSearchUrl !== '') {
localStorage.setItem('searchEngine', customSearchUrl);
console.log('Search engine saved:', customSearchUrl);
}
} else {
localStorage.setItem('searchEngine', searchEngine);
console.log('Search engine saved:', searchEngine);
}
localStorage.setItem('proxyOption', selectedOption);
console.log('Default Proxy Saved:', selectedOption);
if (toggleBeta.checked) {
localStorage.setItem('betaMode', 'true');
} else {
localStorage.removeItem('betaMode');
}
if (blobwindow.checked) {
localStorage.setItem('openblobwindow', 'true');
localStorage.setItem('usingnewtab', 'true');
} else {
localStorage.removeItem('openblobwindow');
localStorage.removeItem('usingnewtab');
}
if (titlerandomizer.checked) {
localStorage.setItem('useRandomizer', true);
console.log('Use Tab Randomizer: true')
} else {
localStorage.removeItem('useRandomizer');
console.log('Use Tab Randomizer: false')
}
if (emergencyHotkeyInput) {
const emergencyHotkey = emergencyHotkeyInput.value.trim().toLowerCase();
localStorage.setItem('emergencyHotkey', emergencyHotkey);
console.log('Emergency hotkey saved:', emergencyHotkey);
}
if (emergencyURLInput) {
const emergencyURL = emergencyURLInput.value.trim();
localStorage.setItem('emergencyURL', emergencyURL);
console.log('Emergency URL saved:', emergencyURL);
}
if (fallbackUrlInput) {
const fallbackUrl = fallbackUrlInput.value.trim();
localStorage.setItem('fallbackUrl', fallbackUrl);
console.log('Fallback URL saved:', fallbackUrl);
}
if (bareServerInput) {
const bareServer = bareServerInput.value.trim();
caches.open('bareServerCache').then(cache => {
cache.put('bareServerKey', new Response(bareServer));
});

localforage.config({
driver: localforage.INDEXEDDB,
name: 'Sodium',
version: 1.0,
storeName: 'sodium_config',
description: 'Sodiums Config for IndexedDB'
})
localforage.setItem('bare', bareServer);
navigator.serviceWorker.getRegistrations().then(function (registrations) {
for (let registration of registrations) {
registration.update();
console.log("Service Workers Updated");
}
});
localStorage.setItem('bareServer', bareServer);
console.log('BareServer URL saved:', bareServer);
}
if (use24HourTimeCheckbox) {
localStorage.setItem('use24HourTime', use24HourTimeCheckbox.checked.toString());
console.log('Use 24 Hour Time Saved:', use24HourTimeCheckbox.checked);
}
if (includeDateCheckbox) {
localStorage.setItem('showDate', includeDateCheckbox.checked.toString());
console.log('Show Date Saved:', includeDateCheckbox.checked);
}
if (useSecondsCheckbox) {
localStorage.setItem('useSeconds', useSecondsCheckbox.checked);
console.log('Include Seconds in TimeBar:', useSecondsCheckbox.checked);
}
if (clockCheckbox) {
localStorage.setItem('noClock', clockCheckbox.checked);
console.log('Disable Clock:', clockCheckbox.checked);
}
if (metaThemeColor) {
localStorage.setItem('metaThemeColor', metaThemeColor);
console.log('Meta Theme Color:', metaThemeColor);
}

setTimeout(function () {
const notification = document.getElementById('notification');
notification.textContent = 'Settings Saved!';
notification.classList.remove('hidden');

setTimeout(() => {
notification.style.top = '40px';
}, 10);

setTimeout(() => {
notification.style.top = '-50px';
setTimeout(() => {
notification.classList.add('hidden');
//location.reload();
}, 500);
}, 3000);
}, 100);
}
3 changes: 3 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import "@mercuryworkshop/alicejs"
import Nav from "@components/nav.tsx"
// @ts-expect-error stfu
import Footer from "@components/footer"
// @ts-expect-error stfu
import { loadSettings } from "@components/modules/inject"
import "../public/assets/css/home.css"
export default function Home() {
loadSettings()
return (
<div>
<Nav />
Expand Down

0 comments on commit d86cd20

Please sign in to comment.