! Fix handling of youtube link middle mouse click (#2083)

To properly open new windows
This commit is contained in:
PikachuEXE 2022-05-31 02:32:38 +08:00 committed by GitHub
parent 0ca4212934
commit 2db528b8dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 15 deletions

View File

@ -433,10 +433,11 @@ function runApp() {
return powerSaveBlocker.start('prevent-display-sleep') return powerSaveBlocker.start('prevent-display-sleep')
}) })
ipcMain.on(IpcChannels.CREATE_NEW_WINDOW, () => { ipcMain.on(IpcChannels.CREATE_NEW_WINDOW, (_e, { windowStartupUrl = null } = { }) => {
createWindow({ createWindow({
replaceMainWindow: false, replaceMainWindow: false,
showWindowNow: true showWindowNow: true,
windowStartupUrl: windowStartupUrl
}) })
}) })

View File

@ -349,7 +349,11 @@ export default Vue.extend({
const isYoutubeLink = youtubeUrlPattern.test(el.href) const isYoutubeLink = youtubeUrlPattern.test(el.href)
if (isYoutubeLink) { if (isYoutubeLink) {
this.handleYoutubeLink(el.href) // `auxclick` is the event type for non-left click
// https://developer.mozilla.org/en-US/docs/Web/API/Element/auxclick_event
this.handleYoutubeLink(el.href, {
doCreateNewWindow: event.type === 'auxclick'
})
} else if (this.externalLinkHandling === 'doNothing') { } else if (this.externalLinkHandling === 'doNothing') {
// Let user know opening external link is disabled via setting // Let user know opening external link is disabled via setting
this.showToast({ this.showToast({
@ -366,7 +370,7 @@ export default Vue.extend({
} }
}, },
handleYoutubeLink: function (href) { handleYoutubeLink: function (href, { doCreateNewWindow = false } = { }) {
this.getYoutubeUrlInfo(href).then((result) => { this.getYoutubeUrlInfo(href).then((result) => {
switch (result.urlType) { switch (result.urlType) {
case 'video': { case 'video': {
@ -379,9 +383,11 @@ export default Vue.extend({
if (playlistId && playlistId.length > 0) { if (playlistId && playlistId.length > 0) {
query.playlistId = playlistId query.playlistId = playlistId
} }
this.$router.push({ const path = `/watch/${videoId}`
path: `/watch/${videoId}`, this.openInternalPath({
query: query path,
query,
doCreateNewWindow
}) })
break break
} }
@ -389,9 +395,11 @@ export default Vue.extend({
case 'playlist': { case 'playlist': {
const { playlistId, query } = result const { playlistId, query } = result
this.$router.push({ const path = `/playlist/${playlistId}`
path: `/playlist/${playlistId}`, this.openInternalPath({
query path,
query,
doCreateNewWindow
}) })
break break
} }
@ -399,9 +407,11 @@ export default Vue.extend({
case 'search': { case 'search': {
const { searchQuery, query } = result const { searchQuery, query } = result
this.$router.push({ const path = `/search/${encodeURIComponent(searchQuery)}`
path: `/search/${encodeURIComponent(searchQuery)}`, this.openInternalPath({
query path,
query,
doCreateNewWindow
}) })
break break
} }
@ -422,8 +432,10 @@ export default Vue.extend({
case 'channel': { case 'channel': {
const { channelId, subPath } = result const { channelId, subPath } = result
this.$router.push({ const path = `/channel/${channelId}/${subPath}`
path: `/channel/${channelId}/${subPath}` this.openInternalPath({
path,
doCreateNewWindow
}) })
break break
} }
@ -458,6 +470,27 @@ export default Vue.extend({
}) })
}, },
openInternalPath: function({ path, doCreateNewWindow, query = {} }) {
if (this.usingElectron && doCreateNewWindow) {
const { ipcRenderer } = require('electron')
// Combine current document path and new "hash" as new window startup URL
const newWindowStartupURL = [
window.location.href.split('#')[0],
`#${path}?${(new URLSearchParams(query)).toString()}`
].join('')
ipcRenderer.send(IpcChannels.CREATE_NEW_WINDOW, {
windowStartupUrl: newWindowStartupURL
})
} else {
// Web
this.$router.push({
path,
query
})
}
},
enableOpenUrl: function () { enableOpenUrl: function () {
ipcRenderer.on('openUrl', (event, url) => { ipcRenderer.on('openUrl', (event, url) => {
if (url) { if (url) {