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

Add context menu entries to copy YouTube and Invidious links #2957

Merged
merged 1 commit into from
Dec 14, 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
128 changes: 79 additions & 49 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function runApp() {
}
],
// 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
// the /playlist, /channel and /watch in-app URLs get transformed to their equivalent YouTube or Invidious URLs
append: (defaultActions, parameters, browserWindow) => {
let visible = false
const urlParts = parameters.linkURL.split('#')
Expand All @@ -69,61 +69,91 @@ function runApp() {
}
}

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()
}
}
const copy = (url) => {
if (parameters.linkText) {
clipboard.write({
bookmark: parameters.linkText,
text: url
})
} else {
clipboard.writeText(url)
}
}

const transformURL = (toYouTube) => {
let origin

if (toYouTube) {
origin = 'https://www.youtube.com'
} else {
origin = 'https://redirect.invidious.io'
}

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

switch (route) {
case 'playlist':
return `${origin}/playlist?list=${id}`
case 'channel':
return `${origin}/channel/${id}`
case 'watch': {
let url

if (toYouTube) {
url = `https://youtu.be/${id}`
} else {
url = `https://redirect.invidious.io/watch?v=${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
}

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

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

return url
}
}
}

if (parameters.linkText) {
clipboard.write({
bookmark: parameters.linkText,
text: url
})
} else {
clipboard.writeText(url)
return [
{
label: 'Copy Lin&k',
visible: visible && !isInAppUrl,
click: () => {
copy(parameters.linkURL)
}
},
{
label: 'Copy YouTube Link',
visible: visible && isInAppUrl,
click: () => {
copy(transformURL(true))
}
},
{
label: 'Copy Invidious Link',
visible: visible && isInAppUrl,
click: () => {
copy(transformURL(false))
}
}
}]
]
}
})

Expand Down