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 Feb 16, 2022
1 parent 7a4b600 commit ad3ec3f
Show file tree
Hide file tree
Showing 2 changed files with 51 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 @@ -388,10 +388,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
61 changes: 48 additions & 13 deletions src/renderer/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import $ from 'jquery'
import { marked } from 'marked'
import Parser from 'rss-parser'

import { IpcChannels } from '../constants'

let ipcRenderer = null

Vue.directive('observe-visibility', ObserveVisibility)
Expand Down Expand Up @@ -331,7 +333,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 @@ -348,7 +354,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 @@ -361,29 +367,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 @@ -404,8 +416,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 All @@ -430,6 +444,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 ad3ec3f

Please sign in to comment.