金十快讯语音播报

抓取金十页面市场快讯,并进行语音播报(官方语音播报是收费功能)

当前为 2023-09-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         金十快讯语音播报
// @namespace   https://www.jin10.com/
// @version      1.1
// @description  抓取金十页面市场快讯,并进行语音播报(官方语音播报是收费功能)
// @author       robaggio
// @match        https://www.jin10.com/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=jin10.com
// @run-at       document-end
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    // setup speech settings
    const synth = window.speechSynthesis;
    const voices = synth.getVoices();
    var voice = null;
    for (let i = 0; i < voices.length; i++) {
        if (voices[i].name === 'Ting-Ting') {
            voice = voices[i];
        }
    }
    var lastMessageId = null;
    let observer = new MutationObserver(mutations => {
        //console.log(mutations)
        mutations.forEach(function(mutation) {
            mutation.addedNodes.forEach(function(node) {
                // 只获取addnodes里的.jin-flash-item-container变化
                if (node.className?.indexOf('jin-flash-item-container')>=0){
                    // id可以用来判定消息的先后
                    let id = node.id;
                    if (lastMessageId!=null && id<lastMessageId)return;
                    lastMessageId = id;
                    // 标题, 不一定有
                    let title = node.querySelector('.right-common-title')?.innerText;
                    // 正文,肯定有
                    let content = node.querySelector('.collapse-content')?.innerText;
                    if (content == null)return;
                    console.log('content',title,content);
                    var utterThis = new SpeechSynthesisUtterance(title?(title + '。' + content):content);
                    utterThis.lang = 'zh-CN';
                    utterThis.voice = voice;
                    synth.speak(utterThis);
                }
            });
        });
    });
    let box = document.querySelector('#jin_flash_list');
    observer.observe(box,{childList:true,subtree:true});
})();