Greasy Fork is available in English.

MyDealz Enhancer

Verbessert Funktionen von MyDealz, oder stellt sie wieder her

As of 12/07/2015. See the latest version.

// ==UserScript==
// @name        MyDealz Enhancer
// @namespace   mydealz_enhancer
// @description Verbessert Funktionen von MyDealz, oder stellt sie wieder her
// @author	BAERnado
// @include     http://www.mydealz.de/*
// @version     0.81
// @require	http://code.jquery.com/jquery-2.1.4.min.js
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_log
// ==/UserScript==

function ModifyPage() {

	var defaultSettings = {comments: {maxQuoteLevel: 1, editReason: ''}, userMenuDirect : {pn: false, account: false}, hideAds: {topBar: true, gsWidget: true, inside: true,nlSection: true, app: true}, moveButtons: {_: true, deals: true, gutscheine: true, freebies: true, custom: true, gesuche: false, diverses: false, kleinanzeigen: false}, fromTo: {deals: 'deals-new', gutscheine: 'gutscheine-new', freebies: 'freebies-new', gesuche: 'gesuche', diverses: 'diverses', kleinanzeigen: 'kleinanzeigen', bugreports: 'bugreports'}};
	var settings = {};
	var timer = {};
	var userName;

	$.fn.hasAttr = function(name) {  
		return this.attr(name) !== undefined;
	};

	$.fn.extend({
		insertAtCaret: function(myValue) {
			var elem = this[0];
			if (document.selection) {
				elem.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
				elem.focus();
			} else if (elem.selectionStart || elem.selectionStart == '0') {
				var startPos = elem.selectionStart;
				var endPos = elem.selectionEnd;
				var scrollTop = elem.scrollTop;
				elem.value = elem.value.substring(0, startPos)+myValue+elem.value.substring(endPos,elem.value.length);
				elem.focus();
				elem.selectionStart = startPos + myValue.length;
				elem.selectionEnd = startPos + myValue.length;
				elem.scrollTop = scrollTop;
			} else {
				elem.value += myValue;
				elem.focus();
			}
		}
	});

	function printUTCDate(_date) {
		return ('0'+_date.getUTCHours().toString()).replace(/^0*([0-9]{2})$/, '$1') + ':' + ('0'+_date.getUTCMinutes().toString()).replace(/^0*([0-9]{2})$/, '$1') + ':' + ('0'+_date.getUTCSeconds().toString()).replace(/^0*([0-9]{2})$/, '$1');
	}

	function printDate(_date) {
		return ('0'+_date.getHours().toString()).replace(/^0*([0-9]{2})$/, '$1') + ':' + ('0'+_date.getMinutes().toString()).replace(/^0*([0-9]{2})$/, '$1') + ':' + ('0'+_date.getSeconds().toString()).replace(/^0*([0-9]{2})$/, '$1');
	}

	function timeTick() {
		var _allTimes = document.getElementsByClassName('bf-carousel-date');
		var _remain;
		var _until;
		var _diffDate;
		var _toDate;
		var _now = new Date();
		for(var _i = 0; _i < _allTimes.length; _i++) {
			_until = _allTimes[_i];
			_remain = _allTimes[_i].parentNode.getElementsByClassName('bf-carousel-date-remain')[0];
			_toDate = new Date();
			_toDate.setTime(parseInt(_until.innerHTML, 10)*1000);
			_diffDate = new Date();
			_diffDate.setTime(_toDate.getTime() - _now.getTime());
			_remain.innerHTML = printUTCDate(_diffDate); // + ' (' + printDate(_toDate) + ')';
		}
	}

	function addTimes() {
		var _allTimes = document.getElementsByClassName('bf-carousel-date');
		var _newSpan;
		for(var _i = 0; _i < _allTimes.length; _i++) {
			_allTimes[_i].style.display = 'none';
			_newSpan = document.createElement('span');
			_newSpan.setAttribute('class', 'bf-carousel-date-remain');
			_allTimes[_i].parentNode.insertBefore(_newSpan, null);
		}
		timeTick();
		window.setInterval(timeTick, 1000);
	}

	function fixPositions() {
		var _allElems = document.getElementsByTagName('article');
		for(var _i = 0;_i < _allElems.length; _i++) {
			_allElems[_i].setAttribute('style', 'position: absolute; width: 1200px; left: 0px; top: ' + _i*404 + 'px');
		}
		window.setTimeout("document.getElementsByTagName('article')[0].parentNode.setAttribute('style', 'height: " + _allElems.length * 404 + "px;');", 20);
	}

	function changeLinks() {
		var _allLinks = $('a.navTrigger1, .navTrigger1-row-items .menu:last .menu-list .navMenu1-item');
		var _fromTo = settings.fromTo;

		var _loc;
		$(_allLinks).each(function (_lInd, _lVal) {
			var _link = $(_lVal);
			if(_link.hasAttr('href')) {
				_loc = _link.attr('href').replace(/^https?:\/\/www\.mydealz\.de(\/(hot|discussed|new)?)?$/, '/').replace(/^https?:\/\/www\.mydealz\.de\/([a-zA-Z-]+)?$/, '$1').replace(/-?(new|discussed)?$/,'');
				if(_loc in _fromTo) {
					_link.attr('href', _link.attr('href').replace(/^(https?:\/\/www\.mydealz\.de).*$/, '$1' + '/' + _fromTo[_loc]));
				}
			}
		});
	}

	function remodelNavBar() {
		// Besten Dank an lolnickname für die Vorlage
		var _subMenu = $('header strong[data-handler="menu"]');
		_subMenu.find('span[class!="navTrigger-arrow"]').remove();
		_subMenu = _subMenu.parent().parent();
		var _buttonIndex = 1;
		var _buttonBeforeSubIndex = 0;
		var _button;
		var _subHiddenButton;
		var _selected;
		var _moreHighlight = true;
		for(_bInd in settings.moveButtons) {
			if(_bInd == '_') {
				_button = $('.navTrigger1-row-items > li > a.navTrigger1[href="http://www.mydealz.de/"], .navTrigger1-row-items li.menu ul.menu-list li.hide--downThrough- a[href="http://www.mydealz.de/"]');
			} else {
				_button = $('.navTrigger1-row-items > li > a.navTrigger1[href*="' + _bInd + '"], .navTrigger1-row-items li.menu ul.menu-list li.hide--downThrough- a[href*="' + _bInd + '"]');
			}
			
			_selected = _button.hasClass('navTrigger1--selected') || _button.hasClass('navMenu1-item--selected');
			
			if(_button.hasClass('navTrigger1') && settings.moveButtons[_bInd]) {
				_button.attr('class', 'navTrigger1 navTrigger1hide--upTo-menu' + _buttonIndex++);
			} else if (_button.hasClass('navTrigger1') && !settings.moveButtons[_bInd]) {
				// Verschiebe in Untermenu
				_subMenu.find('li.hide--downThrough- a.navMenu1-item').eq(_buttonBeforeSubIndex++).parent().before(_button.attr('class', 'navMenu1-item').parent().attr('class', 'hide--downThrough-'));
			} else if(_button.hasClass('navMenu1-item') && settings.moveButtons[_bInd]) {
				// Verschiebe ins Hauptmenu
				_subMenu.before(_button.removeClass('navMenu1-item navMenu1-item--selected').addClass('navTrigger1').parent().removeClass('navMenu1-item hide--downThrough-').addClass('tGrid-cell hide--upTo-menu' + _buttonIndex));
			}

			if(_selected) {
				if(_button.hasClass('navTrigger1')) {
					_button.addClass('navTrigger1--selected');
					_moreHighlight = false;
				} else {
					_button.addClass('navMenu1-item--selected');
					if($('header.header .navTrigger1-row .navTrigger1-row-items a.navTrigger1[href="' + _button.attr('href') + '"]').length == 0) {
						_moreHighlight = true;
					} else {
						_moreHighlight = false;
					}
				}
			}
		}

		for(_buttonIndex = 1; _buttonIndex <= 4; _buttonIndex++) {
			_button = $('.navTrigger1-row-items > li.hide--upTo-menu' + _buttonIndex + ' > a.navTrigger1');
			if(_button.length) {
				_subHiddenButton = $('.navTrigger1-row-items li.menu ul.menu-list li.hide--downThrough-menu' + _buttonIndex + ' a');
				_subHiddenButton.attr('href', _button.attr('href')).html(_button.html());
			} else {
				$('.navTrigger1-row-items li.menu ul.menu-list li.hide--downThrough-menu' + _buttonIndex).remove();
			}
		}

		$('header strong[data-handler="menu"]').removeClass('navTrigger1--selected');
		if(_moreHighlight) {
			$('header strong[data-handler="menu"]').addClass('navTrigger1--selected');
		}
		if(settings.userMenuDirect.pn) {
			var _menuElem = $('ul.userBar label.userBar-button--pm').removeAttr('data-menu').removeAttr('data-handler');
			_menuElem.find('input').removeAttr('data-handler').click(function() { window.location.href = document.location.protocol + '//' + document.location.hostname + '/profile/' + userName + '/messages'; });
		}
		if(settings.userMenuDirect.account) {
			var _menuElem = $('ul.userBar label.userBar-button--user').removeAttr('data-menu').removeAttr('data-handler');
			_menuElem.find('input').removeAttr('data-handler').click(function() { window.location.href = document.location.protocol + '//' + document.location.hostname + '/profile/' + userName; });
		}
	}

	function addCyberDealz() {
		var _newMenu = $('<li>').addClass('hide--downThrough-').append($('<a>').addClass('navMenu1-item').attr('href','http://www.mydealz.de/cyber-monday/cyberdeals').html('Cyber-Deals')).appendTo($('.navMenu--width-l').eq(0));
	}

	function removeAds() {
		var _ads = {topBar: '.topBar', gsWidget: '#gs-widget', inside: "#side section[data-track=\"{\\\"category\\\":\\\"Banner Sidebar\\\"}\"]", nlSection: '.inline-newsletter, .newsletter', app: '[alt="app banner for ios android and window phone"]'};
		for(_aInd in _ads) {
			if(_aInd in settings.hideAds && settings.hideAds[_aInd]) {
				if($(_ads[_aInd]) != null) {
					$(_ads[_aInd]).remove();
				}
			}
		}
	}

	function addFuncLinks() {
		var _thread = $('div.thread, li.thread');
		if(_thread.length) {
			var _list = _thread.find('footer ul');
			var _author = $.trim($(".thread-author").eq(0).text());
			var _topic  = $.trim($(".thread-title h1").eq(0).text());
			var _pnLink = $('<li>').addClass('hList-item').append($('<a>').click(startWaitForPN).addClass('link').addClass('ico').addClass('ico--type-pn-blue').html('PN').attr('data-modal', '{"endpoint":"http:\\/\\/www.mydealz.de\\/profile\\/' + userName + '\\/messages\\/modal-window?to=' + encodeURIComponent(_author) + '"}').attr('data-handler', 'modal').attr('href', 'http://hukd.mydealz.de/profile/' + userName + '/messages/compose-mail?to=' + encodeURIComponent(_author)));
			addGlobalStyle('.ico--type-pn-blue::before { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAB/SURBVChTY/gPBm8+/Vh95N6kTVeACMgAciHiIOkbjz9oZ61FQ0BBkPTnb7+AnP2XniHLQbhAKYYTN14iCyEzgFIMQJsgHIgEsjFAKRRpNLTt9CMGZOVoCOg6hvsvPqGJwhHUY0C/okkAEVAQKg0Bz95+vXDvLdC1QAZU6P9/AKPpvjdJZe34AAAAAElFTkSuQmCC); background-size: 10px 10px; background-position: 3px 3px;}');
			_list.append(_pnLink);

			var _reportLink = _thread.find('.ico--type-megaphone-blue').parent();
			_newLink = $('<div>').addClass('hList-item').html('<button class="link ico ico--type-spam-blue ico--pos-l" data-handler="replace" type="button">Spam</button>').find('button').attr('data-replace', '["/vote?v=spam&t=' + _thread.attr('id') + '", "div"]').parent();
			_reportLink.before(_newLink);
			_newLink = $('<div>').addClass('hList-item').html('<button class="link ico ico--type-time-blue ico--pos-l" data-handler="replace" type="button">Abgelaufen</button>').find('button').attr('data-replace', '["/vote?v=expired&t=' + _thread.attr('id') + '", "div"]').parent();
			_reportLink.before(_newLink);
			_reportLink.remove();
		}
	}

	function startWaitForPN () {
		var _thread_id = $('div.thread, li.thread').has($(this)).attr('id');
		timer.pn = window.setInterval(function () { waitForPN(_thread_id) }, 50);
	}

	function waitForPN(_thread_id) {
		if(!$('#' + _thread_id).find('.ico--type-pn-blue').hasClass('seal--spin')) {
			if('pn' in timer) {
				window.clearInterval(timer.pn);
				delete timer.pn;
			}
			$('#pm-subject').val($.trim($('#' + _thread_id + ' header.thread-head div.thread-title .inlineText').text()));
		}
	}

	function insertDirectLink() {
		// Direktlink by Nico
		// onClick entfernt
		$(".comments-list > li").each(function(_index) {
			var _directLink = document.location.protocol + '//' + document.location.hostname + document.location.pathname + '?page=' + $(".form--narrow input[name=cur_page]").val() +'#' + $(this).prop('id');
			$(this).find(".hList:first").append("<li class=\"comment-option hList-item\"> <a href=" + _directLink +"><button class=\"link ico ico--type-arrow-blue ico--pos-l\">Direktlink</button><a/></li>");
		});
	}
	
	function markPnRead() {
		if(settings.userMenuDirect.pn) {
			$.ajax({
				method: 'POST',
				url: 'http://www.mydealz.de/activity?type=pm',
				contentType: 'application/x-www-form-urlencoded',
				accept: 'application/json, text/javascript, */*',
				dataType: 'json'
			}).done(function () {
				$('#activities-count-pm').remove();
			});
		}
	}

	function filterPath() {
		_path = document.location.pathname.replace(/^\/(new|hot|discussed)$/, '/').replace(/^\/([^\/]+).*$/, '$1').replace(/(-?(new|discussed))?$/, '');
		if(_path != '/' && !(_path in settings.fromTo)) {
			return false;
		}
		return _path;
	}

	function startWaitForSubmit() {
		timer.submit = window.setInterval(waitForSubmit, 50);
	}

	function waitForSubmit() {
		if(!$('#commentForm').eq(0).hasClass('seal--spin')) {
			if('submit' in timer) {
				window.clearInterval(timer.submit);
				delete timer.submit;
			}
			modifyChangeLinks();
		}
	}

	function modifyChangeLinks() {
		var _changeLinks = $('ul.comments-list .ico--type-edit-blue').off('click', startWaitForChange).on('click', startWaitForChange);
		$('#commentForm').submit(startWaitForSubmit);
	}

	function startWaitForChange() {
		var _link = $(this);
		var _params = JSON.parse(_link.attr('data-replace'))[2];
		timer['change_'+_params.comment_id] = window.setInterval(function() { waitForChange(_params.comment_id)}, 50);
	}

	function waitForChange(_comment_id) {
		if(!$('ul.comments-list #post' + _comment_id + ' div.comments-body').eq(0).hasClass('seal--spin')) {
			if(('change_'+_comment_id) in timer) {
				window.clearInterval(timer['change_'+_comment_id]);
				delete timer['change_'+_comment_id];
			}
			$('ul.comments-list #post' + _comment_id + ' #commentEditForm-reason').attr('value', settings.comments.editReason).val(settings.comments.editReason);
			$('#commentEditForm-content-' + _comment_id).val($('#commentEditForm-content-' + _comment_id).val().replace(/\<br\s*\/\>$/m, ''));
		}
	}

	function modifyQuotes() {
		var _quoteLinks = $('.ico--type-quote-blue');
		if(_quoteLinks.length) {
			_quoteLinks.each(function (_qlInd, _qlVal) {
				var _quoteLink = $(_qlVal);
				_quoteLink.removeAttr('data-track').removeAttr('data-handler');
				_quoteLink.click(grabQuote);
			});
		}
	}

	function grabQuote() {
		var _quoteLink = $(this);
		var _params = JSON.parse(_quoteLink.attr('data-comment-quote'));
		$.ajax({
			method: 'POST',
			url: 'http://www.mydealz.de/comment?raw=1',
			contentType: 'application/x-www-form-urlencoded',
			accept: 'application/json, text/javascript, */*',
			data: 'comment_id=' + _params.commentId + '&thread_id=' + _params.threadId,
			dataType: 'json'
		}).done(function (_rdata) {
			if(typeof _rdata == 'object' && 'data' in _rdata && typeof _rdata.data == 'object' && 'comment_id' in _rdata.data) {
				var _comment = '[quote=' + _params.username + ']' + _rdata.data.content.replace(/\<br \/\>\[edit_reasons\][^]+\[\/edit_reasons\]/m, '') + '[/quote]';
				// vom HUKD Toolkit (Danke an lolnickname)
				/* Zeilenwechsel entfernen */
				_comment = _comment.replace(/^\s+$/g, '');
				while(/\n\r?\n/m.test(_comment)) {
					_comment = _comment.replace(/\n\r?\n/m,"\n");
				}
				/* mehrfache Leerzeichen */
				while(/(\S)[^\S\r\n]{2,}(\S)/.test(_comment)) {
					_comment = _comment.replace(/(\S)[^\S\r\n]{2,}(\S)$/g,"$1 $2");
				}
				/* einzelne Leerzeichen nach BBCode */
				_comment = _comment.replace(/quote(=([a-zA-Z0-9]+)?)?\]\s/g,"quote$1]");           
				/* einzelne Leerzeichen vor BBCode */
				_comment = _comment.replace(/\s\[(\/)?quote/g,"[$1quote");
				/* Leerzeichen an Anfang und Ende des Strings */
				_comment = $.trim(_comment);
				_comment = maxQuotes(_comment);
				$('#commentForm-content').insertAtCaret(_comment);
			}
		});
	}

	function maxQuotes(quoteContent) {
		// vom HUKD Toolkit (Danke lolnickname)
		var quoteStartPattern = /\[quote(=[0-9a-zA-Z_]+)?\]/g;
		var quoteEndPattern = /\[\/quote\]/g;
		var quoteStarts = quoteContent.match(quoteStartPattern);
		var quoteEnds = quoteContent.match(quoteEndPattern);
		var quoteStartCount = 0;
		var quoteEndCount = 0;

		if (quoteStarts && quoteEnds)	{
			quoteStartCount = quoteStarts.length;
			quoteEndCount = quoteEnds.length;
		}

		if (quoteStartCount != quoteEndCount) {
			alert("BBCodes fuer Zitate nicht eindeutig.");
			return quoteContent;
		}

		/* maximale Ebenen  */
		/* mit 0 beginnend */
		var maxQuoteLevel = settings.comments.maxQuoteLevel;
		if (quoteStartCount <= maxQuoteLevel) {
			return quoteContent;
		}

		var startPos = new Array();
		var endPos = new Array();  	
		for (var i = 0; i < quoteStartCount;  i++) {
			startPos[i] = quoteContent.indexOf(quoteStarts[i], startPos[i-1] + 1);
			endPos[i] = quoteContent.indexOf(quoteEnds[i], endPos[i-1] + 1);
		}

		var multiQuote = false;

		for (var i = 0; i < quoteStartCount;  i++) {
			for (var j = 0; j < quoteStartCount;  j++) {
				if (startPos[i] >= endPos[j]) {
			  	// not supported yet

			  	multiQuote = true;

			  	break;
				}
			}
			if (multiQuote) {
				break;
			};
		}
		if (multiQuote) {	
			return quoteContent;
		} else {
			var reducedQuote = '';
			var levelDiff =  Math.abs(maxQuoteLevel - quoteStartCount);
			contentToDiscard = quoteContent.substring(startPos[maxQuoteLevel], endPos[levelDiff - 1] + quoteEnds[levelDiff - 1].length);
			reducedQuote += quoteContent.replace(contentToDiscard, '');
			return reducedQuote;
		}
	}

	function addMenuSwitcher(_path) {
		addGlobalStyle('.enhancer-link-menuswitch { color: #ff0000 !important; }');
		_path = filterPath(_path);
		if(!_path) {
			return;
		}

		var _menu = $('.navTrigger2-row .fGrid-last .menu:first');
		if(_menu.length) {
			_menu.on('mouseenter', initMenuSwitch);
			_menu.on('mouseleave', stopMenuSwitch);
		}
	}

	function initMenuSwitch() {
		timer.menuSwitch = window.setTimeout(menuSwitch, 3000);
	}

	function menuSwitch() {
		var _menu = $('.navTrigger2-row .fGrid-last .menu:first');
		_menu.find('li a.nav2Morph-link').addClass('enhancer-link-menuswitch').on('click', savePageChoice);
		_menu.find('li a.nav2Morph-link--selected').addClass('enhancer-link-menuswitch--selected');
	}

	function savePageChoice() {
		var _elem = $(this);
		_path = filterPath(_path);
		if(!_path) {
			return;
		}

		settings.fromTo[_path] = _elem.attr('href').replace(document.location.origin + '/', '');
		saveSettings();
	}

	function stopMenuSwitch() {
		if('menuSwitch' in timer) {
			try {
				window.clearTimeout(timer.menuSwitch);
			} finally {
				delete timer.menuSwitch;
			}
		}
		var _menu = $('.navTrigger2-row .fGrid-last .menu:first');
		_menu.find('.enhancer-link-menuswitch').removeClass('enhancer-link-menuswitch').removeClass('enhancer-link-menuswitch--selected').off('click', savePageChoice);
	}

	function addImageZoom() {
		// Besten Dank an vielleichtmann1 für die Idee
		// Bilder-Vergroesserungs-Button einfuegen
		var _imgs = $('span.imageFrame .imageFrame-image');
		if(_imgs.length) {
			_imgs.each(function (_iInd, _iVal) {
				var _img = $(_iVal);
				var _link = $('<a>').attr('href', _img.attr('src').replace("threads/", "threads/high-res/")).attr('target', '_blank');
				_link.appendTo(_img.parent()).append(_img);
				_link.click(zoomImage);
			});
		}
		var _imgLinks = $('a.imageFrame');
		_imgLinks.click(zoomImage);
	}

	function zoomImage(_evt) {
		_evt.preventDefault();
		_evt.stopPropagation();
		var _link = $(this);
		var _container = $('<div>').attr('style', 'position: fixed; left: 0; right: 0; top: 0; bottom: 0; z-index: 99; background-color: rgba(68,68,68,0.5); white-space: nowrap; text-align: center; margin: 0;').appendTo($(document.body)).append($('<span>').attr('style', 'display: inline-block; height: 100%; vertical-align: middle;')).append($('<img>').attr('src', _link.attr('href')).attr('style', 'vertical-align: middle; max-height: ' + ($(window).height() - 2) + 'px; max-width: ' + ($(window).width() - 2) + 'px; border-radius: 5px; border: 1px solid #ffffff;'));
		_container.click(function () { $(this).remove();});
	}

	function addDealLinks() {
		// Danke an vielleichtmann1 für die Idee
		var _thread = $('div.thread div.thread-body div.section-sub').eq(0);
		if(_thread.length) {
			_thread.html(_thread.html().replace(/([^\";]https?:\/\/[^ ]+?)(\n|\<br\>|\s)/g, '<a href="$1" target="_blank">$1</a>$2'));
		}
	}

	function retrieveUserName() {
		try {
			userName = $('.userBar-userCenter-menu .avatar-frame').parent().html().replace(/(\r|\n)/g, '').replace(/^.*\\\/profile\\\/([^\\\<\>]+)\\\/ajax.*$/, '$1');
		} catch (e) {
			alert('Bitte einloggen.');
			window.location.href = 'https://www.mydealz.de/login';
		}
	}

	function addGlobalStyle(_css) {
		var _head, _style;
		_head = document.getElementsByTagName('head')[0];
		if (!_head) { return; }
		_style = document.createElement('style');
		_style.type = 'text/css';
		_style.innerHTML = _css;
		_head.appendChild(_style);
	}

	function loadSettings() {
		var _verbose = false;

		var _lSettings = JSON.parse(GM_getValue('settings', '{}'));
		var _sprop;

		// sinnvolleren, rekursiven Parser schreiben
		for (var _prop in defaultSettings) {
			if(_prop in _lSettings) {
				settings[_prop] = _lSettings[_prop];
			} else {
				settings[_prop] = defaultSettings[_prop];
			}

			if(_verbose) {
				GM_log(_prop + ': ' + settings[_prop]);
			}
			for(_sprop in defaultSettings[_prop]) {
				if(typeof _lSettings[_prop] == 'object' && _sprop in _lSettings[_prop]) {
					settings[_prop][_sprop] = _lSettings[_prop][_sprop];
				} else {
					settings[_prop][_sprop] = defaultSettings[_prop][_sprop];
				}
				if(_verbose) {
					GM_log(_prop + '.' + _sprop + ': ' + settings[_prop][_sprop]);
				}
			}
		}
	}

	function addSetupLink() {
		var _newMenu = $('<li>').addClass('hide--downThrough-').attr('id', 'mydealz-enhancer-setup--link').append($('<a>').addClass('navMenu1-item').attr('href','#mydealz-enhancer').html('MyDealz-Enhancer')).appendTo($('.navMenu--width-l').eq(0));
		_newMenu.find('a').click(showSetup);
	}

	function saveSettings() {
		GM_setValue('settings', JSON.stringify(settings));
	}

	function showSetup() {
		document.title = 'MyDealz-Enhancer Einstellungen';
		$('strong[data-handler="menu"]').parent().parent().removeClass('menu--active');
		$('.navTrigger1--selected, .navMenu1-item--selected').removeClass('navTrigger1--selected navMenu1-item--selected');
		$('#mydealz-enhancer-setup--link').addClass('navMenu1-item--selected');
		$('.navTrigger2-row').remove();
		$('#main').parent().find('aside').remove();
		$('.page-canvas > section').eq(0).remove();
		$('#main').html('').attr('class', 'content-main content-background').html('<ul class="breadcrumb section--padded--tight"><li class="breadcrumb-item size--all-xSmall size--xxSmall-small" itemtype="http://data-vocabulary.org/Breadcrumb" itemscope="itemscope" itemprop="child"><a class="breadcrumb-link link" href="/" itemprop="url">Home</a><ul class="breadcrumb-list "><li class="breadcrumb-item size--all-xSmall size--xxSmall-small" itemtype="http://data-vocabulary.org/Breadcrumb" itemscope="itemscope" itemprop="child"><span class="breadcrumb-current">Bearbeite Deine MyDealz-Enhancer Einstellungen</span></li></ul></li></ul><form class="form"><div class="section section--padded"><h1 class="section-title">Bearbeite Deine MyDealz-Enhancer Einstellungen</h1></div></form>');
		var _sections = $('#main .section--padded');
		var _form = $('#main form.form');
		_form.submit(submitSettings);

		var _section = $('<section>').addClass('section-sub').html('<h2 class="section-subTitle">Info und Diskussionsthread</h2><p>Informationen und Verbesserungsvorschl&auml;ge im <a href="http://www.mydealz.de/diverses/mydealz-enhancer-userscript-573012" class="link">MyDealz-Enhancer-Thread</a>.');
		_section.appendTo(_sections);

		_section = $('<section>').addClass('section-sub').html('<h2 class="section-subTitle">Werbung ausblenden</h2><ul class="form-list"><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="adForm-hideAds_topBar" type="checkbox" name="hideAds_topBar"> Top-Bar (Gutscheinsammler, Urlaubspiraten, …)</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="adForm-hideAds_nlSection" type="checkbox" name="hideAds_nlSection"> Newsletter</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="adForm-hideAds_inside" type="checkbox" name="hideAds_inside"> MyDealz Inside Banner</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="adForm-hideAds_gsWidget" type="checkbox" name="hideAds_gsWidget"> Gutschein-Widget</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="adForm-hideAds_app" type="checkbox" name="hideAds_app"> MyDealz-App</label></div></li></ul>');
		_section.appendTo(_sections);
		for(var _sInd in settings.hideAds) {
			if(settings.hideAds[_sInd]) {
				_section.find('#adForm-hideAds_' + _sInd).prop('checked', 'checked').attr('checked', 'checked');
			} else {
				_section.find('#adForm-hideAds_' + _sInd).prop('checked', false).removeAttr('checked');
			}
		}

		_section = $('<section>').addClass('section-sub').html('<h2 class="section-subTitle">Buttons im Hauptmenu</h2><ul class="form-list"><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="buttonForm-moveButtons__" type="checkbox" name="moveButtons__"> Alles</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="buttonForm-moveButtons_deals" type="checkbox" name="moveButtons_deals"> Deals</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="buttonForm-moveButtons_gutscheine" type="checkbox" name="moveButtons_gutscheine"> Gutscheine</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="buttonForm-moveButtons_freebies" type="checkbox" name="moveButtons_freebies"> Freebies</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="buttonForm-moveButtons_custom" type="checkbox" name="moveButtons_custom"> MyTab</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="buttonForm-moveButtons_gesuche" type="checkbox" name="moveButtons_gesuche"> Gesuche</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="buttonForm-moveButtons_diverses" type="checkbox" name="moveButtons_diverses"> Diverses</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="buttonForm-moveButtons_kleinanzeigen" type="checkbox" name="moveButtons_kleinanzeigen"> Kleinanzeigen</label></div></li></ul>');
		_section.appendTo(_sections);
		for(var _sInd in settings.moveButtons) {
			if(settings.moveButtons[_sInd]) {
				_section.find('#buttonForm-moveButtons_' + _sInd).prop('checked', 'checked').attr('checked', 'checked');
			} else {
				_section.find('#buttonForm-moveButtons_' + _sInd).prop('checked', false).removeAttr('checked');
			}
		}

		_section = $('<section>').addClass('section-sub').html('<h2 class="section-subTitle">UserMenu Direktlinks</h2><ul class="form-list"><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="userMenuForm-direct_pn" type="checkbox" name="direct_pn"> Private Nachrichten</label></div></li><li class="form-list-row"><span class="form-list-label"></span><div class="form-list-content"><label class="form-text"><input id="userMenuForm-direct_account" type="checkbox" name="direct_account"> Account</label></div></li></ul>');
		_section.appendTo(_sections);
		for(var _sInd in settings.userMenuDirect) {
			if(settings.userMenuDirect[_sInd]) {
				_section.find('#userMenuForm-direct_' + _sInd).prop('checked', 'checked').attr('checked', 'checked');
			} else {
				_section.find('#userMenuForm-direct_' + _sInd).prop('checked', false).removeAttr('checked');
			}
		}

		_section = $('<section>').addClass('section-sub').html('<h2 class="section-subTitle">Kommentieren</h2><ul class="form-list"><li class="form-list-row"><label class="form-list-label" for="commentForm-comments_maxQuoteLevel"> Zitattiefe </label><div class="form-list-content"><input id="commentForm-comments_maxQuoteLevel" class="input" type="number" min="1"	required="" name="maxQuoteLevel"></div></li><li class="form-list-row"><label class="form-list-label" for="commentForm-comments_editReason"> Grund der &Auml;nderung </label><div class="form-list-content"><input id="commentForm-comments_editReason" class="input" type="text" name="editReason"></div></li></ul>');
		_section.appendTo(_sections);
		for(var _sInd in settings.comments) {
			_section.find('#commentForm-comments_' + _sInd).val(settings.comments[_sInd]).attr('value', settings.comments[_sInd]);
		}

		_section.find('ul').append($('<li>').addClass('form-list-row').html('<span class="form-list-label"></span><div class="form-list-content"><input class="button button--type-primary" type="submit" name="save_privacy_options" value="Speichern"><input class="link form-cancel" type="reset" value="Zurücksetzen"></div>'));
	}

	function createMessageWindow(_text, _type) {
		var _messageList = $('#globalMsg .globalMsg-list');
		$('<li>').addClass('globalMsg-item globalMsg-item--type-success message  message--type-success').html('<li class="globalMsg-item globalMsg-item--type-' + _type + ' message  message--type-' + _type + '"><div class="message-inner mGrid"><span class="globalMsg-icon  globalMsg-icon--type-' + _type + ' mGrid-media centerChild2 ico ico--middle size--all-large"></span><button data-handler="globalMsg-close" class="globalMsg-close fGrid-right space--left-4 ico ico--type-close-' + (_type == 'error' ? 'red' : 'green' )+ '"></button><div class="mGrid-content"><div class="globalMsg-title">' + _text + '</div></div></div></li>').appendTo(_messageList);
		window.setTimeout(function () {
			try {
				_messageList.find('li').remove();
			} catch(e) {
			}
		}, 8000);
	}

	function submitSettings(_evt) {
		_evt.preventDefault();
		_evt.stopPropagation();

		var _allOK = true;
		for(var _sInd in settings.hideAds) {
			try {
				settings.hideAds[_sInd] = $('#adForm-hideAds_' + _sInd).prop('checked');
			} catch(e) {
					alert(e);
				_allOK = false;
			}
		}

		for(var _sInd in settings.moveButtons) {
			try {
				settings.moveButtons[_sInd] = $('#buttonForm-moveButtons_' + _sInd).prop('checked');
			} catch(e) {
				_allOK = false;
			}
		}

		for(var _sInd in settings.userMenuDirect) {
			try {
				settings.userMenuDirect[_sInd] = $('#userMenuForm-direct_' + _sInd).prop('checked');
			} catch(e) {
				_allOK = false;
			}
		}

		for(var _sInd in settings.comments) {
			try {
				settings.comments[_sInd] = ($('#commentForm-comments_' + _sInd).attr('type') == 'number' ? parseInt($('#commentForm-comments_' + _sInd).val(), 10) : $('#commentForm-comments_' + _sInd).val());
			} catch(e) {
				_allOK = false;
			}
		}

		if(_allOK) {
			saveSettings();
			createMessageWindow('Deine Einstellungen wurden gespeichert.<br />Seite neu laden zum Anwenden.', 'success');
		} else {
			createMessageWindow('Ein Fehler trat beim Speichern auf.', 'error');
		}
	}

	function init() {
		var _noError = true;
		try {
			var _path = document.location.pathname.replace(/^\/([^\?]+).*$/, '$1');
			if(/^(visit|image)/.test(_path) || /^JavaScript Shell/.test(document.title)) {
				return;
			}
			retrieveUserName();
			loadSettings();
			addMenuSwitcher(filterPath());
			remodelNavBar();
			removeAds();
			addCyberDealz();
			addSetupLink();
			changeLinks();

			if(document.location.hash == '#mydealz-enhancer') {
				showSetup();
			}
			switch(_path) {
				case 'cyber-monday/cyberdeals':
					addTimes();
				case 'profile/' + userName + '/messages':
					markPnRead();
			}

			addImageZoom();
			addFuncLinks();
			modifyQuotes();
			modifyChangeLinks();
			insertDirectLink();
			addDealLinks();
		} catch (e) {
			alert(e);
			_noError = false;
		}

		// Danke an lolnickname für die Idee
		$('.userBar-link .avatar-image').css( 'border', '1px dotted ' + (_noError ? '#00ff00' : '#aa0000' ) );
	}

	init();
}

// Erst laden, wenn DOM fertig ist
if(document.readyState == 'interactive') {
	ModifyPage();
} else {
	$(document).ready(ModifyPage);
}