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

Copy in-app links as YouTube links #2951

Merged
merged 2 commits into from
Dec 13, 2022
Merged
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
81 changes: 79 additions & 2 deletions src/main/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
app, BrowserWindow, dialog, Menu, ipcMain,
powerSaveBlocker, screen, session, shell, nativeTheme, net, protocol
powerSaveBlocker, screen, session, shell,
nativeTheme, net, protocol, clipboard
} from 'electron'
import path from 'path'
import cp from 'child_process'
Expand All @@ -22,6 +23,7 @@ function runApp() {
showSaveImageAs: true,
showCopyImageAddress: true,
showSelectAll: false,
showCopyLink: false,
prepend: (defaultActions, parameters, browserWindow) => [
{
label: 'Show / Hide Video Statistics',
Expand All @@ -47,7 +49,82 @@ function runApp() {
browserWindow.webContents.selectAll()
}
}
]
],
// only show the copy link entry for external links and the /playlist, /channel and /watch in-app URLs
// the /playlist, /channel and /watch in-app URLs get transformed to their equivalent YouTube URLs
append: (defaultActions, parameters, browserWindow) => {
let visible = false
const urlParts = parameters.linkURL.split('#')
const isInAppUrl = urlParts[0] === browserWindow.webContents.getURL().split('#')[0]

if (parameters.linkURL.length > 0) {
if (isInAppUrl) {
const path = urlParts[1]

if (path) {
visible = ['/playlist', '/channel', '/watch'].some(p => path.startsWith(p))
}
} else {
visible = true
}
}

return [{
label: 'Copy Lin&k',
Copy link
Member

Choose a reason for hiding this comment

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

I'm guessing the & is a typo?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it makes that context menu entry activatable by clicking on it or pressing the k key while the context menu is open. As electron-context-menu does the same (https://github.com/sindresorhus/electron-context-menu/blob/main/index.js#L167) I thought it would be a good idea to copy it, so that the behaviour is the same (I should probably add it for the Select All option as well).

Copy link
Member

Choose a reason for hiding this comment

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

Ahhh cool, didnt know that

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add code comment for it :S

visible: visible,
click: () => {
let url = parameters.linkURL

if (isInAppUrl) {
const [path, query] = urlParts[1].split('?')
const [route, id] = path.split('/').filter(p => p)

switch (route) {
case 'playlist':
url = `https://youtube.com/playlist?list=${id}`
break
case 'channel':
url = `https://www.youtube.com/channel/${id}`
break
case 'watch': {
url = `https://youtu.be/${id}`

if (query) {
const params = new URLSearchParams(query)
const newParams = new URLSearchParams()
let hasParams = false

if (params.has('playlistId')) {
newParams.set('list', params.get('playlistId'))
hasParams = true
}

if (params.has('timestamp')) {
newParams.set('t', params.get('timestamp'))
hasParams = true
}

if (hasParams) {
url += '?' + newParams.toString()
}
}

break
}
}
}

if (parameters.linkText) {
clipboard.write({
bookmark: parameters.linkText,
text: url
})
} else {
clipboard.writeText(url)
}
}
}]
}
})

// disable electron warning
Expand Down