Dev_Multi_Open2

Easier Opening in Deviantart Notification Center

目前為 2021-06-07 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Dev_Multi_Open2
// @namespace    http://phi.pf-control.de/apps/userscripts
// @version      1.3
// @description  Easier Opening in Deviantart Notification Center
// @author       Dediggefedde
// @match        https://www.deviantart.com/notifications/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @require      http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js
// @grant        GM.xmlHttpRequest
// ==/UserScript==
/* globals $*/

(function() {
    'use strict';

    var selfTrg = false;
    var selContainer;
    var selectees = "" + //things that drag can select
        "section._3fxzN._3q1dq ," + //watch thumbs
        "div._1AEwc"; //notification entries
    var topBar = "div._1MpFZ._3DiMC._65VTy._2Jm2O > div._1MpFZ._3DiMC";
    var selectedEl = "._1S1FT"; //selected entries
    var viewAllBut = "a._25AwR._2gSAL._2aAuV._31fTQ._1dZ7P"; //._3xVcb._1KcL_._21wpm"; //identify view-all-button
    //var allButClass = "_9rMMY _1qwC7 _3xVcb _1iaVy UR1Kt"; //new class for open-all-button //now copied from viewallbut
    var stackCount = "div.paUD_._1dZ7P"; //display of "# deviations" in a stack

    function prepareSite() {
        var scr = $('<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">');
        scr.appendTo(document.head);
        var sty = $("<style type='text/css'></style>");
        sty.append(".ui-selectable-helper{background-image:linear-gradient(rgb(0,255,0),rgb(0,100,0));opacity:0.3;}");
        sty.append(".dmo_openAll{top: 68%;}");
        sty.appendTo(document.head);
        scr.ready(function() {
            setInterval(dynamicInsertion, 1000);
        });
    }

    function selecting(ev, ui) {
        //console.log(ui,ev,ev.originalEvent.result);
        /* if(ev.originalEvent.result){
             return;
         }*/
        var target = $(ui.selecting);
        var selecting = true;
        if (target.length == 0) {
            target = $(ui.unselecting);
            selecting = false;
        }
        var url = target.attr("url");
        selfTrg = true;

        if (!selecting) {
            target.find("label").click(); //unselecting DOM
            if (ev == 0) target.removeClass("ui-selected").addClass("ui-unselecting");
        } else {
            target.find("label").click(); //selecting DOM
            if (ev == 0) target.addClass("ui-selecting");
        }
        selfTrg = false;
    }

    function insertOpenButton() {
        if ($("button.dmo2_openTab").length > 0) return;
        // var btnOpen = $("<button class='eNvqE dmo2_openTab'>Open in new Tab</button>");
        // $(topBar).append(btnOpen);
        var btnOpen = $(topBar).find("button").last().clone().html("Open in new Tab").css("margin", "0px 15px").appendTo(topBar);

        $("button.eNvqE").addClass("_1BMgL"); //add space between buttons
        btnOpen.click(function() {
            $(selectees).filter(selectedEl).each(function() {
                console.log($(this).attr("url"));
                if ($(this).attr("url") != undefined) window.open($(this).attr("url"));
                else $(this).find("button.dmo_openAll").click();
            });
        });
    }

    function getStackURLs(offset, userid, type) {
        return new Promise(function(resolve, reject) {
            GM.xmlHttpRequest({
                method: "GET",
                url: "https://www.deviantart.com/_napi/da-messagecentre/api/stack?stackId=uq:devwatch:tg%3D" + type + ",sender%3D" + userid + "&type=deviations&offset=" + offset + "&limit=24",
                onerror: function(response) {
                    reject(response);
                },
                onload: function(response) {
                    var resp = JSON.parse(response.responseText);
                    //response.results[0].deviation.url;
                    for (var el of resp.results) {
                        window.open(el.deviation.url);
                    }
                    if (resp.hasMore) resolve(getStackURLs(24, userid));
                    else resolve(1);
                }
            });
        });
    }

    function requestAllOpen(event) {
        event.preventDefault();
        event.stopPropagation();

        var sender = $(event.target);
        var userid = sender.closest(selectees).find("a.user-link").attr("data-userid");
        //keyword in div.Ey06z
        console.log("rO:", sender, selectees, sender.closest(selectees), sender.attr("type"));

        switch (sender.attr("type")) {
            case "1":
                getStackURLs(0, userid, "deviations");
                getStackURLs(0, userid, "groupdeviations");
                break;
            case "2":
                getStackURLs(0, userid, "journals");
                break;
            case "3":
                getStackURLs(0, userid, "polls");
                break;
        }
    }

    function insertOpenAllButton() {
        var el = $("<button>Open All</button>").attr("dmo2_openAll", true).click(requestAllOpen);
        el.attr("class", $(viewAllBut).attr("class")).addClass("dmo_openAll");
        $(viewAllBut).parent().not("[dmo2_openAll]").attr("dmo2_openAll", true).append(el);

        $("button.dmo_openAll:not([type])").each(function() {
            var type = $(this).closest(selectees).find(stackCount).text();
            if (type.indexOf("Deviations") != -1) {
                $(this).attr("type", 1);
            } else if (type.indexOf("Journals") != -1) {
                $(this).attr("type", 2);
            } else if (type.indexOf("Polls") != -1) {
                $(this).attr("type", 3);
            } else {
                $(this).attr("type", 0);
            }
        })
    }

    function makeSelectable() {
        var selected = $([]),
            offset = { top: 0, left: 0 };
        selContainer.selectable({ //jquery ui selectable
            filter: selectees,
            distance: 10, //allows clicking. deprecated, hopefully stays a while
            selecting: selecting, //during selection; select/unselect only regarding mark-area
            unselecting: selecting,
            cancel: "[contenteditable]"
        });

        $(selectees).find("label input").change(function(event) { //change selection by hand
            if (!selfTrg) {
                event.stopPropagation();
                var target = $(this).closest(selectees);
                var el = {};
                if (!target.hasClass(selectedEl)) el.unselecting = target;
                else el.selecting = target;
                selecting(0, el);
                selContainer.selectable('refresh');
                selContainer.data("ui-selectable")._mouseStop(null);
            }
        });
        insertOpenButton();
    }

    function dynamicInsertion() {
        $(selectees).not("[url]").each(function() { //href disappears for selected items. copy it beforehand
            $(this).attr("url", $(this).find("a[data-hook='deviation_link']").attr("href"));
        });
        insertOpenAllButton();
        if ($(selectees).not("[dmo2]").length == 0) return; //only do if you have selectees without attribute
        $(selectees).attr("dmo2", true);
        selContainer = $(selectees).parents("section").parent(); // $(selcont);
        makeSelectable();
    }
    prepareSite();
})();

/* deviantart API
https://www.deviantart.com/_napi/da-messagecentre/api/stack?stackId=uq:devwatch:tg%3Ddeviations,sender%3D4165994&type=deviations&offset=0&limit=24
stackId	uq:devwatch:tg=deviations,sender=4165994
type	deviations
offset	0
limit	24

response
response.results[0].deviation.url
response.counts.total
response.hasMore*/