2024-07-27 10:07:26 +01:00
|
|
|
import { get } from "svelte/store";
|
|
|
|
|
|
|
|
import { app, device } from "$lib/device";
|
|
|
|
import settings from "$lib/state/settings";
|
|
|
|
|
|
|
|
import { createDialog } from "$lib/dialogs";
|
|
|
|
|
2024-07-28 08:32:21 +01:00
|
|
|
export const openSavingDialog = (url: string) =>
|
|
|
|
createDialog({
|
|
|
|
type: "saving",
|
|
|
|
id: "saving",
|
|
|
|
url
|
|
|
|
})
|
|
|
|
|
2024-07-27 10:07:26 +01:00
|
|
|
export const openURL = (url: string) => {
|
2024-07-28 08:49:23 +01:00
|
|
|
const open = window.open(url, "_blank");
|
|
|
|
|
|
|
|
/* if new tab got blocked by user agent, show a saving dialog */
|
|
|
|
if (!open) {
|
|
|
|
openSavingDialog(url);
|
|
|
|
}
|
2024-07-27 10:07:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const shareURL = async (url: string) => {
|
|
|
|
try {
|
|
|
|
return await navigator?.share({ url });
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const copyURL = async (url: string) => {
|
|
|
|
try {
|
|
|
|
return await navigator?.clipboard.writeText(url);
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-07-22 09:33:43 +01:00
|
|
|
|
|
|
|
export const downloadFile = (url: string) => {
|
2024-07-27 10:07:26 +01:00
|
|
|
const savingPreference = get(settings).save.downloadPopup;
|
2024-07-28 08:32:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
user actions (such as invoke share, open new tab) have expiration.
|
|
|
|
in webkit, for example, that timeout is 5 seconds.
|
|
|
|
https://github.com/WebKit/WebKit/blob/b838f8bb3573bd5906bc5f02fcc8cb274b3c9b8a/Source/WebCore/page/LocalDOMWindow.cpp#L167
|
|
|
|
|
|
|
|
navigator.userActivation.isActive makes sure that we're still able to
|
|
|
|
invoke an action without the user agent interrupting it.
|
|
|
|
if not, we show a saving dialog for user to re-invoke that action.
|
|
|
|
*/
|
|
|
|
if (savingPreference || !navigator.userActivation.isActive) {
|
|
|
|
openSavingDialog(url);
|
2024-07-27 10:07:26 +01:00
|
|
|
} else if (device.is.iOS && app.is.installed) {
|
|
|
|
return shareURL(url);
|
2024-07-22 09:33:43 +01:00
|
|
|
} else {
|
2024-07-27 10:07:26 +01:00
|
|
|
return openURL(url);
|
2024-07-22 09:33:43 +01:00
|
|
|
}
|
2024-07-27 10:07:26 +01:00
|
|
|
}
|