Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 217 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,44 @@
opacity: 0.3;
}
}

/* Photo Attribution / Title Infobox (bottom-right corner) */
.photo-infobox {
position: fixed;
bottom: 16px;
right: 16px;
z-index: 900;
display: none;
flex-direction: column;
gap: 2px;
max-width: min(380px, calc(100vw - 32px));
padding: 8px 12px;
border-radius: 8px;
background: rgba(0, 0, 0, 0.45);
backdrop-filter: blur(8px);
border: 1px solid rgba(255, 255, 255, 0.18);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
color: rgba(255, 255, 255, 0.92);
font-size: 0.75rem;
line-height: 1.35;
text-align: right;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.6);
pointer-events: auto;
}

.photo-infobox.visible {
display: flex;
}

.photo-infobox-title {
font-weight: 600;
color: rgba(255, 255, 255, 0.96);
}

.photo-infobox-attribution {
font-size: 0.7rem;
color: rgba(255, 255, 255, 0.78);
}
</style>
</head>
<body>
Expand All @@ -725,7 +763,8 @@
<label for="theme-dropdown">Theme:</label>
<select id="theme-dropdown">
<option value="auto">Auto (Date-based)</option>
<option value="default">Default</option>
<option value="default">Default (Bing)</option>
<option value="planks">Planks</option>
<option value="halloween">Halloween</option>
<option value="movember">Movember</option>
<option value="christmas">Christmas</option>
Expand All @@ -746,6 +785,11 @@

<div class="clocks-container" id="clocks-container"></div>

<div class="photo-infobox" id="photo-infobox" aria-live="polite">
<div class="photo-infobox-title" id="photo-infobox-title"></div>
<div class="photo-infobox-attribution" id="photo-infobox-attribution"></div>
</div>

<script>
// Default clock configuration. Used whenever the active theme does not
// specify its own `clocks` array. This is the single source of truth for
Expand Down Expand Up @@ -1909,6 +1953,31 @@
handGlow: '0 0 3px 1px rgba(0,0,0,0.5), 0 0 6px 2px rgba(0,0,0,0.25)'
},
default: {
clockFace: 'clock-face.png',
clockFaceScale: 'cover',
background: 'planks.jpg', // Fallback background if API is unreachable
getBackground: fetchBingDailyBackground,
backgroundSize: 'cover',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center',
backgroundAttachment: 'fixed',
textColor: '#FFFFFF',
centerDotColor: 'radial-gradient(circle, #f5f5f5 20%, #d0d0d0 40%, #a8a8a8 80%, #808080 100%)',
centerDotBorder: '#888',
handColors: {
hour: 'linear-gradient(to right, #1a0f08, #2c1810, #1a0f08)',
minute: 'linear-gradient(to right, #2c1810, #4a2c1a, #2c1810)',
second: 'linear-gradient(to bottom, #8b0000, #dc143c, #8b0000)'
},
clockContainer: {
background: 'rgba(0, 0, 0, 0.2)',
backdropFilter: 'blur(12px)',
borderRadius: '20px',
border: '1px solid rgba(255, 255, 255, 0.22)',
boxShadow: '0 8px 32px rgba(0, 0, 0, 0.35)'
}
},
planks: {
clockFace: 'clock-face.png',
clockFaceScale: 'cover',
background: 'planks.jpg',
Expand All @@ -1926,6 +1995,118 @@
}
};

// Parse Bing's combined "Title (© Photographer/Agency)" copyright string
// into separate title and attribution fields.
function parseBingCopyright(rawCopyright, explicitTitle) {
const raw = typeof rawCopyright === 'string' ? rawCopyright.trim() : '';
const givenTitle = typeof explicitTitle === 'string' ? explicitTitle.trim() : '';

if (!raw) {
return { title: givenTitle, attribution: '' };
}

const match = raw.match(/^(.*?)\s*\(([^()]+)\)\s*$/);
if (match) {
return {
title: givenTitle || match[1].trim(),
attribution: match[2].trim()
};
}

if (givenTitle) {
return { title: givenTitle, attribution: raw };
}

return { title: raw, attribution: '' };
}

// Fetch the daily Bing wallpaper and its title/attribution via CORS-enabled
// endpoints, falling back to a secondary mirror if the primary is unavailable.
async function fetchBingDailyBackground() {
const primaryUrl = 'https://bing.biturl.top/?resolution=UHD&format=json&index=0&mkt=nb-NO';
const primaryData = await tryFetchJson(primaryUrl);
if (primaryData && primaryData.url) {
const { title, attribution } = parseBingCopyright(primaryData.copyright, primaryData.title);
return {
url: primaryData.url,
title,
attribution
};
}

const fallbackUrl = 'https://peapix.com/bing/feed?n=1';
const fallbackData = await tryFetchJson(fallbackUrl);
if (Array.isArray(fallbackData) && fallbackData.length > 0) {
const item = fallbackData[0];
const imageUrl = item.fullUrl || item.imageUrl;
if (imageUrl) {
const { title, attribution } = parseBingCopyright(item.copyright, item.title);
return {
url: imageUrl,
title,
attribution
};
}
}

return null;
}

// Normalize photo title/attribution from a theme definition and/or dynamic
// background payload.
function resolvePhotoInfo(theme, dynamicResult) {
const themeInfo = (theme && typeof theme.infoBox === 'object' && theme.infoBox) || {};
const dynObj = (dynamicResult && typeof dynamicResult === 'object') ? dynamicResult : {};
const dynInfo = (typeof dynObj.infoBox === 'object' && dynObj.infoBox) || {};

const title = (
dynInfo.title ??
dynObj.title ??
dynObj.photoTitle ??
themeInfo.title ??
theme?.photoTitle ??
''
).toString().trim();

const attribution = (
dynInfo.attribution ??
dynObj.attribution ??
dynObj.photoAttribution ??
themeInfo.attribution ??
theme?.photoAttribution ??
''
).toString().trim();

return { title, attribution };
}

// Show or hide the bottom-right photo infobox depending on whether any
// title or attribution text is provided.
function updatePhotoInfoBox(info) {
const box = document.getElementById('photo-infobox');
const titleEl = document.getElementById('photo-infobox-title');
const attrEl = document.getElementById('photo-infobox-attribution');
if (!box || !titleEl || !attrEl) return;

const title = info && typeof info.title === 'string' ? info.title.trim() : '';
const attribution = info && typeof info.attribution === 'string' ? info.attribution.trim() : '';

if (!title && !attribution) {
box.classList.remove('visible');
titleEl.textContent = '';
attrEl.textContent = '';
return;
}

titleEl.textContent = title;
titleEl.style.display = title ? 'block' : 'none';

attrEl.textContent = attribution;
attrEl.style.display = attribution ? 'block' : 'none';

box.classList.add('visible');
}

// Determine which theme key should be active right now, based on the
// user's manual selection or the date-based automatic schedule.
function resolveSelectedThemeKey() {
Expand Down Expand Up @@ -1971,13 +2152,17 @@
// Apply purely visual theme styling to the (already rendered) clocks.
// City names themselves are produced during render, so this only handles
// colors, backgrounds, and the clock-face image.
function applyThemeStyles(theme) {
// Apply background image and properties
document.body.style.backgroundImage = `url('${theme.background}')`;
async function applyThemeStyles(theme) {
// Apply initial or previously cached background image and properties immediately
const initialBg = theme._cachedBackground || theme.background;
document.body.style.backgroundImage = `url('${initialBg}')`;
document.body.style.backgroundSize = theme.backgroundSize || 'auto';
document.body.style.backgroundRepeat = theme.backgroundRepeat || 'repeat';
document.body.style.backgroundPosition = theme.backgroundPosition || 'top left';
document.body.style.backgroundAttachment = theme.backgroundAttachment || 'scroll';

// Update photo infobox with any static theme info (or cached dynamic info)
updatePhotoInfoBox(resolvePhotoInfo(theme, theme._cachedPhotoInfo));

// Apply text colors
document.querySelectorAll('.clock, .city-name, .date, .temperature').forEach(element => {
Expand Down Expand Up @@ -2040,6 +2225,30 @@
}
`;
document.head.appendChild(style);

// If the theme defines a dynamic background resolver, fetch and apply it
if (typeof theme.getBackground === 'function') {
try {
const dynamicResult = await theme.getBackground();
// Guard against race conditions if the user switched themes while fetching
if (getThemeByKey(resolveSelectedThemeKey()) !== theme) return;

if (dynamicResult) {
const dynamicUrl = typeof dynamicResult === 'string'
? dynamicResult
: (dynamicResult.url || dynamicResult.background);

if (dynamicUrl) {
theme._cachedBackground = dynamicUrl;
document.body.style.backgroundImage = `url('${dynamicUrl}')`;
}
theme._cachedPhotoInfo = dynamicResult;
}
updatePhotoInfoBox(resolvePhotoInfo(theme, dynamicResult || theme._cachedPhotoInfo));
} catch (err) {
console.warn('Failed to fetch dynamic theme background:', err);
}
}
}

// Resolve theme + clock layout, rebuild the DOM only when the active set of
Expand All @@ -2059,7 +2268,7 @@
activeClockSignature = signature;
}

applyThemeStyles(theme);
await applyThemeStyles(theme);

if (needsRebuild) {
updateAllClocks();
Expand All @@ -2080,7 +2289,7 @@
}

const theme = themes[selectedThemeKey];
if (!theme.dateRange) {
if (!theme || !theme.dateRange) {
dateInfoElement.textContent = 'Resten av året';
return;
}
Expand All @@ -2102,7 +2311,8 @@
const themeDropdown = document.getElementById('theme-dropdown');
if (!themeDropdown) return;

const savedTheme = localStorage.getItem('selectedTheme') || 'auto';
const rawSavedTheme = localStorage.getItem('selectedTheme') || 'auto';
const savedTheme = (rawSavedTheme === 'auto' || themes[rawSavedTheme]) ? rawSavedTheme : 'default';
themeDropdown.value = savedTheme;
updateThemeDateInfo();

Expand Down