Skip to content

Commit

Permalink
Make sure IDB and service worker work without cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
tomayac committed Aug 29, 2022
1 parent 50a3875 commit b9d2c59
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 28 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions docs/assets/index.a1ccdec1.js → docs/assets/index.c9eeed59.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion docs/assets/share.232178b4.js

This file was deleted.

1 change: 1 addition & 0 deletions docs/assets/share.518e5a0f.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
}
</script>
<base target="_blank" />
<script type="module" crossorigin src="/assets/index.a1ccdec1.js"></script>
<script type="module" crossorigin src="/assets/index.c9eeed59.js"></script>
<link rel="stylesheet" href="/assets/style.713cd514.css">
<link rel="manifest" href="/manifest.webmanifest"></head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion docs/sw.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/js/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ saveSVGButton.addEventListener('click', async () => {
};

try {
const fileName = getSuggestedFileName(await get(FILE_HANDLE));
let fileHandle = false;
try {
fileHandle = await get(FILE_HANDLE);
} catch (err) {
// Do nothing. The user probably blocks cookies.
}
const fileName = getSuggestedFileName(fileHandle);
await fileSave(createPromiseBlob(), {
fileName,
description: 'SVG file',
Expand Down
11 changes: 8 additions & 3 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ if ('share' in navigator && 'canShare' in navigator) {

if ('serviceWorker' in navigator) {
window.addEventListener('load', async () => {
await navigator.serviceWorker.register('./sharetargetsw.js', {
scope: '/share-target/',
});
try {
await navigator.serviceWorker.register('./sharetargetsw.js', {
scope: '/share-target/',
});
} catch (err) {
console.error(err.name, err.message);
showToast(err.message);
}

if (location.search.includes('share-target')) {
const keys = await caches.keys();
Expand Down
11 changes: 9 additions & 2 deletions src/js/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ shareSVGButton.addEventListener('click', async () => {
showToast(i18n.t('optimizingSVG'), Infinity);
svg = await optimizeSVG(svg);
clearToast();
const suggestedFileName =
getSuggestedFileName(await get(FILE_HANDLE)) || 'Untitled.svg';
let fileHandle = false;
try {
fileHandle = await get(FILE_HANDLE);
} catch (err) {
// Do nothing. The user probably blocks cookies.
}
const suggestedFileName = fileHandle
? getSuggestedFileName(fileHandle)
: 'Untitled.svg';
const file = new File([svg], suggestedFileName, { type: 'image/svg+xml' });
const data = {
files: [file],
Expand Down
34 changes: 25 additions & 9 deletions src/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,20 @@ const initUI = async () => {
onMaxWidthMatch();
mediaQueryList.addEventListener('change', onMaxWidthMatch);

colorRadio.checked = (await get(colorRadio.id)) ?? colorRadio.defaultChecked;
monochromeRadio.checked =
(await get(monochromeRadio.id)) ?? monochromeRadio.defaultChecked;
try {
colorRadio.checked =
(await get(colorRadio.id)) ?? colorRadio.defaultChecked;
} catch (err) {
// The user probably blocks cookies.
colorRadio.checked = colorRadio.defaultChecked;
}
try {
monochromeRadio.checked =
(await get(monochromeRadio.id)) ?? monochromeRadio.defaultChecked;
} catch (err) {
// The user probably blocks cookies.
monochromeRadio.checked = monochromeRadio.defaultChecked;
}
if (colorRadio.checked) {
svgOutput.classList.add(COLOR);
}
Expand Down Expand Up @@ -663,13 +674,18 @@ const resetSettings = async () => {
};

const getSettings = async () => {
const settings = colorRadio.checked
? await get(COLOR_SETTINGS)
: await get(MONOCHROME_SETTINGS);
if (settings) {
return settings;
try {
const settings = colorRadio.checked
? await get(COLOR_SETTINGS)
: await get(MONOCHROME_SETTINGS);
if (settings) {
return settings;
}
return {};
} catch (err) {
// The user probably blocks cookies.
return {};
}
return {};
};

const storeSettings = async (input) => {
Expand Down

0 comments on commit b9d2c59

Please sign in to comment.