Skip to content

Commit

Permalink
✨ autosave for discord settings
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerluiz committed Jul 19, 2020
1 parent 8a8465e commit 61df117
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 61 deletions.
4 changes: 2 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,8 @@ function createWindow() {
backgroundColor: '#232323',
width: 600,
minWidth: 600,
height: 300,
minHeight: 300,
height: 220,
minHeight: 220,
autoHideMenuBar: false,
skipTaskbar: false,
webPreferences: {
Expand Down
8 changes: 0 additions & 8 deletions src/pages/settings/discord_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@
</div>
</div>
</div>

<div class="row center-align">
<a
class="btn red center"
id="btn-save"
i18n="i18n_LABEL_SAVE"
></a>
</div>
</div>

<script src="../../../src/assets/js/materialize.min.js"></script>
Expand Down
14 changes: 8 additions & 6 deletions src/pages/settings/discord_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ function loadSettings() {
discordSettings.hideIdle
}

document.addEventListener('DOMContentLoaded', function () {
loadSettings()
})

buttonSave.addEventListener('click', function () {
function save() {
settingsProvider.set('discord-presence-settings', {
details: document.getElementById('settings-discord-show-title').checked,
state: document.getElementById('settings-discord-show-artist').checked,
time: document.getElementById('settings-discord-show-time').checked,
hideIdle: document.getElementById('settings-discord-show-idle').checked,
})
}

document.addEventListener('DOMContentLoaded', function () {
loadSettings()
buttonSave.innerText = __.trans('LABEL_SAVED')
})

document.querySelectorAll('input').forEach((input) => {
input.addEventListener('click', () => save())
})
91 changes: 46 additions & 45 deletions src/providers/infoPlayerProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function getTrackInfo() {

function isPaused(webContents) {
webContents
.executeJavaScript(`document.getElementsByTagName('video')[0].paused;`)
.executeJavaScript(`document.querySelector('video').paused;`)
.then((isPaused) => {
debug(`Is paused: ${isPaused}`)
player.isPaused = isPaused
Expand All @@ -86,7 +86,7 @@ function isPaused(webContents) {
function getTitle(webContents) {
webContents
.executeJavaScript(
`document.getElementsByClassName('title ytmusic-player-bar')[0].innerText;`
`document.querySelector('.title.ytmusic-player-bar').textContent;`
)
.then((title) => {
debug(`Title is: ${title}`)
Expand All @@ -98,7 +98,7 @@ function getTitle(webContents) {
function getDuration(webContents) {
webContents
.executeJavaScript(
`document.getElementById('progress-bar').getAttribute('aria-valuemax');`
`document.querySelector('#progress-bar').getAttribute('aria-valuemax');`
)
.then((duration) => {
debug(`Duration is: ${parseInt(duration)}`)
Expand All @@ -116,7 +116,7 @@ function getDuration(webContents) {
function getLikeStatus(webContents) {
webContents
.executeJavaScript(
`document.getElementById('like-button-renderer').getAttribute('like-status');`
`document.querySelector('#like-button-renderer').getAttribute('like-status');`
)
.then((likeStatus) => {
debug(`Like status is: ${likeStatus}`)
Expand All @@ -132,7 +132,7 @@ function getLikeStatus(webContents) {
function getSeekbarPosition(webContents) {
webContents
.executeJavaScript(
`document.getElementById('progress-bar').getAttribute('aria-valuenow');`
`document.querySelector('#progress-bar').getAttribute('aria-valuenow');`
)
.then((position) => {
debug(`Seekbar position is: ${parseInt(position)}`)
Expand All @@ -147,7 +147,7 @@ function getSeekbarPosition(webContents) {
function getVolume(webContents) {
webContents
.executeJavaScript(
`document.getElementsByClassName('volume-slider style-scope ytmusic-player-bar')[0].getAttribute('value');`
`document.querySelector('.volume-slider.ytmusic-player-bar').getAttribute('value');`
)
.then((volume) => {
debug(`Volume % is: ${parseInt(volume)}`)
Expand All @@ -160,14 +160,14 @@ function getAuthor(webContents) {
webContents
.executeJavaScript(
`
var bar = document.getElementsByClassName('subtitle ytmusic-player-bar')[0];
if (bar.getElementsByClassName('yt-simple-endpoint yt-formatted-string')[0]) {
title = bar.getElementsByClassName('yt-simple-endpoint yt-formatted-string')[0].innerText;
} else if (bar.getElementsByClassName('byline ytmusic-player-bar')[0]) {
title = bar.getElementsByClassName('byline ytmusic-player-bar')[0].innerText;
}
title;
var bar = document.getElementsByClassName('subtitle ytmusic-player-bar')[0];
if (bar.getElementsByClassName('yt-simple-endpoint yt-formatted-string')[0]) {
title = bar.getElementsByClassName('yt-simple-endpoint yt-formatted-string')[0].textContent;
} else if (bar.getElementsByClassName('byline ytmusic-player-bar')[0]) {
title = bar.getElementsByClassName('byline ytmusic-player-bar')[0].textContent;
}
title;
`
)
.then((author) => {
Expand All @@ -181,19 +181,19 @@ function getAlbum(webContents) {
webContents
.executeJavaScript(
`
var album = '';
var player_bar = document.getElementsByClassName("byline ytmusic-player-bar")[0].children;
var arr_player_bar = Array.from(player_bar);
arr_player_bar.forEach( function(data, index) {
if (data.getAttribute('href') != null && data.getAttribute('href').includes('browse')) {
album = data.innerText;
}
} )
album
`
var album = '';
var player_bar = document.getElementsByClassName("byline ytmusic-player-bar")[0].children;
var arr_player_bar = Array.from(player_bar);
arr_player_bar.forEach( function(data, index) {
if (data.getAttribute('href') != null && data.getAttribute('href').includes('browse')) {
album = data.innerText;
}
} )
album
`
)
.then((album) => {
debug(`Album is: ${album}`)
Expand All @@ -206,19 +206,19 @@ function getCover(webContents) {
webContents
.executeJavaScript(
`
var cover;
var cover;
var thumbnail = document.getElementsByClassName('thumbnail ytmusic-player no-transition')[0];
var image = thumbnail.getElementsByClassName('yt-img-shadow')[0].src;
var thumbnail = document.querySelector('.thumbnail.ytmusic-player.no-transition');
var image = thumbnail.querySelector('.yt-img-shadow').src;
cover = image;
cover = image;
if (cover.includes("data:image")) {
cover = document.getElementsByClassName("image ytmusic-player-bar")[0].src
}
if (cover.includes("data:image")) {
cover = document.querySelector(".image.ytmusic-player-bar").src;
}
cover;
`
cover;
`
)
.then((cover) => {
debug(`Cover is: ${cover}`)
Expand All @@ -230,7 +230,7 @@ function getCover(webContents) {
function getRepeatType(webContents) {
webContents
.executeJavaScript(
`document.getElementsByTagName("ytmusic-player-bar")[0].getAttribute("repeat-mode_");`
`document.querySelector("ytmusic-player-bar").getAttribute("repeat-mode_");`
)
.then((repeatType) => {
debug(`Repeat type is: ${repeatType}`)
Expand All @@ -242,7 +242,7 @@ function getRepeatType(webContents) {
function getUrl(webContents) {
webContents
.executeJavaScript(
`document.getElementsByClassName('ytp-title-link yt-uix-sessionlink')[0].href`
`document.querySelector('.ytp-title-link.yt-uix-sessionlink').href`
)
.then((url) => {
if (url) {
Expand Down Expand Up @@ -333,7 +333,7 @@ function addToLibary(webContents) {
function isVideo(webContents) {
webContents
.executeJavaScript(
`document.getElementById('player').hasAttribute('video-mode_')`
`document.querySelector('#player').hasAttribute('video-mode_')`
)
.then((isVideo) => {
track.isVideo = !!isVideo
Expand All @@ -345,7 +345,7 @@ function isVideo(webContents) {
function isAdvertisement(webContents) {
webContents
.executeJavaScript(
`document.getElementsByClassName('advertisement ')[0].hasAttribute('hidden')`
`document.querySelector('.advertisement').hasAttribute('hidden')`
)
.then((isAdvertisement) => {
track.isAdvertisement = !isAdvertisement
Expand Down Expand Up @@ -447,11 +447,12 @@ function firstPlay(webContents) {
webContents
.executeJavaScript(
`
var carousel = document.getElementsByClassName('carousel')[0];
var firstChild = carousel.querySelector('#items').children[0];
var playButton = firstChild.querySelector('#play-button')
playButton.click();`
var carousel = document.querySelector('.carousel');
var firstChild = carousel.querySelector('#items').children[0];
var playButton = firstChild.querySelector('#play-button')
playButton.click();
`
)
.then()
.catch((_) => console.log('error firstPlay'))
Expand Down

0 comments on commit 61df117

Please sign in to comment.