Skip to content

Commit

Permalink
✨ Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerluiz committed May 28, 2020
1 parent 6a07eaf commit 9d8e7df
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 47 deletions.
25 changes: 14 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ global.sharedObj = { title: 'N/A', paused: true }
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow, view, miniplayer, lyrics, settings

const defaultUrl = 'https://music.youtube.com'

let mainWindowParams = {
url: 'https://music.youtube.com',
url: defaultUrl,
width: 1500,
height: 800,
}
Expand Down Expand Up @@ -190,19 +192,20 @@ function createWindow() {
view = new BrowserView({
webPreferences: {
nodeIntegration: true,
webviewTag: true,
preload: path.join(app.getAppPath(), '/utils/injectControls.js'),
},
})

// mainWindow.loadFile(path.join(app.getAppPath(), "/pages/home/home.html"));
mainWindow.loadFile(
path.join(
__dirname,
'./pages/shared/window-buttons/window-buttons.html'
),
{ search: 'page=home/home&title=YouTube Music' }
)
mainWindow.setBrowserView(view)

mainWindow.addBrowserView(view)

view.setBounds(calcYTViewSize(settingsProvider, mainWindow))

Expand Down Expand Up @@ -279,10 +282,7 @@ function createWindow() {
}
`)
})
setInterval(() => {
// console.log(getAll())
// document.querySelector('ytmusic-player-bar')
}, 1000)

view.webContents.on('media-started-playing', function() {
if (!infoPlayer.hasInitialized()) {
infoPlayer.init(view)
Expand All @@ -304,16 +304,16 @@ function createWindow() {
})

view.webContents.on('did-start-navigation', function(_) {
loadAudioOutput()
loadCustomTheme()

view.webContents.executeJavaScript('window.location').then(location => {
if (location.hostname != 'music.youtube.com') {
mainWindow.send('off-the-road')
global.on_the_road = false
} else {
mainWindow.send('on-the-road')
global.on_the_road = true

loadAudioOutput()
loadCustomTheme()
}
})
})
Expand Down Expand Up @@ -812,7 +812,10 @@ function createWindow() {
})

ipcMain.on('reset-url', () => {
mainWindow.getBrowserView().webContents.loadURL(mainWindowParams.url)
mainWindowParams.url = defaultUrl

const options = { extraHeaders: 'pragma: no-cache\n' }
view.webContents.loadURL(mainWindowParams.url, options)
})

ipcMain.on('show-editor-theme', function() {
Expand Down
60 changes: 34 additions & 26 deletions pages/shared/window-buttons/window-buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const currentWindow = remote.getCurrentWindow()
const winElement = document.getElementById('win')
const macElement = document.getElementById('mac')

const webview = document.querySelector('webview')

if (isMac()) {
winElement.remove()
macElement.classList.remove('hide')
Expand Down Expand Up @@ -38,32 +40,38 @@ ipc.on('window-is-maximized', function(_, value) {
}
})

document.addEventListener('DOMContentLoaded', function() {
checkUrlParams()

document
.getElementById('btn-minimize')
.addEventListener('click', function() {
currentWindow.minimize()
})

document
.getElementById('btn-maximize')
.addEventListener('click', function() {
if (!currentWindow.isMaximized()) {
currentWindow.maximize()
} else {
currentWindow.unmaximize()
}
})

document.getElementById('btn-close').addEventListener('click', function() {
currentWindow.close()
})
})
document.addEventListener(
'DOMContentLoaded',
function() {
checkUrlParams()

document
.getElementById('btn-minimize')
.addEventListener('click', function() {
currentWindow.minimize()
})

document
.getElementById('btn-maximize')
.addEventListener('click', function() {
if (!currentWindow.isMaximized()) {
currentWindow.maximize()
} else {
currentWindow.unmaximize()
}
})

document
.getElementById('btn-close')
.addEventListener('click', function() {
currentWindow.close()
})
},
false
)

// ENABLE FOR DEBUG
// document.getElementById("webview").addEventListener("dom-ready", function(){ webview.openDevTools(); });
// webview.addEventListener("did-start-loading", () => { webview.openDevTools(); });

function checkUrlParams() {
const params = new URL(window.location).searchParams
Expand All @@ -74,15 +82,15 @@ function checkUrlParams() {
let hide = params.get('hide')

if (page) {
document.getElementById('webview').src = `../../${page}.html`
webview.src = `../../${page}.html`
}

if (icon) {
document.getElementById('icon').innerText = icon
}

if (title) {
document.getElementById('music-title').innerText = title
// document.getElementById('music-title').innerText = title
}

if (hide) {
Expand Down
44 changes: 34 additions & 10 deletions utils/injectControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ var content = remote.getCurrentWebContents()
content.addListener('dom-ready', function() {
createContextMenu()

content.executeJavaScript('window.location').then(location => {
if (location.hostname == 'music.youtube.com') {
createMiddleContent()
createRightContent()
playerBarScrollToChangeVolume()
createPlayerBarContent()
} else {
// Show menu off the road;
}
})
content
.executeJavaScript('window.location')
.then(location => {
if (location.hostname == 'music.youtube.com') {
createMiddleContent()
createRightContent()
playerBarScrollToChangeVolume()
createPlayerBarContent()
} else {
createOffTheRoadContent()
}
})
.catch(_ => ipcRenderer.send('debug', 'error on inject'))
})

function createContextMenu() {
Expand Down Expand Up @@ -245,3 +248,24 @@ function playerBarScrollToChangeVolume() {
});
`)
}

function createOffTheRoadContent() {
content.executeJavaScript(
`
var body = document.getElementsByTagName('body')[0];
var elementBack = document.createElement('i');
elementBack.id = 'ytmd_lyrics';
elementBack.classList.add('material-icons');
elementBack.style.cssFloat = "left";
elementBack.style.cursor = "pointer";
elementBack.innerText = 'arrow_back';
elementBack.addEventListener('click', function() { ipcRenderer.send('reset-url') } )
body.prepend(elementBack);
`
)
}

0 comments on commit 9d8e7df

Please sign in to comment.