// ==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
// @downloadURL none
// ==/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 = $('');
scr.appendTo(document.head);
var sty = $("");
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 = $("");
// $(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 = $("").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*/