// ==UserScript==
// @name Action-Buttons in RT Search-Results
// @version 0.3
// @downloadURL https://cdn.piraten.tools/libs/piraten-userscripts/0.1/js/action-buttons_in_rt_search_results.user.js
// @author Daniel Jäger (DJaeger)
// @match https://helpdesk.piratenpartei-nrw.de/rt/Search/*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @require https://cdn.piraten.tools/libs/jquery/2.2.4/jquery.min.js
// @require https://cdn.piraten.tools/libs/jquery-ui/1.13.2/jquery-ui.min.js
// ==/UserScript==
function request (url, pattern) {
var dfd = $.Deferred();
GM_xmlhttpRequest({
method: "GET",
url: url,
ignoreCache: true,
onload: function(response) {
if (response.responseText.indexOf(pattern) != -1) {
dfd.resolve();
} else {
if (response.responseText.indexOf("Possible cross-site request forgery") != -1) {
var parsed = $.parseHTML(response.responseText);
parsed = $('
').append(parsed);
href = $( parsed ).find("#body p strong a").attr("href");
if(typeof href !== undefined && href !== undefined && href !== null) {
var link = "https://helpdesk.piratenpartei-nrw.de" + href;
GM_xmlhttpRequest({
method: "GET",
url: link,
ignoreCache: true,
onload: function(response) {
if (response.responseText.indexOf(pattern) != -1) {
dfd.resolve();
} else {
dfd.reject();
}
},
onerror: function(response) {
dfd.reject();
}
});
}
} else {
dfd.reject();
}
}
},
onerror: function(response) {
dfd.reject();
}
});
return dfd.promise();
}
function processRow(row,type) {
var dfd = $.Deferred();
var href = null;
href = $( "td:first a", row ).attr("href");
if(typeof href !== undefined && href !== undefined && href !== null) {
var rowindex = $( row ).index() + 1;
$( "td:last", row ).append('working...
');
if ($( row ).hasClass( "doublerow" )) {
row = $("table.ticket-list tr:nth-child(" + (rowindex) + "), table.ticket-list tr:nth-child(" + (rowindex+1) + ")");
}
var link = "https://helpdesk.piratenpartei-nrw.de" + href;
var requesturl, pattern;
if(type=="spam") {
requesturl = link.replace(/Display.html/,'ReportSpam.html');
pattern = "The ticket has been reported as SPAM. Thank you. Sent email successfully";
} else if(type=="del") {
requesturl = link + "&Status=deleted";
pattern = "Anfrage gelöscht";
} else {
dfd.reject();
}
request(requesturl, pattern)
.done(function() {
$( "td", row )
.animate( { backgroundColor: 'green' }, 1000 )
.queue(function( next ) {
row.fadeOut( 1000 )
.queue(function( next ) {
row.remove();
dfd.resolve();
});
});
})
.fail(function() {
$( "td", row )
.animate( { backgroundColor: 'red' }, 1000 )
.queue(function( next ) {
dfd.reject();
});
})
.always(function() {
$( ".three-quarters-loader", row )
.fadeOut( 1000 )
.queue(function( next ) {
$( ".three-quarters-loader", row ).remove();
});
});
} else {
dfd.resolve();
}
return dfd.promise();
}
(function() {
'use strict';
var title = document.querySelector('title').textContent;
if (title.indexOf("1 Anfrage gefunden") != -1 || title.indexOf("Anfragen gefunden") != -1) {
$("head").append ('');
GM_addStyle (
"table.ticket-list tr .actions {width: 20em;}" +
"table.ticket-list tr .actions .three-quarters-loader {float: right; right: -0.5em; height: auto;}" +
"table.ticket-list tr.doublerow .actions .three-quarters-loader {top: -4px; bottom: 2px;}" +
"table.ticket-list tr:not(.doublerow) .actions {padding-top: 0em;}" +
"table.ticket-list tr:not(.doublerow) .actions button {padding-top: 0em; padding-bottom: 0em;}" +
"table.ticket-list tr:not(.doublerow) .actions .three-quarters-loader {border-width: 4px; border-radius: 8px; width: 16px; height: 16px;margin-top: 0.1em;}"
);
$( "table.ticket-list tr:first" ).append('Actions | ');
var buttonallspam = $('', {
text: 'Alle SPAM',
click: function () {
$( "table.ticket-list tr:nth-child(2) th:last" ).append('working...
');
var rowprocessarray = [];
$( "table.ticket-list tr:gt(1)" ).each(function() {
rowprocessarray.push(processRow(this,"spam"));
});
$.when.apply($, rowprocessarray)
.done(function() {
setTimeout(function(){
$("table.ticket-list tr:nth-child(2) th.actions button").prop("disabled",true);
if (confirm('Reload page?')) {
window.location.reload(true);
}
}, 1000);
})
.fail(function() {
alert("Irgendwas ist schiefgelaufen!\nBitte manuell prüfen!");
})
.always(function() {
$( "table.ticket-list tr:nth-child(2) .three-quarters-loader" )
.fadeOut( 1000 )
.queue(function( next ) {
$( "table.ticket-list tr:nth-child(2) .three-quarters-loader" ).remove();
});
});
}
});
var buttonalldel = $('', {
text: 'Alle löschen',
click: function () {
$( "table.ticket-list tr:nth-child(2) th:last" ).append('working...
');
var rowprocessarray = [];
$( "table.ticket-list tr:gt(1)" ).each(function() {
rowprocessarray.push(processRow(this,"del"));
});
$.when.apply($, requestarray)
.done(function() {
sleep(1000);
$("table.ticket-list tr:nth-child(2) th.actions button").prop("disabled",true);
if (confirm('Reload page?')) {
window.location.reload(true);
}
})
.fail(function() {
alert("Irgendwas ist schiefgelaufen!\nBitte manuell prüfen!");
})
.always(function() {
$( "table.ticket-list tr:nth-child(2) .three-quarters-loader" )
.fadeOut( 1000 )
.queue(function( next ) {
$( "table.ticket-list tr:nth-child(2) .three-quarters-loader" ).remove();
});
});
}
});
$( "table.ticket-list tr:nth-child(2)" ).append(' | ');
$( "table.ticket-list tr:nth-child(2) th:last" )
.append(buttonallspam)
.append(buttonalldel);
$("table.ticket-list tr:gt(1)").each(function() {
var row = this;
var href = null;
href = $( "td:first a", row ).attr("href");
if(typeof href !== undefined && href !== undefined && href !== null) {
var rowclass = jQuery.trim($( row ).attr('class'));
var rowindex = $( row ).index() + 1;
var doublerow = false;
if ($("table.ticket-list tr:nth-child(" + (rowindex+1) + ")").hasClass( rowclass )) {
doublerow = true;
$( row ).addClass( "doublerow" );
}
var buttonspam = $('', {
text: 'SPAM',
click: function () {
processRow(row,"spam");
}
});
var buttondel = $('', {
text: 'Löschen',
click: function () {
processRow(row,"del");
}
});
if (doublerow) {
$( this ).append(' | ');
} else {
$( this ).append(' | ');
}
$( "td:last", this )
.append(buttonspam)
.append(buttondel);
}
});
}
})();