Bubble Message

【使用前先看介绍/有问题可反馈】气泡信息 (Bubble Message):能够生成悬浮气泡通知,支持自定义 icon 颜色、文字信息、淡入淡出时间、显示时间,气泡宽度。

当前为 2021-03-08 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Bubble Message
// @namespace    http://tampermonkey.net/
// @version      0.1.4
// @description  【使用前先看介绍/有问题可反馈】气泡信息 (Bubble Message):能够生成悬浮气泡通知,支持自定义 icon 颜色、文字信息、淡入淡出时间、显示时间,气泡宽度。
// @author       cc
// @include      *
// @noframes
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    function getStyle (config) {
        let style = document.createElement('style');
        style.id = 'bubble-message-style';
        style.innerHTML = `
            .bm-container {
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
            }
            .bm-row-container {
                display: flex;
                flex-direction: row;
                align-items: center;
            }
            .bm-text-container {
                display: flex;
                flex-direction: row;
                word-break:break-all;
            }
            .bm-info {
                z-index: 2001;
                padding: 10px;
                background-color: white;
                color: black;
                border: 1px solid #eeeeee;
                font-size: 14px;
                border-radius: 5px;
                position: fixed;
                top: 20px;
                left: 0;
                right: 0;
                margin: auto;
                width: ${config.width}px;
            }
            .bm-info-enter-active {
                animation: fade-in ${config.fadeInTime}s forwards;
            }
            .bm-info-leave-active {
                animation: fade-out ${config.fadeOutTime}s forwards;
            }
            .bm-icon {
                width: 14px;
                height: 14px;
                border-radius: 50%;
                color: white;
                font-size: 11px;
                font-weight: bold;
            }
            @keyframes fade-in {
                from {
                    opacity: 0;
                    scale: 0;
                    transform: translateY(-20px);
                }
                to {
                    opacity: 1;
                    scale: 1;
                    transform: translateY(0px);
                }
            }
            @keyframes fade-out {
                from {
                    opacity: 1;
                    transform: translateY(0px);
                }
                to {
                    opacity: 1;
                    transform: translateY(-20px);
                }
            }
        `;
        return style;
    }
    function BubbleMessage () {
        this.config = {
            cmap: {
                info: '#909399',
                warning: '#e6a23c',
                error: '#f56c6c',
                success: '#67c23a'
            },
            fadeInTime: 0.4,
            fadeOutTime: 0.6,
            width: 240,
        };
        this.message = function (options) {
            options = options || {}
            if (Object.keys(this.config.cmap).indexOf(options.type) < 0) options.type = 'info';
            if (typeof options.message !== 'string') options.message = String(options.message)
            if (typeof options.duration !== 'number' || options.duration < 1500) options.duration = 1500;
            let info = document.createElement('div');
            info.className = 'bm-info bm-row-container bm-info-enter-active';
            info.innerHTML = `
                <div class="bm-container bm-icon" style="background-color: ${this.config.cmap[options.type]};"> <div> ! </div> </div>
                <div class="bm-text-container" style="margin-left: 10px; width: ${this.config.width - 25}px;"> ${options.message} </div>
            `
            let style = getStyle(this.config);
            document.body.appendChild(style);
            document.body.appendChild(info)
            setTimeout(() => {
                info.className = 'bm-info bm-row-container bm-info-leave-active';
                setTimeout(() => {
                    document.body.removeChild(info);
                    document.body.removeChild(style);
                }, 600);
            }, options.duration);
        };
    };
    window.BubbleMessage = BubbleMessage;
    console.info('Bubble Message version: 0.1.4');
})();