Skip to content

Commit

Permalink
Custom favicon support
Browse files Browse the repository at this point in the history
  • Loading branch information
dave9123 committed May 17, 2024
1 parent 02eae19 commit b042394
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
51 changes: 41 additions & 10 deletions static/worksheets/api/newloader.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
// Get the form and form elements
const form = document.getElementById('settings-form');
const faviconSelect = form.elements['favicon'];
const faviconURL = document.getElementById('faviconurl');
const siteNameInput = form.elements['site-name'];
const button = form.querySelector('button');
const favicon = document.querySelector('link[rel="icon"]');

// Get the saved settings, or set default values
const savedSettings = JSON.parse(localStorage.getItem('site-settings')) || {};
const defaultFavicon = '/worksheets/assets/favicon/gmail-16-16.png';
const defaultSiteName = 'Inbox';

// Set the default values
faviconSelect.value = savedSettings.favicon || defaultFavicon;
siteNameInput.value = savedSettings.siteName || defaultSiteName;

// Apply the settings site-wide
const favicon = document.querySelector('link[rel="icon"]');
favicon.href = faviconSelect.value;
document.title = siteNameInput.value;
document.title = savedSettings.siteName || defaultSiteName;
const options = Array.from(faviconSelect.options);

// Check if the saved favicon is in the options
const matchingOption = options.find(option => option.value === savedSettings.favicon);

if (matchingOption) {
// If a matching option is found, set the select value to that
faviconSelect.value = savedSettings.favicon;
faviconURL.value = savedSettings.favicon;
favicon.href = savedSettings.favicon;
} else if (savedSettings.custom) {
// If no matching option is found and custom is true, set the select value to "Custom"
faviconSelect.value = "Custom";
faviconURL.value = savedSettings.favicon;
favicon.href = savedSettings.favicon;
} else {
// If no matching option and custom is false, use the default favicon
faviconSelect.value = defaultFavicon;
faviconURL.value = defaultFavicon;
favicon.href = defaultFavicon;
}

siteNameInput.value = savedSettings.siteName || defaultSiteName;

// Update favicon and save settings when changed
faviconSelect.addEventListener('change', () => {
favicon.href = faviconSelect.value;
if (faviconSelect.value === "Custom") {
favicon.href = faviconURL.value;
} else {
faviconURL.value = faviconSelect.value;
favicon.href = faviconSelect.value;
}
saveSettings();
});

faviconURL.addEventListener('change', () => {
faviconSelect.value = "Custom";
favicon.href = faviconURL.value;
saveSettings();
});

Expand All @@ -39,8 +69,9 @@ button.addEventListener('click', (event) => {
// Save the settings to localStorage
function saveSettings() {
const settings = {
favicon: faviconSelect.value,
siteName: siteNameInput.value
favicon: faviconSelect.value === "Custom" ? faviconURL.value : faviconSelect.value,
siteName: siteNameInput.value,
custom: faviconSelect.value === "Custom"
};
localStorage.setItem('site-settings', JSON.stringify(settings));
console.log("Settings saved!");
Expand Down
5 changes: 5 additions & 0 deletions static/worksheets/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ <h1>Tab Cloak</h1>
<option value="/worksheets/assets/favicon/logo.png">Astroid Logo</option>
<option value="/worksheets/assets/favicon/gmail-16-16.png">Gmail Logo</option>
<option value="/worksheets/assets/favicon/classroom-32-32.png">Classroom Logo</option>
<option id="custom" value="Custom" disabled>Custom</option>
</select>
<br>
<label for="faviconurl">Favicon URL:</label>
<br>
<input type="text" id="faviconurl">
<br>
<label for="site-name">Site Name:</label>
<br>
<input type="text" id="site-name" name="site-name" value="My Site">
Expand Down

0 comments on commit b042394

Please sign in to comment.