Scratch Message Notifier

Notifies every message, checks every 2 seconds

目前為 2022-05-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Scratch Message Notifier
// @author       Hans5958
// @namespace    Hans5958
// @description  Notifies every message, checks every 2 seconds
// @include      http*://scratch.mit.edu/*
// @version      1.3.1.2
// @grant        none
// @icon         https://raw.githubusercontent.com/Hans5958/userscripts/master/Scratch/Message%20Notifier/icon.png
// @run-at       document-idle
// @license      MIT
// @homepageURL  https://github.com/Hans5958/userscripts
// @supportURL   https://github.com/Hans5958/userscripts/issues
// ==/UserScript==

function MSGLog(log) {
    console.log("[MSG] " + log);
}
MSGLog("Preparing...");
var originalTitle = document.title;
var originalCount = 0;
var count = 0;
var player = document.createElement('audio');
player.src = 'https://raw.githubusercontent.com/Hans5958/message-notifier/master/Message%20Notifier/notificationsound.wav';

function getCount() {
    try {
        return $('.notificationsCount').html();
    } catch (e) {
        return document.getElementsByClassName("message-count")[0].innerHTML;
    }
}

var ico = $('<link id="favicon" rel="icon" type="image/x-icon" href="/favicon.ico" />');
ico.appendTo(document.head);

function createIcon() {
    var canvas = document.createElement('canvas'),
        ctx,
        img = document.createElement('img');
    canvas.height = canvas.width = 32;
    ctx = canvas.getContext('2d');
    img.onload = function () {
        ctx.drawImage(this, 0, 0);
        ctx.font = 'bold 21px "helvetica", sans-serif';
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 3;
        if (count > 99) {
            ctx.strokeText('99+', 0, 31);
            ctx.fillStyle = '#FFFFFF';
            ctx.fillText('99+', 0, 31);
        } else {
            ctx.strokeText(count + '', 0, 31);
            ctx.fillStyle = '#FFFFFF';
            ctx.fillText(count + '', 0, 31);
        }
        ico.attr({
            'type': 'image/png',
            'href': canvas.toDataURL('image/png')
        });
    };
    img.src = '/favicon.ico';
}

if (getCount() !== undefined) {
    MSGLog("Starting...");
    setInterval(function () {
            count = getCount();
            if (count == 0) {
                document.title = originalTitle;
                ico.attr({
                    'type': 'image/x-icon',
                    'href': '/favicon.ico'
                });
                originalCount = 0;
            } else {
                if (originalCount != count) {
                    document.title = '(' + count + ') New message!';
                    player.play();
                    originalCount = count;
                    createIcon();
                } else {
                    document.title = '(' + count + ') ' + originalTitle;
                }
            }
        },
        2000);
} else {
    MSGLog("User not logged in.");
}