MVP Wallpaper Setter

This commit is contained in:
Elizabeth Cray 2023-06-18 04:17:25 -04:00
commit 176a46c2e9
9 changed files with 280 additions and 0 deletions

44
project.json Normal file
View File

@ -0,0 +1,44 @@
{
"file" : "scene.html",
"description": "Storage limit: 10240 images because of windows max path and locastorage limits",
"general" :
{
"properties" :
{
"schemecolor" :
{
"order" : 0,
"text" : "ui_browse_properties_scheme_color",
"type" : "color",
"value" : "0.8980392156862745 0.18823529411764706 0.9294117647058824"
},
"dir" :
{
"index" : 2,
"order": 102,
"mode" : "fetchall",
"text" : "Wallpaper Dir",
"type" : "directory"
},
"uri" :
{
"index" : 1,
"order" : 101,
"text" : "Socket URI",
"type" : "textinput",
"value" : ""
},
"systemid" :
{
"index" : 0,
"order" : 100,
"text" : "System ID",
"type" : "textinput",
"value" : ""
}
}
},
"title" : "PluralKit",
"type" : "scene",
"version" : 0
}

22
scene.html Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<!-- Webpage that uses Bootstrap to fill the background with an image from JS -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bootstrap Background</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="scripts/scene.js"></script>
<!-- LiveJS is not supported when run with the file:// protocol -->
<!-- <script src="https://livejs.com/live.js"></script> -->
<style>
body {
/* background-image: url('https://picsum.photos/3840/2160'); */
background-size: cover;
}
</style>
</head>
<body id="wallpaper">
</body>
</html>

45
scene.json Normal file
View File

@ -0,0 +1,45 @@
{
"camera" :
{
"center" : "0.00000 0.00000 -1.00000",
"eye" : "0.00000 0.00000 0.00000",
"up" : "0.00000 1.00000 0.00000"
},
"general" :
{
"ambientcolor" : "0.30000 0.30000 0.30000",
"bloom" : false,
"bloomhdrfeather" : 0.1,
"bloomhdriterations" : 8,
"bloomhdrscatter" : 1.619,
"bloomhdrstrength" : 2.0,
"bloomhdrthreshold" : 1.0,
"bloomstrength" : 2.0,
"bloomthreshold" : 0.64999998,
"camerafade" : true,
"cameraparallax" : false,
"cameraparallaxamount" : 0.5,
"cameraparallaxdelay" : 0.1,
"cameraparallaxmouseinfluence" : 0.5,
"camerapreview" : true,
"camerashake" : false,
"camerashakeamplitude" : 0.5,
"camerashakeroughness" : 1.0,
"camerashakespeed" : 3.0,
"clearcolor" : "0.70000 0.70000 0.70000",
"clearenabled" : true,
"farz" : 10000.0,
"fov" : 50.0,
"hdr" : false,
"nearz" : 0.0099999998,
"orthogonalprojection" :
{
"height" : 2160,
"width" : 3840
},
"skylightcolor" : "0.30000 0.30000 0.30000",
"zoom" : 1.0
},
"objects" : [],
"version" : 1
}

169
scripts/scene.js Normal file
View File

@ -0,0 +1,169 @@
let socket
let socketStatus = false
let jsonValid = (json) => {
try {
JSON.parse(json);
if (json == null) {
return false;
}
} catch (e) {
return false;
}
return true;
}
let setWallpaper = (path) => {
console.log(`Setting wallpaper to ${path}`)
let wallpaper = document.getElementById('wallpaper')
// wallpaper.src = path
wallpaper.style.backgroundImage = `url(${path})`
}
let dumpStorage = () => {
console.log('%cDumping Local Storage', 'color: #EBCB8B; font-family: monospace; font-size: 18px;')
console.log(' Member:' + localStorage.getItem('member'))
// console.log(' Token' + localStorage.getItem('token'))
console.log(' System:' + localStorage.getItem('systemId'))
console.log(' Socket:' + localStorage.getItem('socket'))
console.log(' Wallpapers:' + localStorage.getItem('wallpapers'))
}
let grabLatestFronter = () => {
let systemId = localStorage.getItem('systemId')
if (systemId != null) {
var xhr = new XMLHttpRequest()
xhr.withCredentials = true
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
// console.log(this.responseText)
let fronter = JSON.parse(this.responseText).members[0].id
localStorage.setItem('member', fronter)
console.log(`%cFronter: ${fronter}`, 'color: #ECEFF4; font-family: monospace;')
pickWallpaper()
}
})
xhr.open("GET", "https://api.pluralkit.me/v2/systems/"+systemId+"/fronters")
xhr.send()
}else{
localStorage.setItem('member', 'null')
}
}
let pickWallpaper = () => {
let wallpapers = localStorage.getItem('wallpapers')
if (jsonValid(wallpapers)) {
wallpapers = JSON.parse(localStorage.getItem('wallpapers'))
} else {
wallpapers = []
console.log('%cWallpapers not found in Local Storage', 'color: #BF616A; font-size: 18px; font-family: monospace;')
}
// for (let index = wallpapers.length-1; index >= 0; index--) {
// if (!wallpapers.includes(localStorage.getItem('member'))) {
// wallpapers.splice(index, 1)
// }
// }
let validWallpapers = []
wallpapers.forEach(wp => {
if (wp.includes(localStorage.getItem('member'))) {
validWallpapers.push(wp)
}
})
let wallpaper = "file:///" + validWallpapers[Math.floor(Math.random() * validWallpapers.length)]
if (validWallpapers.length == 0) {
wallpaper = "https://picsum.photos/3840/2160"
}
setWallpaper(wallpaper)
}
let loadSocket = () => {
console.log('%cLoading Socket', 'color: #ECEFF4; font-family: monospace;')
if (socket){
try {
socket.close()
} catch (error) {
console.log('%cSocket not found', 'color: #BF616A; font-size: 18px; font-family: monospace;')
}
}
socket = new WebSocket(localStorage.getItem('socket'))
socket.onopen = (e) => {
console.log('%cSocket Opened', 'color: #A3BE8C; font-size: 18px; font-family: serif;')
socketStatus = true
}
socket.onmessage = (e) => {
console.log('%cMessage Received', 'color: #ECEFF4; font-family: monospace;')
// e.data contains member ID
// console.dir(e)
localStorage.setItem('member', e.data)
pickWallpaper()
// Copilot-generated Code
// let data = JSON.parse(e.data)
// if (data.type === 'wallpaper') {
// setWallpaper(data.path)
// }
}
socket.onclose = (e) => {
console.log('%cSocket Closed', 'color: #BF616A; font-size: 18px; font-family: serif;')
socketStatus = false
setTimeout(() => {
loadSocket()
}, 1200)
}
socket.onerror = (e) => {
console.log('%cSocket Error', 'color: #BF616A; font-size: 18px; font-family: monospace;')
console.dir(e)
}
}
window.wallpaperPropertyListener = {
applyUserProperties: function(properties) {
// Grab PKit token, System ID, and Socket URI
// if (properties.token) {
// // config.token = properties.token.value
// localStorage.setItem('token', properties.token.value)
// }
if (properties.systemid) {
// config.systemId = properties.systemid.value
localStorage.setItem('systemId', properties.systemid.value)
}
if (properties.uri) {
// config.socket = properties.uri.value
localStorage.setItem('socket', properties.uri.value)
}
dumpStorage()
// Reload Socket
if (socketStatus) {
socketStatus = false
socket.close()
}
loadSocket()
},
userDirectoryFilesAddedOrChanged: function(propertyName, changedFiles) {
// Do something with the list of added files here
// console.dir(propertyName)
console.dir(changedFiles) // Array of filepaths added
let wallpapers = localStorage.getItem('wallpapers')
if (jsonValid(wallpapers)) {
wallpapers = JSON.parse(localStorage.getItem('wallpapers'))
} else {
wallpapers = []
console.log('%cWallpapers not found in Local Storage', 'color: #BF616A; font-size: 18px; font-family: monospace;')
}
changedFiles.forEach((file) => {
if (!wallpapers.includes(file)) {
wallpapers.push(file)
}
})
localStorage.setItem('wallpapers', JSON.stringify(wallpapers))
},
}
window.onload = () => {
console.log("%cPage Loaded.", "background: #2E3440; color: #A3BE8C; font-size: 24px;")
// Set default member
dumpStorage()
grabLatestFronter()
loadSocket()
}