Copy in-app links as YouTube links (#2951)

* Copy in-app links as YouTube links

* Store the split URL in a variable instead of splitting it multiple times
This commit is contained in:
absidue 2022-12-13 10:39:22 +01:00 committed by GitHub
parent 6976b015d3
commit c6299e69f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 79 additions and 2 deletions

View File

@ -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'
@ -22,6 +23,7 @@ function runApp() {
showSaveImageAs: true,
showCopyImageAddress: true,
showSelectAll: false,
showCopyLink: false,
prepend: (defaultActions, parameters, browserWindow) => [
{
label: 'Show / Hide Video Statistics',
@ -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',
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