DOTABUFF Player Shortcuts

Add player shortcut links to DOTABUFF

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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();
		});
	}
});