Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer to copy non-HTTP links to the clipboard instead of trying to open them #3388

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

### Changed
- Offer to copy non-HTTP links to the clipboard instead of trying to open them.

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions _locales/_untranslated_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,8 @@
},
"open_sticker_folder": {
"message": "Open Sticker Folder"
},
"desktop_offer_copy_non_web_link_to_clipboard": {
"message": "The link \"%1$d\" can not be opened in the web browser. Do you want to copy the link to the clipboard instead?"
}
}
4 changes: 2 additions & 2 deletions src/renderer/components/helpers/ChatMethods.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ChatStore, { ChatView } from '../../stores/chat'
import { ScreenContext, unwrapContext } from '../../contexts'
import { C } from '@deltachat/jsonrpc-client'
import { runtime } from '../../runtime'
import { getLogger } from '../../../shared/logger'
import AlertDialog from '../dialogs/AlertDialog'
import { BackendRemote, EffectfulBackendActions, Type } from '../../backend-com'
Expand All @@ -12,6 +11,7 @@ import ConfirmationDialog from '../dialogs/ConfirmationDialog'
import { T } from '@deltachat/jsonrpc-client'
import { sendMessageParams } from '../../../shared/shared-types'
import chatStore from '../../stores/chat'
import { openLinkSafely } from './LinkConfirmation'

const log = getLogger('renderer/message')

Expand Down Expand Up @@ -202,7 +202,7 @@ export async function joinCall(
throw new Error('Message has no video chat url')
}

return runtime.openLink(message.videochatUrl)
return openLinkSafely(message.videochatUrl)
} catch (error: todo) {
log.error('failed to join call', error)
screenContext.openDialog(AlertDialog, { message: error.toString() })
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/helpers/ClickableLink.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { PropsWithChildren } from 'react'
import { runtime } from '../../runtime'
import { openLinkSafely } from './LinkConfirmation'

export default class ClickableLink extends React.Component<
PropsWithChildren<{
Expand All @@ -9,7 +9,7 @@ export default class ClickableLink extends React.Component<
> {
onClick(event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) {
event.preventDefault()
runtime.openLink(this.props.href)
openLinkSafely(this.props.href)
}

render() {
Expand Down
17 changes: 17 additions & 0 deletions src/renderer/components/helpers/LinkConfirmation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { runtime } from '../../runtime'
import ConfirmationDialog from '../dialogs/ConfirmationDialog'

/** opens http, https and mailto links, offers to copy all other links */
export function openLinkSafely(url: string) {
const tx = window.static_translate
if (url.startsWith('http') || url.startsWith('mailto')) {
runtime.openLink(url)
} else {
window.__openDialog(ConfirmationDialog, {
message: tx('desktop_offer_copy_non_web_link_to_clipboard', url),
confirmLabel: tx('menu_copy_link_to_clipboard'),
cancelLabel: tx('no'),
cb: yes => yes && runtime.writeClipboardText(url),
})
}
}
9 changes: 5 additions & 4 deletions src/renderer/components/message/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import chatStore from '../../stores/chat'
import reactStringReplace from 'react-string-replace'
import { runtime } from '../../runtime'
import { LinkDestination } from '@deltachat/message_parser_wasm'
import { openLinkSafely } from '../helpers/LinkConfirmation'

const log = getLogger('renderer/LabeledLink')

Expand Down Expand Up @@ -52,7 +53,7 @@ export const LabeledLink = ({

//check if domain is trusted, or if there is no domain like on mailto just open it
if (isDeviceChat || !hostName || isDomainTrusted(hostName)) {
runtime.openLink(target)
openLinkSafely(target)
return
}
// not trusted - ask for confirmation from user
Expand Down Expand Up @@ -133,7 +134,7 @@ function labeledLinkConfirmationDialog(
// trust url
trustDomain(hostname)
}
runtime.openLink(target)
openLinkSafely(target)
}}
>
{tx('open')}
Expand Down Expand Up @@ -164,7 +165,7 @@ export const Link = ({ destination }: { destination: LinkDestination }) => {
punycode.punycode_encoded_url
)
} else {
runtime.openLink(target)
openLinkSafely(target)
}
}
return (
Expand Down Expand Up @@ -229,7 +230,7 @@ function openPunycodeUrlConfirmationDialog(
className={`delta-button bold primary`}
onClick={() => {
onClose()
runtime.openLink(asciiUrl)
openLinkSafely(asciiUrl)
}}
>
{tx('open')}
Expand Down
6 changes: 5 additions & 1 deletion src/renderer/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,12 @@ class Electron implements Runtime {
openLink(link: string): void {
if (link.startsWith('mailto:')) {
processOpenQrUrl(link)
} else {
} else if (link.startsWith('http')) {
Copy link
Collaborator

@link2xt link2xt Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather explicitly check for "http://" or "https://" prefix with ||, who knows if some app decides to register httpx scheme.

ipcBackend.invoke('electron.shell.openExternal', link)
} else {
log.error('tried to open a non mailto or http/https external link', {
link,
})
}
}
private rc_config: RC_Config | null = null
Expand Down
Loading