diff --git a/src/main/index.js b/src/main/index.js index bc06dfef94b0..4232a8da96bd 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -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