mean.cloud common library

Common functions I use for most of my scripts

As of 2018-01-09. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/37180/242211/meancloud%20common%20library.js

// @grant         GM_getValue
// @grant         GM_setValue
// @grant         GM_deleteValue

document.LoadChain = function(scriptSrc, callback, bJQuery)
{
    if (bJQuery === undefined) bJQuery = false;
    
    var script = document.createElement("script");
    script.setAttribute("src", scriptSrc);
    script.addEventListener('load', function()
    {
        var script = document.createElement("script");
        script.textContent = ((bJQuery) ? "window.jQ=jQuery.noConflict(true);" : "") + "(" + callback.toString() + ")();";
        document.body.appendChild(script);
    }, false);
    document.body.appendChild(script);
};

/*
document.LoadJQ = function(oCallback)
{
    var script = document.createElement("script");
    script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js");
    script.addEventListener('load', function()
    {
        var subscript = document.createElement("script");
        if (oCallback === undefined)
            subscript.textContent = "window.jQ=jQuery.noConflict(true);";
        else
            subscript.textContent = "window.jQ=jQuery.noConflict(true);(" + oCallback.toString() + ")();";
        document.body.appendChild(subscript);
    }, false);
    document.body.appendChild(script);
};
*/

document.LoadJavascript = function(sScriptSrc, oCallback)
{
    var script = document.createElement("script");
    script.setAttribute("src", sScriptSrc);    
    if (oCallback !== undefined)
    {
        script.addEventListener('load', function()
        {
            var subscript = document.createElement("script");
            subscript.textContent = "(" + oCallback.toString() + ")();";
            document.body.appendChild(subscript);
        }, false);
    }
    document.body.appendChild(script);
};

document.time_val = function()
{
    return document.CDbl(Math.floor(Date.now() / 1000));
};

document.Random = function(iMin, iMax)
{
    return parseInt(iMin + Math.floor(Math.random() * iMax));
};

document.ShiftClick = function(oEl)
{
    if (oEl === undefined)
    {
        var shiftclick = jQ.Event("click");
        shiftclick.shiftKey = true;

        var shiftclickOrig = jQ.Event("click");
        shiftclickOrig.shiftKey = true;

        shiftclick.originalEvent = shiftclick;
        return shiftclick;
    }
    
    jQ(oEl).trigger(document.ShiftClick());
};

if (!String.prototype.replaceAll)
    String.prototype.replaceAll = function(search, replace) { return ((replace === undefined) ? this.toString() : this.replace(new RegExp('[' + search + ']', 'g'), replace)); };

if (!String.prototype.startsWith)
    String.prototype.startsWith = function(search, pos) { return this.substr(((!pos) || (pos < 0)) ? 0 : +pos, search.length) === search; };

document.CInt = function(v)
{
	try
	{
		if (!isNaN(v)) return Math.floor(v);
		if (typeof v === 'undefined') return parseInt(0);
		if (v === null) return parseInt(0);
		var t = parseInt(v);
		if (isNaN(t)) return parseInt(0);
		return Math.floor(t);
	}
	catch (err) { }

	return parseInt(0);
};

document.CDbl = function(v)
{
	try
	{
		if (!isNaN(v)) return parseFloat(v);
		if (typeof v === 'undefined') return parseFloat(0.0);
		if (v === null) return parseFloat(0.0);
		var t = parseFloat(v);
		if (isNaN(t)) return parseFloat(0.0);
		return t;
	}
	catch (err) { }

	return parseFloat(0.0);
};

// dup of String.prototype.startsWith, but uses indexOf() instead of substr()
document.startsWith = function (haystack, needle) { return (needle === "") || (haystack.indexOf(needle) === 0); };
document.endsWith   = function (haystack, needle) { return (needle === "") || (haystack.substring(haystack.length - needle.length) === needle); };

document.ChopperBlank = function (sText, sSearch, sEnd)
{
	var sIntermediate = "";

	if (sSearch === "")
		sIntermediate = sText.substring(0, sText.length);
	else
	{
		var iIndexStart = sText.indexOf(sSearch);
		if (iIndexStart === -1)
			return "";

		sIntermediate = sText.substring(iIndexStart + sSearch.length);
	}

	if (sEnd === "")
		return sIntermediate;

	var iIndexEnd = sIntermediate.indexOf(sEnd);

	return (iIndexEnd === -1) ? sIntermediate : sIntermediate.substring(0, iIndexEnd);
};

document.CondenseSpacing = function(text)
{
	while (text.indexOf("  ") !== -1)
		text = text.replace("  ", " ");
	return text;
};

document.CookieSet    = function(name, value)  { try { GM_setValue(name, value); } catch (err) { } };
document.CookieGet    = function(name, defVal) { try { return GM_getValue(name, ((defVal !== undefined) ? defVal : null)); } catch (err) { } return null; };
document.CookieDelete = function(name)         { try { GM_deleteValue(name); } catch (err) { } };

document.pad = function(sText, iWidth, sChar)
{
    sChar = ((sChar !== undefined) ? sChar : ('0'));
    sText = sText.toString();
    return ((sText.length >= iWidth) ? (sText) : (new Array(iWidth - sText.length + 1).join(sChar) + sText));
};

document.is_visible = (function () {
    var x = window.pageXOffset ? window.pageXOffset + window.innerWidth - 1 : 0,
        y = window.pageYOffset ? window.pageYOffset + window.innerHeight - 1 : 0,
        relative = !!((!x && !y) || !document.elementFromPoint(x, y));
        function inside(child, parent) {
            while(child){
                if (child === parent) return true;
                child = child.parentNode;
            }
        return false;
    };
    return function (elem) {
        if (
            document.hidden ||
            elem.offsetWidth==0 ||
            elem.offsetHeight==0 ||
            elem.style.visibility=='hidden' ||
            elem.style.display=='none' ||
            elem.style.opacity===0
        ) return false;
        var rect = elem.getBoundingClientRect();
        if (relative) {
            if (!inside(document.elementFromPoint(rect.left + elem.offsetWidth/2, rect.top + elem.offsetHeight/2),elem)) return false;
        } else if (
            !inside(document.elementFromPoint(rect.left + elem.offsetWidth/2 + window.pageXOffset, rect.top + elem.offsetHeight/2 + window.pageYOffset), elem) ||
            (
                rect.top + elem.offsetHeight/2 < 0 ||
                rect.left + elem.offsetWidth/2 < 0 ||
                rect.bottom - elem.offsetHeight/2 > (window.innerHeight || document.documentElement.clientHeight) ||
                rect.right - elem.offsetWidth/2 > (window.innerWidth || document.documentElement.clientWidth)
            )
        ) return false;
        if (window.getComputedStyle || elem.currentStyle) {
            var el = elem,
                comp = null;
            while (el) {
                if (el === document) {break;} else if(!el.parentNode) return false;
                comp = window.getComputedStyle ? window.getComputedStyle(el, null) : el.currentStyle;
                if (comp && (comp.visibility=='hidden' || comp.display == 'none' || (typeof comp.opacity !=='undefined' && comp.opacity != 1))) return false;
                el = el.parentNode;
            }
        }
        return true;
    };
})();