Greasy Fork

Mangadex-RPC

Share your Mangadex reads with Discord RPC.

目前为 2019-03-28 提交的版本。查看 最新版本

// ==UserScript==
// @name     Mangadex-RPC
// @version  0.0.1M
// @author   Jinzulen
// @icon     https://mangadex.org/favicon-192x192.png
// @description Share your Mangadex reads with Discord RPC.
//
// @grant GM_notification
//
// @include https://mangadex.org/chapter/*
// @namespace https://greasyfork.org/users/287142
// ==/UserScript==

(function() {
    'use strict';

    // Fetch chapter ID.
    var chapterID = ((window.location.href).replace("https://mangadex.org/chapter/", "")).split("/")[0];

    // Fetch chapter and manga information from the Mangadex API.
    var Chapter = contactAPI("https://mangadex.org/api/chapter/" + chapterID);
    var Manga = contactAPI("https://mangadex.org/api/manga/" + Chapter.manga_id).manga;

    // Establish DPS connection.
    // Having two readers open on different tabs will confuse the RPC while having it alternate between the two.
    // Will figure out a fix for this in a future revision.
    var Socket = new WebSocket("ws://127.0.0.1:6680");

    var Execution = setInterval(() => {
        Socket.send(JSON.stringify(establishRPC(
            Chapter, Manga,
            ((window.location.href).replace("https://mangadex.org/chapter/", "")).split("/")[1]
        )));
    }, 1000);

    Socket.onerror = function (Error)
    {
        console.error ("# [Mangadex-RPC] " + Error);
    };

    Socket.onclose = function (Error)
    {
        console.error ("# [Mangadex-RPC] " + Error);

        if (Error.code != "1000")
        {
            notifyUser("DPS Error", "It seems the DPS (Discord Pipe Socket) isn't running on port 6680.");
        }
    };

    Socket.onopen = function()
    {
        notifyUser(`Ch. ${Chapter.chapter}`, "Opened DPS connection for " + Manga.title, "https://mangadex.org" + Manga.cover_url);
    };
})();

function establishRPC(Chapter, Manga, Page)
{
    return {
        cid: "560723716435214357",

        rpc:
        {
            state: "Page",
            details: `${Chapter.title ? "" : Manga.title + " -"} ${Chapter.chapter ? "Ch. " + Chapter.chapter : ""} ${Chapter.title ? " - " + Chapter.title : ""}`,

            partySize: Page,
            partyMax: (Chapter.page_array).length,

            largeImageKey: "mangadex-512",
            largeImageText: Chapter.title ? Manga.title : "",

            smallImageKey: "mangadex-512",
            smallImageText: "Mangadex RPC by Jin#8303"
        }
    }
};

function contactAPI(Endpoint)
{
    var xmlHTTP = new XMLHttpRequest();
    xmlHTTP.open("GET", Endpoint, false);
    xmlHTTP.send(null);

    return JSON.parse(xmlHTTP.responseText);
};

function notifyUser(Title, Message, Icon)
{
    var Iconz = Icon ? Icon : "https://mangadex.org/favicon-192x192.png";

    if (GM_info.scriptHandler === "Tampermonkey")
    {
        GM_notification(Message, Title, Iconz);
    }

    if (GM_info.scriptHandler === "Greasemonkey")
    {
        if (!("Notification" in window))
        {
            console.log("# [Mangadex-RPC] This browser does not support desktop notifications.");
        }

        if (Notification.permission !== "denied")
        {
            Notification.requestPermission().then(function (Permission) {
                if (Permission === "granted")
                {
                    new Notification(Title, {body: Message, icon: Iconz});
                }
            });
        }
    }
};