風雲 (ID: 3)
头衔:论坛版主
等级:大天使
积分:1632
发帖:72 篇
来自:保密
注册:2022/3/30 15:28:53
造访:2024/12/21 22:25:57
发帖:72 篇
来自:保密
注册:2022/3/30 15:28:53
造访:2024/12/21 22:25:57
[ 第 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:已设置保密