Files
2024-09-27 01:07:59 +08:00

51 lines
1.8 KiB
JavaScript
Executable File

const { ipcRenderer } = require('electron')
const { render } = require('./render')
const isImg = target => target.tagName === 'A' && target.parentNode.tagName === 'FIGURE'
const mouseoverFn = function (e){
const { target } = e
if (isImg(target)){
const { parentNode } = target
if (!parentNode.addChild){
const node = parentNode
const wallpaperId = node.getAttribute('data-wallpaper-id')
const url = node.querySelector('img').getAttribute('data-src')
const isPng = Boolean(node.querySelector('.png'))
const [width, height] = node.querySelector('.wall-res').innerHTML.split('x')
const downloadUrl = `https://w.wallhaven.cc/full/${wallpaperId.slice(0, 2)}/wallhaven-${wallpaperId}.${isPng ? 'png' : 'jpg'}`
const options = {
width: width.trim(),
height: height.trim(),
url,
downloadUrl,
}
parentNode.addChild = render(options)
parentNode.addChild.style.zIndex = '9999'
parentNode.appendChild(parentNode.addChild)
}
}
}
const mouseoutFn = function (e){
const { target } = e
if (isImg(target)){
const { parentNode } = target
if (parentNode.addChild){
window.setTimeout(() => {
if (!parentNode.addChild.mouseoverFlag){
parentNode.removeChild(parentNode.addChild)
parentNode.addChild = null
}
}, 80)
}
}
}
ipcRenderer.on('dom-ready', () => {
console.log('================wall-load')
document.querySelector('body').addEventListener('mouseover', mouseoverFn, false)
document.querySelector('body').addEventListener('mouseout', mouseoutFn, false)
})