風雲論壇脚本中心 → 浏览:帖子主题
* 帖子主题:Windows 迷你主机当电视盒用,观看中央电视台
風雲 (ID: 3)
头衔:论坛版主
等级:天使
积分:1373
发帖:60
来自:保密
注册:2022/3/30 15:28:53
造访:2024/5/3 6:15:31
[ 第 1 楼 ] 回复
安装 Node.js:
winget install openjs.nodejs

安装 electron 模块:
npm i electron --registry=https://registry.npmmirror.com

放入 index.js,并运行命令:
npx electron .

index.js 内容如下:
var { app, BrowserWindow, Menu } = require('electron');

// 频道列表
var progs = [
    "https://tv.cctv.com/live/cctv1/",
    "https://tv.cctv.com/live/cctv2/",
    "https://tv.cctv.com/live/cctv3/",
    "https://tv.cctv.com/live/cctv4/",
    "https://tv.cctv.com/live/cctv5/",
    "https://tv.cctv.com/live/cctv6/",
    "https://tv.cctv.com/live/cctv7/",
    "https://tv.cctv.com/live/cctv8/",
    "https://tv.cctv.com/live/cctvjilu/",
    "https://tv.cctv.com/live/cctv10/",
    "https://tv.cctv.com/live/cctv11/",
    "https://tv.cctv.com/live/cctv12/",
    "https://tv.cctv.com/live/cctv13/",
    "https://tv.cctv.com/live/cctvchild/",
    "https://tv.cctv.com/live/cctv15/",
    "https://tv.cctv.com/live/cctv16/",
    "https://tv.cctv.com/live/cctv17/"
];

// 播放指定频道
function play(ch) {
    // “00”指令为退出观看
    if(ch == "00") return app.quit();
    var url = progs[ch - 1];
    if(!url) return;
    ctrl.chanel = ch;
    ctrl.win.loadURL(url);
    // 如果存在 video 标签,则请求全屏
    ctrl.win.webContents.on('did-finish-load', () => {
        ctrl.win.webContents.removeAllListeners('before-input-event');
        ctrl.win.webContents.on("before-input-event", (e, input) => {
            if (input.type == "keyDown") ctrl.onkey(input.key);
        });
        setTimeout(() => {
            // 模拟触发一次按键
            ctrl.win.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'ArrowLeft' });
            ctrl.win.webContents.sendInputEvent({ type: 'keyUp', keyCode: 'ArrowLeft' });
            ctrl.win.webContents.executeJavaScript(`self.tvCtrl = new ${tvFunc}(${ch});`);
        }, 100);
    });
}

// 电视注入函数【显示频道】
function tvFunc(curCH) {
    this.showCH = function(ch) { b.style.display = "block"; b.innerHTML = ch; };
    var tv = document.querySelector("video").parentNode;
    var b = document.createElement("b");
    b.style.cssText = "position: fixed; color: #0f0; font: bold 10vh/15vh arial; top: 5vh; right: 5vw; z-index: 9";
    tv.appendChild(b).innerHTML = curCH;
    setTimeout(() => { b.style.display = "none"; }, 3210);
    // 请求全屏播放
    tv.requestFullscreen();
}

// 遥控控制
var ctrl = new function() {
    // 输入频道号
    this.onkey = function(key) {
        if(key == "ArrowUp") return this.changeCH(1);
        if(key == "ArrowDown") return this.changeCH(-1);
        if(isNaN(key)) return;
        nums.push(key);
        if(nums.length > 2) nums.shift();
        if(this.handler) clearTimeout(this.handler);
        // 1秒后确认跳转频道
        this.handler = setTimeout(() => {
            play(nums.join(""));
            this.handler = nums.length = 0;
        }, 1000);
        this.win.webContents.executeJavaScript(`tvCtrl.showCH("${nums.join('')}");`);
    };

    // 上下调台
    this.changeCH = function(ost) {
        var ch = this.tempCH || ctrl.chanel - 0;
        ch += ost;
        if(ch < 1) ch = progs.length;
        if(ch > progs.length) ch = 1;
        // 缓存临时频道
        this.tempCH = ch;
        if(this.handler) clearTimeout(this.handler);
        this.handler = setTimeout(() => {
            play(ch);
            this.handler = 0;
            this.tempCH = 0;
        }, 1000);
        this.win.webContents.executeJavaScript(`tvCtrl.showCH("${ch}");`);
    };

    var nums = new Array;
};

app.on("ready", () => {
    // 最大化无菜单,保留关闭按钮打开窗口
    ctrl.win = new BrowserWindow;
    // 清空菜单
    var menu = Menu.buildFromTemplate([ ]);
    ctrl.win.setMenu(menu);

    // 最大化窗口,播放 CCTV-9
    ctrl.win.maximize(); play(9);
});
2024/3/3 13:15:31 IP:已设置保密
風雲 (ID: 3)
头衔:论坛版主
等级:天使
积分:1373
发帖:60
来自:保密
注册:2022/3/30 15:28:53
造访:2024/5/3 6:15:31
[ 第 2 楼 ] 回复
使用方法:输入频道号换台,例如 1 秒内输入 "1", "4",自动跳到 少儿频道(CCTV-14)并全屏播放。

支持 CCTV-1~17 共 17 套节目。也可按上下键切换频道,支持一次跳多个频道。

输入 “00” 退出观看。

使用此命令启动:
node_modules\electron\dist\electron.exe index.js
不会出现黑色控制台窗口。
2024/3/3 13:21:51 IP:已设置保密
pojin (ID: 2)
等级:风云使者
积分:251
发帖:1
来自:保密
注册:2022/3/30 11:42:27
造访:2024/5/1 8:09:33
[ 第 3 楼 ] 回复
飘过。。。抄走了一篇原创文章~~
2024/3/4 11:56:06 IP:已设置保密
分页: 1, 共 1 页
快速回复主题
账号/密码
用户: 没有注册? 密码:
评论内容