Tampermonkey Support Library

to reduce repetition in creating scripts for Tampermonkey support

Fra 07.05.2018. Se den seneste versjonen.

Dette scriptet burde ikke installeres direkte. Det er et bibliotek for andre script å inkludere med det nye metadirektivet // @require https://update.greatest.deepsurf.us/scripts/23115/595953/Tampermonkey%20Support%20Library.js

var tm = {
    addGlobalStyle: function(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);
    },
    log: function(msg) {
        console.log('Tampermonkey: ' + msg);
    },
    selectText: function (targetClass) {
        var textToCopy, range;
        try {
            if (document.selection) {
                range = document.body.createTextRange();
                range.moveToElementText(document.getElementsByClassName(targetClass)[0]);
                range.select();
            } else if (window.getSelection) {
				var selection = window.getSelection();
                range = document.createRange();
                range.selectNode(document.getElementsByClassName(targetClass)[0]);
				selection.removeAllRanges();
                selection.addRange(range);
            }
        } catch (err) {
            tm.log('Failed to select text');
        }
    },
    sysFriendly: function(el) {
        var text = $(el).text();
        $(el).text(text.replace(/\/|\:|\<|\>|\\|\||\*|\?/g, '-'));
    },
    ping: function (ip, callback) { // via http://jsfiddle.net/GSSCD/203/
        if (!this.inUse) {
            this.status = 'unchecked';
            this.inUse = true;
            this.callback = callback;
            this.ip = ip;
            var _that = this;
            this.img = new Image();

            this.img.onload = function () {
                _that.inUse = false;
                _that.callback('responded');
            };

            this.img.onerror = function (e) {
                if (_that.inUse) {
                    _that.inUse = false;
                    _that.callback('error', e);
                }
            };

            this.start = new Date().getTime();
            this.img.src = "http://" + this.ip;
            this.timer = setTimeout(function () {
                if (_that.inUse) {
                    _that.inUse = false;
                    _that.callback('timeout');
                }
            }, 1500);
        }
    },
    isTagIn: function (targetString, tags) {
        var isFound = false;
        _.each(tags, function scanTarget (tag) {
            if (targetString.toLowerCase().indexOf(tag.toLowerCase()) > -1) {
                isFound = true;
            }
        });
        return isFound;
    },
    copyTextToClipboard: function (text) {
        var copyFrom = document.createElement("textarea");
        copyFrom.textContent = text;
        var body = document.getElementsByTagName('body')[0];
        body.appendChild(copyFrom);
        copyFrom.select();
        document.execCommand('copy');
        body.removeChild(copyFrom);
    },
    showModal: function(modalId, modalBody) {
        if ($('#' + modalId).is(":visible")) {
            $('#' + modalId).remove();
        } else {
            $('body').append('<div class="popupDetailWindow" id="' + modalId + '">' +
                             '    <div class="popupDetailTitle">&nbsp;</div>' +
                             '    <div class="popupDetailContent fingery" style="text-align:right;" onclick="$(this).parent().remove()">[CLOSE]</div>' +
                             '    ' + modalBody +
                             '</div>');
            }
        },
    waitTimers: [],
    getContainer: function (opts) {
        var options = {
            id: opts.id ? opts.id : opts.el.replace(/[ (:)]/g, ''),
            el: opts.el,
            try: 0,
            max: opts.max ? opts.max : 20,
            spd: opts.spd ? opts.spd : 500
        };
        clearTimeout(tm.waitTimers[options.id]);
        return new Promise(function (resolve, reject) {
            tm.waitForContainerElement(resolve, options);
        });
    },
    waitForContainerElement: function (resolve, options) {
        var $configElement = $(options.el);
        if ($configElement.length === 0) {
            options.try++;
            if (options.try < options.max) {
                tm.waitTimers[options.id] = setTimeout(tm.waitForContainerElement.bind(this, resolve, options), options.spd);
            } else {
                $('#output').val($('#output').val() + 'Maximum searches reached\n');
            }
        } else {
            resolve($configElement);
        }
    }
};