Greasy Fork

Send Skype Credit+

Speed up process sending skype credit: removed animation and remembering settings for each person.

目前为 2020-05-26 提交的版本。查看 最新版本

// ==UserScript==
// @name        Send Skype Credit+
// @namespace   V@no
// @author      V@no
// @description Speed up process sending skype credit: removed animation and remembering settings for each person.
// @include     https://secure.skype.com/send-credit
// @version     1.1
// @license     MIT
// @grant       none
// ==/UserScript==

function $$(o)
{
	return document.getElementById(o);
}

function pref(id, data)
{
	if (typeof(data) == "undefined")
	{
		return prefs[id];
	}
	prefs[id] = data;
	pref.save();
}

pref.load = function()
{
	let p = ls("pref");
			r = _prefs.clone();

	if (!p || typeof(p) != "object")
		p = r;

	for(let i in r)
	{
		if (typeof(r[i]) == typeof(p[i]))
			r[i] = p[i];
	}
	return r;
}

pref.save = function()
{
	ls("pref", prefs);
}

function ls(id, data, stringify)
{
	let r;
	if (typeof(data) == "undefined")
	{
		r = localStorage.getItem(id);
		try
		{
			r = JSON.parse(r);
		}
		catch(e)
		{
			log(e);
			log([id, data, r]);
		}
		return r;
	}

	if (typeof(stringify) == "undefined" || stringify)
		data = JSON.stringify(data);

	r = localStorage.setItem(id, data);
	return r;
}

function rand(min, max)
{
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

Object.prototype.clone = function()
{
	return Object.assign({}, this);
}

Array.prototype.clone = function()
{
	return Object.assign([], this);
}

function load()
{
	let l = ls("last"),
			r = listDefault.clone();

	if (!l || typeof(l) != "object")
		l = {};

	if (!l.n)
		l.n = "";

	if (!l[l.n])
		l[l.n] = r;

	for (let m in l)
	{
		if (["n", "clone"].indexOf(m) != -1)
			continue;

		let d = l[m];
		if (Array.isArray(d))
		{
			for(let i = 0; i < r.length; i++)
			{
				if (typeof(d[i]) == typeof(r[i]))
					r[i] = d[i];
			}
			l[m] = d;
		}
		else
			l[m] = r;
	}
	return l;
}

function save()
{
	ls("last", list);
}

function getId(o)
{
	if (!o)
		return;

	if(!o.id || !o.id.match("contactListId-"))
		return getId(o.parentNode);

	return o.id.replace("contactListId-", "");
}

let log = console.log.bind(console),
		listDefault = [2, 1],
		_prefs = {},
		prefs = pref.load(),
		list = load(),
		prevId,
		scope = this,
		func = loop1,
		o = {};


if ($ && $.fn)
{
	$.fn.animate = function(){};
}

//type username
function loop1()
{
	let obj = $$("quickFilterInput"),
			oe = $$("searchOptions"),
			f = arguments.callee,
			n = f.name;

	function c()
	{
		let f,i = 2;

		while(typeof(f=scope["loop"+i++]) == "function")
		{
			f();
		}
		prevId = list.n;
	}

	if (oe && !oe._inited)
	{
		oe.addEventListener("mousedown", function(e)
		{
			if (!e.isTrusted)
				return;

			let id = getId(e.target);
			if (!id)
				return;

			list.n = id;
			if (!list[id])
				list[id] = listDefault.clone();

			delete list[""];
			save();
			prevId = null;
			func = f;
		}, true);
		oe._inited = true;
	}

	if (!obj)
		return c();

	if (obj.value == "" && !o[n])
	{
		obj.value = list.n;
		obj.dispatchEvent(new KeyboardEvent('keydown',{'key':'Shift'}));
		o[n] = obj;
	}
	c();
}

//pick user from list
function loop2()
{
	let f = arguments.callee,
			n = f.name,
			obj = $$("contactListId-" + list.n) || o[n];

	if (!obj)
		return;

	o[n] = obj;
	if (list[list.n])
		obj.dispatchEvent(new MouseEvent('mousedown',{'button':0}));
}

function findChild(obj, parent)
{
	if (!obj || !parent || obj == parent)
		return null;

	if (obj.parentNode == parent)
		return obj;

	return findChild(obj.parentNode, parent);
}
//select amount
function loop3()
{
	let f = arguments.callee,
			n = f.name,
			obj = $$("offer-selection");

	if (!obj)
		return;

	if (!obj._inited)
	{
		if (list[list.n][0] < 0 || list[list.n][0] > obj.children.length - 1)
			list[list.n][0] = obj.children - 1;

		obj.children[list[list.n][0]].click();
		obj.addEventListener("click", function(e)
		{
			if (!e.isTrusted)
				return;

			let c = findChild(e.target, obj),
					i = -1;

			if (c === null)
				return;

			while(obj.children[++i] != c);
			list[list.n][0] = i;
			save();
		}, true);
		obj._inited = true;
	}
	o[n] = obj;
}

//select design
function loop4()
{
	let f = arguments.callee,
			n = f.name,
			obj = document.querySelector(".form-area.gift-card-holder") || o[n];

	if (!obj)
		return;

	if (!obj._inited)
	{
		let cb = document.createElement("input"),
				box = document.createElement("label"),
				p = {
					get m ()
					{
						return obj.children.length - 1;
					},
					get i()
					{
						return Math.min(p.m, Math.max((list[list.n][1] >> 1) - 1, 0));
					},
					set i(d)
					{
						d++;
						list[list.n][1] = (list[list.n][1] & 1) | (d << 1);
					},
					get r()
					{
						return list[list.n][1] & 1 ? true : false;
					},
					set r(d)
					{
						if (d)
							list[list.n][1] |= 1;
						else
							list[list.n][1] &= ~1;
					}
				};

		function sel()
		{
			let i = p.i;
			if (cb.checked)
			{
//				i = rand(0, p.m);
				while((i = rand(0, p.m)) == p.i);
			}
			obj.children[i].click();
		}

		cb.type = "checkbox";
		cb.checked = p.r;
		cb.addEventListener("input", function(e)
		{
			p.r = cb.checked;
			sel();
		}, false);
		sel();
		box.appendChild(cb);
		box.appendChild(document.createTextNode("Random"));
		obj.parentNode.insertBefore(box, obj);
		obj.addEventListener("click", function(e)
		{
			if (!e.isTrusted && cb.checked)
			{
				p.r = true;
			}
			else
			{
				let c = findChild(e.target, obj),
						i = -1;

				if (c === null)
					return;

				while(obj.children[++i] != c);
				p.i = i;
				p.r = cb.checked = false;
			}
			save();
		}, true);
		obj._inited = true;
	}
	o[n] = obj;
}

//click confirm
function loop5()
{
	let f = arguments.callee,
			n = f.name,
			obj = $$("send-money");

	if (!obj)
		return;

	if (!obj._inited || prevId != list.n)
	{
		setTimeout(function()
		{
			obj.scrollIntoView(false);
			func = null;
		}, 500);
		obj._inited = true;
	}
	o[n] = obj;
//	obj.click();
}

//css
let css = document.createElement("style");
css.innerHTML = `
.offer-item
{
	height: unset;
	padding-top: 5px;
	padding-bottom: 5px;
}
.gift-card-span
{
	max-width: 22%;
	left: unset !important;
	right: unset !important;
	margin-top: unset;
}
.offers-footer,
.content-wrapper,
#amount-selector > div.row
{
	padding: 10px;
}
.gift-card-holder
{
	margin-top: 0.2em;
}

#design-selector > h3
{
	display: inline-block;
}
#design-selector > label
{
	float: right;
	line-height: initial;
	margin: 0;
	padding: 0;
	color: initial;
	font-weight: initial;
	width: initial;
}
#design-selector > label,
#design-selector > label > input
{
	cursor: pointer;
}
`;
document.head.appendChild(css);

//main loop
!function loop()
{
	if (func)
	{
//log(func && func.name);
		func();
	}
	setTimeout(loop, rand(0, 1000));
}();