Dev_Multi_Open2

Easier Opening in Deviantart Notification Center

目前為 2019-10-28 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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.0
// @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 = "div._1FZFD>div,div.CMvV0>div,div.VG6bf>div,div._3igXW>div,div._1F-dq>div"; //,div._3KMiG
    var topBar = "div._24VFR._3zk0a._3Rgyb";
    var checkedBox = "div._2qxrJ,div._15Po9";
    var selcont = "div._3Pgai";
    var viewAllBut = "button._9rMMY._1qwC7._3xVcb._1KcL_._21wpm";
    var allButClass = "_9rMMY _1qwC7 _3xVcb _1KcL_ _21wpm";
    var stackCount="div.Ey06z";

    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) {
        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) {
            if (target.find(checkedBox).length == 1) target.find("label").click(); //unselecting DOM
            if (ev == 0) target.removeClass("ui-selected").addClass("ui-unselecting");
        } else {
            if (target.find(checkedBox).length == 0) 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);
        $("button.eNvqE").addClass("_1BMgL"); //add space between buttons
        btnOpen.click(function () {
            $(selectees).has(checkedBox).each(function () {
                if ($(this).attr("url") != undefined) window.open($(this).attr("url"));
            });
        });
    }

    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) {
        var userid = $(event.target).closest(selectees).find("a.user-link").attr("data-userid");
        //keyword in div.Ey06z
        var type=$(this).closest(selectees).find(stackCount).text();
        if(type.indexOf("Deviations")!=-1){
            getStackURLs(0,userid,"deviations");
            getStackURLs(0,userid,"groupdeviations");
        }else if(type.indexOf("Journals")!=-1){
            getStackURLs(0,userid,"journals");
        }else if(type.indexOf("Polls")!=-1){
            getStackURLs(0,userid,"polls");
        }
    }

    function insertOpenAllButton() {
        $(viewAllBut).not("[dmo2_openAll]").attr("dmo2_openAll", true).after($("<button class='dmo_openAll " + allButClass + "'>Open All</button>").click(requestAllOpen));
    }

    function makeSelectable() {
        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
        });
        $(selectees).find("label input").change(function (event) { //change selection by hand
            if (!selfTrg) {
                event.stopPropagation();
                var target = $(this).closest(selectees);
                var el = {};
                if (target.find(checkedBox).length == 0) 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 = $(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*/