This commit is contained in:
cutls 2020-01-12 11:38:28 +09:00
parent 51af1590ad
commit ee7ca45b91
2 changed files with 148 additions and 141 deletions

View File

@ -1,189 +1,198 @@
function spotifyConnect() { function spotifyConnect() {
var auth = "https://accounts.spotify.com/authorize?client_id=0f18e54abe0b4aedb4591e353d3aff69&redirect_uri=https://thedesk.top/spotify-connect&response_type=code&scope=user-read-currently-playing"; var auth =
var platform = localStorage.getItem("platform"); 'https://accounts.spotify.com/authorize?client_id=0f18e54abe0b4aedb4591e353d3aff69&redirect_uri=https://thedesk.top/spotify-connect&response_type=code&scope=user-read-currently-playing'
if (platform == "win32") { var platform = localStorage.getItem('platform')
postMessage(["openUrl", auth], "*"); if (platform == 'win32') {
postMessage(["sendSinmpleIpc", "quit"], "*"); postMessage(['openUrl', auth], '*')
postMessage(['sendSinmpleIpc', 'quit'], '*')
} else { } else {
auth = auth + "&state=code"; auth = auth + '&state=code'
$("#spotify-code-show").removeClass("hide"); $('#spotify-code-show').removeClass('hide')
postMessage(["openUrl", auth], "*"); postMessage(['openUrl', auth], '*')
} }
} }
function spotifyAuth() { function spotifyAuth() {
var code = $("#spotify-code").val(); var code = $('#spotify-code').val()
localStorage.setItem("spotify", "code"); localStorage.setItem('spotify', 'code')
localStorage.setItem("spotify-refresh", code); localStorage.setItem('spotify-refresh', code)
$("#spotify-code-show").addClass("hide"); $('#spotify-code-show').addClass('hide')
$("#spotify-enable").addClass("disabled"); $('#spotify-enable').addClass('disabled')
$("#spotify-disable").removeClass("disabled"); $('#spotify-disable').removeClass('disabled')
} }
function spotifyDisconnect() { function spotifyDisconnect() {
localStorage.removeItem("spotify"); localStorage.removeItem('spotify')
localStorage.removeItem("spotify-refresh"); localStorage.removeItem('spotify-refresh')
checkSpotify(); checkSpotify()
} }
function checkSpotify() { function checkSpotify() {
if (localStorage.getItem("spotify")) { if (localStorage.getItem('spotify')) {
$("#spotify-enable").addClass("disabled"); $('#spotify-enable').addClass('disabled')
$("#spotify-disable").removeClass("disabled"); $('#spotify-disable').removeClass('disabled')
} else { } else {
$("#spotify-enable").removeClass("disabled"); $('#spotify-enable').removeClass('disabled')
$("#spotify-disable").addClass("disabled"); $('#spotify-disable').addClass('disabled')
} }
var content = localStorage.getItem("np-temp"); var content = localStorage.getItem('np-temp')
if (!content || content == "" || content == "null") { if (!content || content == '' || content == 'null') {
var content = "#NowPlaying {song} / {album} / {artist}\n{url} #SpotifyWithTheDesk"; var content = '#NowPlaying {song} / {album} / {artist}\n{url} #SpotifyWithTheDesk'
} }
$("#np-temp").val(content); $('#np-temp').val(content)
var flag = localStorage.getItem("artwork"); var flag = localStorage.getItem('artwork')
if (flag) { if (flag) {
$("#awk_yes").prop("checked", true); $('#awk_yes').prop('checked', true)
} else { } else {
$("#awk_no").prop("checked", true); $('#awk_no').prop('checked', true)
} }
} }
function spotifyFlagSave() { function spotifyFlagSave() {
var awk = $("[name=awk]:checked").val(); var awk = $('[name=awk]:checked').val()
if (awk == "yes") { if (awk == 'yes') {
localStorage.setItem("artwork", "yes"); localStorage.setItem('artwork', 'yes')
M.toast({ html: lang.lang_spotify_img, displayLength: 3000 }); M.toast({ html: lang.lang_spotify_img, displayLength: 3000 })
} else { } else {
localStorage.removeItem("artwork"); localStorage.removeItem('artwork')
M.toast({ html: lang.lang_spotify_imgno, displayLength: 3000 }); M.toast({ html: lang.lang_spotify_imgno, displayLength: 3000 })
} }
} }
function nowplaying(mode) { function nowplaying(mode) {
if (mode == "spotify") { if (mode == 'spotify') {
var start = "https://thedesk.top/now-playing?at=" + localStorage.getItem("spotify") + "&rt=" + localStorage.getItem("spotify-refresh"); var start =
var at = localStorage.getItem("spotify"); 'https://thedesk.top/now-playing?at=' +
localStorage.getItem('spotify') +
'&rt=' +
localStorage.getItem('spotify-refresh')
var at = localStorage.getItem('spotify')
if (at) { if (at) {
fetch(start, { fetch(start, {
method: "GET", method: 'GET',
headers: { headers: {
"content-type": "application/json" 'content-type': 'application/json'
} }
}) })
.then(function(response) { .then(function(response) {
if (!response.ok) { if (!response.ok) {
response.text().then(function(text) { response.text().then(function(text) {
setLog(response.url, response.status, text); setLog(response.url, response.status, text)
}); })
} }
return response.json(); return response.json()
}) })
.catch(function(error) { .catch(function(error) {
todo(error); todo(error)
setLog(start, "JSON", error); setLog(start, 'JSON', error)
console.error(error); console.error(error)
}) })
.then(function(json) { .then(function(json) {
console.table(json); console.table(json)
if (json.length < 1) { if (json.length < 1) {
return false; return false
} }
var item = json.item; var item = json.item
var img = item.album.images[0].url; var img = item.album.images[0].url
var flag = localStorage.getItem("artwork"); var flag = localStorage.getItem('artwork')
if (flag) { if (flag) {
postMessage(["bmpImage", [img, 0]], "*"); postMessage(['bmpImage', [img, 0]], '*')
} }
var content = localStorage.getItem("np-temp"); var content = localStorage.getItem('np-temp')
if (!content || content == "" || content == "null") { if (!content || content == '' || content == 'null') {
var content = "#NowPlaying {song} / {album} / {artist}\n{url}"; var content = '#NowPlaying {song} / {album} / {artist}\n{url}'
} }
var regExp = new RegExp("{song}", "g"); var regExp = new RegExp('{song}', 'g')
content = content.replace(regExp, item.name); content = content.replace(regExp, item.name)
var regExp = new RegExp("{album}", "g"); var regExp = new RegExp('{album}', 'g')
content = content.replace(regExp, item.album.name); content = content.replace(regExp, item.album.name)
var regExp = new RegExp("{artist}", "g"); var regExp = new RegExp('{artist}', 'g')
content = content.replace(regExp, item.artists[0].name); content = content.replace(regExp, item.artists[0].name)
var regExp = new RegExp("{url}", "g"); var regExp = new RegExp('{url}', 'g')
content = content.replace(regExp, item.external_urls.spotify); content = content.replace(regExp, item.external_urls.spotify)
var regExp = new RegExp("{composer}", "g"); var regExp = new RegExp('{composer}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
var regExp = new RegExp("{hz}", "g"); var regExp = new RegExp('{hz}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
var regExp = new RegExp("{bitRate}", "g"); var regExp = new RegExp('{bitRate}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
var regExp = new RegExp("{lyricist}", "g"); var regExp = new RegExp('{lyricist}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
var regExp = new RegExp("{bpm}", "g"); var regExp = new RegExp('{bpm}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
var regExp = new RegExp("{genre}", "g"); var regExp = new RegExp('{genre}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
$("#textarea").val(content); $('#textarea').val(content)
}); })
} else { } else {
Swal.fire({ Swal.fire({
type: "info", type: 'info',
title: lang.lang_spotify_acct title: lang.lang_spotify_acct
}); })
} }
} else if (mode == "itunes") { } else if (mode == 'itunes') {
postMessage(["itunes", ""], "*"); postMessage(['itunes', ''], '*')
} }
} }
function npCore(arg) { function npCore(arg) {
if (arg.cmd) { if (arg.cmd) {
console.error(arg); console.error(arg)
return; return
} }
console.table(arg); console.table(arg)
var content = localStorage.getItem("np-temp"); if (arg.raw) {
if (!content || content == "" || content == "null") { $('#textarea').val(arg.data + ' #nowplaying')
var content = "#NowPlaying {song} / {album} / {artist}\n{url}"; return true
} }
var flag = localStorage.getItem("artwork"); var content = localStorage.getItem('np-temp')
var platform = localStorage.getItem("platform"); if (!content || content == '' || content == 'null') {
if (platform == "win32") { var content = '#NowPlaying {song} / {album} / {artist}\n{url}'
}
var flag = localStorage.getItem('artwork')
var platform = localStorage.getItem('platform')
if (platform == 'win32') {
if (flag && arg.path) { if (flag && arg.path) {
media(arg.path, "image/png", "new"); media(arg.path, 'image/png', 'new')
} }
} else if (platform == "darwin") { } else if (platform == 'darwin') {
if (flag && arg.artwork) { if (flag && arg.artwork) {
media(arg.artwork, "image/png", "new"); media(arg.artwork, 'image/png', 'new')
} }
} }
var regExp = new RegExp("{song}", "g"); var regExp = new RegExp('{song}', 'g')
content = content.replace(regExp, arg.name); content = content.replace(regExp, arg.name)
var regExp = new RegExp("{album}", "g"); var regExp = new RegExp('{album}', 'g')
if (arg.album) { if (arg.album) {
if (arg.album.name) { if (arg.album.name) {
content = content.replace(regExp, arg.album.name); content = content.replace(regExp, arg.album.name)
} }
} }
var regExp = new RegExp("{artist}", "g"); var regExp = new RegExp('{artist}', 'g')
content = content.replace(regExp, arg.artist); content = content.replace(regExp, arg.artist)
var regExp = new RegExp("{url}", "g"); var regExp = new RegExp('{url}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
var regExp = new RegExp("{composer}", "g"); var regExp = new RegExp('{composer}', 'g')
content = content.replace(regExp, arg.composer); content = content.replace(regExp, arg.composer)
var regExp = new RegExp("{hz}", "g"); var regExp = new RegExp('{hz}', 'g')
content = content.replace(regExp, arg.sampleRate / 1000 + "kHz"); content = content.replace(regExp, arg.sampleRate / 1000 + 'kHz')
var regExp = new RegExp("{lyricist}", "g"); var regExp = new RegExp('{lyricist}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
var regExp = new RegExp("{bpm}", "g"); var regExp = new RegExp('{bpm}', 'g')
content = content.replace(regExp, ""); content = content.replace(regExp, '')
var regExp = new RegExp("{bitRate}", "g"); var regExp = new RegExp('{bitRate}', 'g')
content = content.replace(regExp, arg.bitRate + "kbps"); content = content.replace(regExp, arg.bitRate + 'kbps')
var regExp = new RegExp("{genre}", "g"); var regExp = new RegExp('{genre}', 'g')
content = content.replace(regExp, arg.genre); content = content.replace(regExp, arg.genre)
$("#textarea").val(content); $('#textarea').val(content)
} }
function spotifySave() { function spotifySave() {
var temp = $("#np-temp").val(); var temp = $('#np-temp').val()
localStorage.setItem("np-temp", temp); localStorage.setItem('np-temp', temp)
M.toast({ html: lang.lang_spotify_np, displayLength: 3000 }); M.toast({ html: lang.lang_spotify_np, displayLength: 3000 })
} }
if (location.search) { if (location.search) {
var m = location.search.match(/\?mode=([a-zA-Z-0-9]+)\&code=(.+)/); var m = location.search.match(/\?mode=([a-zA-Z-0-9]+)\&code=(.+)/)
var mode = m[1]; var mode = m[1]
var codex = m[2]; var codex = m[2]
if (mode == "spotify") { if (mode == 'spotify') {
var coder = codex.split(":"); var coder = codex.split(':')
localStorage.setItem("spotify", coder[0]); localStorage.setItem('spotify', coder[0])
localStorage.setItem("spotify-refresh", coder[1]); localStorage.setItem('spotify-refresh', coder[1])
} else { } else {
} }
} }

View File

@ -4,9 +4,7 @@ function np(mainWindow) {
const app = electron.app const app = electron.app
const fs = require('fs') const fs = require('fs')
const fpath = join(app.getPath('userData'), 'npexec') const fpath = join(app.getPath('userData'), 'npexec')
const npExec = fs.readFileSync(fpath, 'utf8')
const ipc = electron.ipcMain const ipc = electron.ipcMain
const { exec } = require('child_process') const { exec } = require('child_process')
ipc.on('itunes', async (e, args) => { ipc.on('itunes', async (e, args) => {
console.log('Access') console.log('Access')
@ -18,11 +16,7 @@ function np(mainWindow) {
try { try {
const nowplaying = require('itunes-nowplaying-mac') const nowplaying = require('itunes-nowplaying-mac')
let value = await nowplaying() let value = await nowplaying()
if(!value && npExec) { if (!value && npExec) {
exec(npExec, (error, stdout, stderr) => {
e.sender.webContents.send('itunes-np', stdout)
})
} else {
const artwork = await nowplaying.getThumbnailBuffer(value.databaseID) const artwork = await nowplaying.getThumbnailBuffer(value.databaseID)
const base64 = artwork.toString('base64') const base64 = artwork.toString('base64')
value.artwork = base64 value.artwork = base64
@ -31,12 +25,16 @@ function np(mainWindow) {
} catch (error) { } catch (error) {
// エラーを返す // エラーを返す
console.error(error) console.error(error)
e.sender.webContents.send('itunes-np', error) try {
const npExec = fs.readFileSync(fpath, 'utf8')
exec(npExec, (error, stdout, stderr) => {
e.sender.webContents.send('itunes-np', { raw: true, data: stdout })
})
} catch (e) {}
} }
} else { } else {
} }
} }
}) })
} }
exports.TheDeskNowPlaying = np exports.TheDeskNowPlaying = np