深夜了,制作一个简单的桌面跳舞女郎小程序吧

Mr_MAO 3小时前 11

大家写代码很辛苦,无聊的时候运行一下这个小程序,放松一下心情☺~


import win.ui;
/*DSG{{*/
var winform = win.form(text="桌面跳舞女郎";right=615;bottom=407;border="none";clipch=1;exmode="toolwindow")
winform.add()
/*}}*/

//-------弹出菜单---------
import win.ui.menu;
winform.popmenu = win.ui.popmenu(winform);
winform.popmenu.add("置顶显示", 
    function(id){
        var ischecked = winform.popmenu.checked(1); 
        winform.popmenu.setString(1,ischecked ?  "置顶显示" : "取消置顶" );
        win.setTopmost(winform.hwnd, not ischecked);
        winform.popmenu.check(1,not ischecked); 
    }
);
winform.popmenu.add();
winform.popmenu.add('退出',function(id){ winform.close() });

import win.util.tray;
tray = win.util.tray(winform);
tray.tip = "桌面跳舞女郎";
tray.pop("右键点击画面可以关闭程序","《桌面跳舞女郎》",,3);

winform.onTrayMessage = {
    [0x205/*_WM_RBUTTONUP*/] = function(wParam){ 
        winform.popmenu.popup() 
    };
    [0x203/*_WM_LBUTTONDBLCLK*/] = function(wParam){ 
        win.setForeground(winform.hwnd)
    };
}

winform.onClose = function(){
    if(tray){ tray.delete(); tray = null;}
}

var urlVideoCodes = {
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1VUQRCXFNPThEDBAQGPiRbUWFfAxJAWVNpUUYcR1dUDA";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1GDgdHXFNEThECCQMFPjZeWUFFADFdSEtpUUUcR1dUDA";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1dAABTWVsZBEYEBAdpMhhcSVN0DRZIVW1XEyhcX1xDBRIcR1dUDA";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1GDgdHXFNEThECBwAGPjtbREZaBDRTQEBfAhJtWFNUTwBXUl8";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1GDgdHXFNEThICAAoAPiFbX15XIxZbXFdPPkcAHkVTAxo";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1VUQRCXFNPThEDBwcEPjhRRF1dFAVdbwIHTwBXUl8";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1VUQRCXFNPThIDCQsPPjVXXFlXPkcDHkVTAxo";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1VUQRCXFNPThIDCQsGPjlbU1tpUUQcR1dUDA";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1GDgdHXFNEThEDAQsGPjtTSV5XMhRTQl5TFQNtAAAYFhJQXQ";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1GDgdHXFNEThICBAcGPjBeX0BfACRdXG0GVVlFVVBb";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1dAABTWVsZB0YABAdpIBtbQ0FXJxhKSW1eABUcR1dUDA";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1GDgdHXFNEThICBwQEPjpbXFN3GwJeb1pXA1lFVVBb";
    "CQNGQEEMTlhFR0UYCARFX0BdEh9dQBxTFFhbXVNRBAQdQF1GER5cV39ZBRJeQx1VUQRCXFNPThEDBwEDPjZBWFBPNh5cRFdEPkcDHkVTAxo";
}

import web.view;
var userDataDir = io.appData("random-online-player\webview2-user-data-transparent");
var wb = web.view(winform,{
    userDataDir = userDataDir;
    startArguments = {
        autoplayPolicy = "no-user-gesture-required";
        allowFileAccessFromFiles = true;
    };
});

wb.transparent = true;
wb.enableDefaultContextMenus(false);
wb.enableDevTools(false);
wb.enableStatusBar(false);

wb.external = {
    startMove = function(){
        ::User32.ReleaseCapture();
        ::User32.SendMessage(winform.hwnd,0xA1/*_WM_NCLBUTTONDOWN*/,2/*_HTCAPTION*/,0);
        return true;
    };

    closeApp = function(){
        winform.close();
        return true;
    }
};

import crypt;
var decodeUrl = function(code){
    var key = "aw2026";
    var data = crypt.decodeUrlBase64(code);
    var out = raw.buffer(#data);

    for(i=1;#data){
        out[i] = data[i] ^ key[(i-1) % #key + 1];
    }

    return tostring(out);
}

var videos = [];
for(i,code in urlVideoCodes){
    table.push(videos,decodeUrl(code));
}

var videoJson = JSON.stringify(videos);

/*网页内容{{*/
var html = string.concat(`<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>在线视频播放器</title>
<style>
html,body{
    margin:0;
    width:100%;
    height:100%;
    overflow:hidden;
    background:transparent !important;
    color:#fff;
    font-family:"Microsoft YaHei",Arial,sans-serif;
    user-select:none;
}
#player{
    width:100%;
    height:100%;
    background:transparent !important;
    object-fit:contain;
    display:block;
    opacity:0;
    transition:opacity 1.2s ease-in-out;
    cursor:move;
}
#player.fade-in{
    opacity:1;
}
#player.fade-out{
    opacity:0;
}
#hint{
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
    padding:14px 20px;
    border-radius:8px;
    background:rgba(0,0,0,.45);
    color:#fff;
    font-size:14px;
    display:none;
}
#bar{
    display:none;
}
#error{
    color:#ffb0b0;
}
</style>
</head>
<body>
<video id="player" autoplay muted playsinline preload="auto"></video>

<div id="hint">单击这里开始/继续播放</div>

<div id="bar">
    <div id="title"></div>
    <span id="count"></span>
    <span id="error"></span>
</div>

<script>
const videos = `, videoJson, `;

const player = document.getElementById('player');
const title = document.getElementById('title');
const count = document.getElementById('count');
const hint = document.getElementById('hint');
const errBox = document.getElementById('error');

const fadeOutMs = 1200;
const fadeOutSeconds = fadeOutMs / 1000;

let lastIndex = -1;
let retryTimer = 0;
let switching = false;
let fadingForNaturalEnd = false;
let currentToken = 0;

function normalizeVideoUrl(url){
    url = String(url);
    if(/^https?:\/\//i.test(url) || /^file:\/\//i.test(url)){
        return url;
    }
}

function basename(url){
    url = String(url).split('?')[0].split('#')[0].replace(/\\/g,'/');
    var name = url.substring(url.lastIndexOf('/') + 1);

    try{
        return decodeURIComponent(name);
    }
    catch(e){
        return name;
    }
}

function mediaErrorText(){
    if(!player.error) return '';

    const map = {
        1:'用户中止播放',
        2:'网络或在线视频读取失败',
        3:'视频解码失败,可能是编码格式不受 WebView2 支持',
        4:'格式或编码不支持,建议使用浏览器可播放的 WebM/MP4'
    };

    return map[player.error.code] || ('未知错误 code=' + player.error.code);
}

function pickIndex(){
    if(videos.length <= 1) return 0;

    let i;
    do{
        i = Math.floor(Math.random() * videos.length);
    }while(i === lastIndex);

    return i;
}

function setFadeState(state){
    player.classList.remove('fade-in');
    player.classList.remove('fade-out');

    if(state){
        player.classList.add(state);
    }
}

function playRandom(){
    clearTimeout(retryTimer);
    errBox.textContent = '';
    switching = false;
    fadingForNaturalEnd = false;
    currentToken++;

    if(!videos.length){
        title.textContent = '没有视频 URL';
        return;
    }

    const i = pickIndex();
    lastIndex = i;

    const src = videos[i];
    const url = normalizeVideoUrl(src);

    title.textContent = '[' + (i + 1) + '/' + videos.length + '] ' + basename(src);
    count.textContent = '共 ' + videos.length + ' 个视频';

    setFadeState('fade-out');

    player.removeAttribute('src');
    player.src = url;
    player.load();

    const token = currentToken;
    const p = player.play();
    if(p && p.catch){
        p.then(function(){
            if(token != currentToken) return;
            hint.style.display = 'none';
        }).catch(function(e){
            if(token != currentToken) return;
            hint.style.display = 'block';
            errBox.textContent = '自动播放被阻止,请单击画面继续。'
                + (e && e.message ? ' ' + e.message : '');
        });
    }
}

function fadeInCurrent(){
    requestAnimationFrame(function(){
        requestAnimationFrame(function(){
            setFadeState('fade-in');
        });
    });
}

function fadeOutAndNext(){
    if(switching) return;
    switching = true;
    fadingForNaturalEnd = true;

    setFadeState('fade-out');
    clearTimeout(retryTimer);
    retryTimer = setTimeout(function(){
        playRandom();
    },fadeOutMs + 80);
}

player.addEventListener('canplay', function(){
    errBox.textContent = '';
    hint.style.display = 'none';
    fadeInCurrent();
});

player.addEventListener('timeupdate', function(){
    if(switching || fadingForNaturalEnd) return;
    if(!isFinite(player.duration) || player.duration <= 0) return;

    const remain = player.duration - player.currentTime;

    if(remain <= fadeOutSeconds && player.currentTime > 0.2){
        fadeOutAndNext();
    }
});

player.addEventListener('ended', function(){
    fadeOutAndNext();
});

player.addEventListener('error', function(){
    const msg = mediaErrorText();
    errBox.textContent = '播放失败:' + msg + ',1 秒后切换下一个。';
    setFadeState('fade-out');
    retryTimer = setTimeout(playRandom, 1000);
});

hint.addEventListener('click', function(){
    player.play();
});

document.addEventListener('mousedown', function(e){
    if(e.button === 0){
        e.preventDefault();
        aardio.startMove();
    }
});

document.addEventListener('contextmenu', function(e){
    e.preventDefault();
    aardio.closeApp();
});

// 键盘:N 下一条,空格暂停/继续,Esc 关闭
document.addEventListener('keydown', function(e){
    if(e.code === 'Space'){
        e.preventDefault();
        if(player.paused) player.play();
        else player.pause();
    }
    else if(e.key === 'n' || e.key === 'N' || e.key === 'ArrowRight'){
        fadeOutAndNext();
    }
    else if(e.key === 'Escape'){
        aardio.closeApp();
    }
});

playRandom();
</script>
</body>
</html>`);
/*}}*/

var htmlPath = io.appData("random-online-player\transparent-player.html", html);
win.lowerRight(winform.hwnd, 20, 0);
winform.show();
wb.go(htmlPath);

win.loopMessage();


最新回复 (0)
返回