Last active
October 17, 2016 18:18
-
-
Save adrianheine/ac0a4d6b84ff5647581bc0612519fecb to your computer and use it in GitHub Desktop.
GreaseMonkey-Script, das einen Knopf hinzufügt, um alle kommenden Abholungen anzuzeigen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name foodsharing.de pickups | |
// @name:de foodsharing.de-Abholungen | |
// @namespace https://adrianheine.de | |
// @description Add a button to load all upcoming food pickups | |
// @description:de Zeigt einen Knopf um alle kommenden Abholungen anzuzeigen | |
// @include https://foodsharing.de/?page=dashboard | |
// @grant none | |
// @version 1.1 | |
// ==/UserScript== | |
var $target = $('#left > .inside'); | |
function add_field(title, content) { | |
var $field = $( '<div class="field">' ); | |
$('<div class="head ui-widget-header ui-corner-top"></div>').text( title ).appendTo( $field ); | |
var $content = $('<div class="ui-widget ui-widget-content corner-bottom margin-bottom ui-padding"><div class="scroller"></div></div>'); | |
$content.append( content ); | |
$content.appendTo( $field ); | |
$field.appendTo( $target ); | |
return $field; | |
} | |
var $add_button = $('<button class="button">Abholungen zeigen</button>'); | |
var $add_link = add_field( | |
'Nächste Abholungen', | |
$add_button.add( $( '<p style="text-align: right;"><a style="color: lightgray;" href="https://gist.github.com/adrianheine/ac0a4d6b84ff5647581bc0612519fecb">(script)</a></p>' ) ) | |
); | |
$add_button.click( function() { | |
$add_button.text('Lädt …'); | |
doIt().then(function() { | |
$add_link.remove(); | |
}); | |
}); | |
function cmp(a, b) { | |
return a < b ? -1 : (a > b ? 1 : 0); | |
} | |
function doIt() { | |
var betriebe = $('#left > div > div:nth-child(2) > div.ui-widget.ui-widget-content.corner-bottom.margin-bottom.ui-padding > ul > li a').map(function () { return [ [this.href, this.innerText] ]; }); | |
if (betriebe.length === 0) console.log( "Keine Betriebe gefunden" ) | |
var ajaxCalls = betriebe.map( function (_, betrieb) { return $.ajax( betrieb[0] );}); | |
return $.when.apply( $, ajaxCalls ).then(function (/* … */) { | |
var by_day = {}; | |
$.each( arguments, function( i, res ) { | |
var betrieb = betriebe[i][1]; | |
var $termine = $(res[0]).find('#scroller .input-wrapper'); | |
$.each( $termine, function( _, termin ) { | |
var $termin = $(termin); | |
var $terminLabel = $termin.find('.wrapper-label'); | |
$terminLabel.text( $terminLabel.text().replace(/^.+, /, betrieb + ', ') ); | |
$termin.find('li.empty a').attr('onclick', '').attr('href', betriebe[i][0]); | |
var day = termin.id.match(/fetch-(\d{4})(\d{2})(\d{2})\d{6}-wrapper/); | |
day = day[1] + '-' + day[2] + '-' + day[3]; | |
by_day[day] = by_day[day] || []; | |
by_day[day].push( { | |
dom: termin, | |
labelMatch: $terminLabel.text().match(/^(.*), (\d{1,2})\.(\d{2}) Uhr$/) || [] | |
} ); | |
} ); | |
} ); | |
var days = Object.keys(by_day) | |
if (days.length === 0) console.log( "Keine Abholtage gefunden" ) | |
days.forEach( function (day) { | |
add_field(day, by_day[day].sort( function( termin1, termin2 ) { | |
return cmp(Number(termin1.labelMatch[2]), Number(termin2.labelMatch[2])) || cmp(termin1.labelMatch[3], termin2.labelMatch[3]) || | |
cmp(termin1.labelMatch[1], termin2.labelMatch[1]); | |
} ).map( function( termin ) { return termin.dom; } )); | |
}); | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment