Skip to content

Commit 5fc15d4

Browse files
committed
feat: add share button for gifs on mobile
1 parent 5100cae commit 5fc15d4

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

index.html

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,139 @@
268268
navigator.serviceWorker.register('sw.js');
269269
});
270270
}
271+
272+
function addShareButton() {
273+
const downloadLink = document.getElementById('rfd-output');
274+
if (!downloadLink) {
275+
return;
276+
}
277+
278+
const parent = downloadLink.parentElement;
279+
if (parent.querySelector('.share-button')) {
280+
return;
281+
}
282+
283+
// remove the "Ok" button, keep only "Cancel" button
284+
const rfdButtons = parent.querySelectorAll('.rfd-button');
285+
if (rfdButtons.length >= 2) {
286+
rfdButtons[0].remove();
287+
}
288+
289+
if (!navigator.share) {
290+
return;
291+
}
292+
293+
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
294+
(navigator.maxTouchPoints && navigator.maxTouchPoints > 2);
295+
296+
if (!isMobile) {
297+
return;
298+
}
299+
300+
const blobUrl = downloadLink.href;
301+
const fileName = downloadLink.download || 'recording.gif';
302+
303+
const container = document.createElement('div');
304+
container.style.cssText = `
305+
display: flex;
306+
flex-direction: column;
307+
gap: 10px;
308+
align-items: center;
309+
font-weight: bold;
310+
padding-bottom: 10px;
311+
padding-top: 10px;
312+
`;
313+
314+
const shareButton = document.createElement('button');
315+
shareButton.className = 'share-button';
316+
shareButton.textContent = 'save/share';
317+
shareButton.style.cssText = `
318+
padding: 12px 24px;
319+
background-color: #007bff;
320+
color: white;
321+
border: none;
322+
border-radius: 5px;
323+
cursor: pointer;
324+
font-weight: bold;
325+
font-size: 16px;
326+
touch-action: manipulation;
327+
-webkit-tap-highlight-color: transparent;
328+
width: 100%;
329+
max-width: 300px;
330+
`;
331+
332+
shareButton.addEventListener('click', async function(e) {
333+
e.preventDefault();
334+
e.stopPropagation();
335+
336+
try {
337+
const response = await fetch(blobUrl);
338+
const blob = await response.blob();
339+
const file = new File([blob], fileName, { type: 'image/gif' });
340+
341+
if (navigator.canShare && !navigator.canShare({ files: [file] })) {
342+
throw new Error('File sharing not supported');
343+
}
344+
345+
await navigator.share({
346+
files: [file],
347+
title: 'obamification',
348+
text: 'made with https://obamify.com'
349+
});
350+
} catch (error) {
351+
if (error.name !== 'AbortError') {
352+
console.error('Error sharing:', error);
353+
// Fall back to showing the download link
354+
alert('Could not share file: ' + error.message + '\nTry using the download link instead.');
355+
} else {
356+
console.log('Share cancelled by user');
357+
}
358+
}
359+
});
360+
361+
// Style the download link as a secondary button
362+
downloadLink.style.cssText = `
363+
padding: 10px 20px;
364+
background-color: transparent;
365+
color: white;
366+
border: 1px solid white;
367+
border-radius: 5px;
368+
cursor: pointer;
369+
font-weight: normal;
370+
font-size: 14px;
371+
text-decoration: none;
372+
display: inline-block;
373+
touch-action: manipulation;
374+
`;
375+
downloadLink.textContent = 'or download file';
376+
377+
// Wrap everything in the container
378+
parent.insertBefore(container, downloadLink);
379+
container.appendChild(shareButton);
380+
container.appendChild(downloadLink);
381+
}
382+
383+
const observer = new MutationObserver(function(mutations) {
384+
mutations.forEach(function(mutation) {
385+
if (mutation.addedNodes.length) {
386+
addShareButton();
387+
}
388+
});
389+
});
390+
391+
if (document.readyState === 'loading') {
392+
document.addEventListener('DOMContentLoaded', function() {
393+
observer.observe(document.body, {
394+
childList: true,
395+
subtree: true
396+
});
397+
});
398+
} else {
399+
observer.observe(document.body, {
400+
childList: true,
401+
subtree: true
402+
});
403+
}
271404
</script>
272405
</body>
273406

0 commit comments

Comments
 (0)