Skip to content

Commit

Permalink
! Fix handling of youtube link middle mouse click
Browse files Browse the repository at this point in the history
To properly open new windows
  • Loading branch information
PikachuEXE committed May 12, 2022
1 parent 252f85c commit e4b7c9b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,11 @@ function runApp() {
return powerSaveBlocker.start('prevent-display-sleep')
})

ipcMain.on(IpcChannels.CREATE_NEW_WINDOW, () => {
ipcMain.on(IpcChannels.CREATE_NEW_WINDOW, (_e, { windowStartupUrl = null } = { }) => {
createWindow({
replaceMainWindow: false,
showWindowNow: true
showWindowNow: true,
windowStartupUrl: windowStartupUrl
})
})

Expand Down
59 changes: 46 additions & 13 deletions src/renderer/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ export default Vue.extend({
const isYoutubeLink = youtubeUrlPattern.test(el.href)

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') {
// Let user know opening external link is disabled via setting
this.showToast({
Expand All @@ -366,7 +370,7 @@ export default Vue.extend({
}
},

handleYoutubeLink: function (href) {
handleYoutubeLink: function (href, { doCreateNewWindow = false } = { }) {
this.getYoutubeUrlInfo(href).then((result) => {
switch (result.urlType) {
case 'video': {
Expand All @@ -379,29 +383,35 @@ export default Vue.extend({
if (playlistId && playlistId.length > 0) {
query.playlistId = playlistId
}
this.$router.push({
path: `/watch/${videoId}`,
query: query
const path = `/watch/${videoId}`
this.openInternalPath({
path,
query,
doCreateNewWindow
})
break
}

case 'playlist': {
const { playlistId, query } = result

this.$router.push({
path: `/playlist/${playlistId}`,
query
const path = `/playlist/${playlistId}`
this.openInternalPath({
path,
query,
doCreateNewWindow
})
break
}

case 'search': {
const { searchQuery, query } = result

this.$router.push({
path: `/search/${encodeURIComponent(searchQuery)}`,
query
const path = `/search/${encodeURIComponent(searchQuery)}`
this.openInternalPath({
path,
query,
doCreateNewWindow
})
break
}
Expand All @@ -422,8 +432,10 @@ export default Vue.extend({
case 'channel': {
const { channelId, subPath } = result

this.$router.push({
path: `/channel/${channelId}/${subPath}`
const path = `/channel/${channelId}/${subPath}`
this.openInternalPath({
path,
doCreateNewWindow
})
break
}
Expand Down Expand Up @@ -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 () {
ipcRenderer.on('openUrl', (event, url) => {
if (url) {
Expand Down

0 comments on commit e4b7c9b

Please sign in to comment.