DOTABUFF Player Shortcuts

Add player shortcut links to DOTABUFF

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        DOTABUFF Player Shortcuts
// @namespace   bazetts
// @include     https://dotabuff.com/*
// @grant       GM_getValue
// @grant       GM_setValue
// @description Add player shortcut links to DOTABUFF
// @version     1
// ==/UserScript==

// JQuery fix for chrome
function with_jquery(f) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.textContent = "(" + f.toString() + ")(jQuery)";
    document.body.appendChild(script);
};

with_jquery(function($) {
	// GM_getValue for chrome
	if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported")>-1)) {
		this.GM_getValue=function (key,def) {
			return localStorage[key] || def;
		};
		this.GM_setValue=function (key,value) {
			return localStorage[key]=value;
		};
		this.GM_deleteValue=function (key) {
			return delete localStorage[key];
		};
	}
	
	// Shortcut-div
	function updateShortcuts()
	{
		$('#shortcuts').remove();
		var text = '<div id="shortcuts">';
		$.each(shortcuts, function(index, value){
			text += '<a href="/players/' + index + '/">' + value + '</a> - ';
		});
		text += '</div>';
		$(text).css('margin-top', '10px').insertBefore('#content-header');
	}
	
	var pathname = window.location.pathname.split('/');
	var shortcuts = JSON.parse(GM_getValue('shortcuts', '{}'));
	
	updateShortcuts();
	
	// Add Shortcut-button on player pages
	if (pathname[1] == "players")
	{
		var add = (pathname[2] in shortcuts ? false : true);
		$('<div id="shortcut-add"><a href="#">' + (add ? 'Add' : 'Remove') + ' shortcut</a></div>').insertAfter('#content-header-primary > .content-header-title > h1');
		$('#shortcut-add').click(function(){
			if(add)
				shortcuts[pathname[2]] = $('.image-avatar').attr('alt');
			else
				delete shortcuts[pathname[2]];
			
			add = !add;
			$('#shortcut-add > a').html((add ? 'Add' : 'Remove') + ' shortcut');
			GM_setValue('shortcuts', JSON.stringify(shortcuts));
			updateShortcuts();
		});
	}
});