My Function library

enter something useful

目前為 2015-04-12 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greatest.deepsurf.us/scripts/9160/45900/My%20Function%20library.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

//// ==UserScript==
// @name            My Function library
// @namespace       http://use.i.E.your.homepage/
// @version         0.25
// @description     enter something useful

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

// @run-at          document-start

// @created         2015-04-06
// @released        2014-00-00
// @updated         2014-00-00
// @history         @version 0.25 - first version: public@released - 2015-04-12

// @compatible      Greasemonkey, Tampermonkey
// @license         GNU GPL v3 (http://www.gnu.org/copyleft/gpl.html)
// @copyright       2014+, Magnus Fohlström
// ==/UserScript==

/*global $, jQuery*/

/*jshint -W014, -W030, -W082*/
// -W014, laxbreak, Bad line breaking before '+'
// -W030, Expected assignment or funtion call insted saw an expression
// -W082, a function declaration inside a block statement

    $.fn.waitUntilExists = function (handler, shouldRunHandlerOnce, isChild){
        var found	= 'found',
            $this	= $(this.selector),
            $elements	= $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);
        if( !isChild )
        {
            (window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {})[this.selector] =
                window.setInterval(function () {
                    $this.waitUntilExists(
                        handler, shouldRunHandlerOnce, true);
                }, 500);
        }
        else if (shouldRunHandlerOnce && $elements.length)
        {
            window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
        }
        return $this;
    };

    $.fn.toggleClasses = function (add, remove, ifnone){
        var $this = $( this.selector );
        ifnone !== undefined && !$this.hasClass( add ) && !$this.hasClass( remove ) && $this.addClass( ifnone );
        $this.addClass( add ).removeClass( remove );
    };

    //ScrollZoomTune("div.thumb .title a",1,-25,1,'slow');
    function ScrollZoomTune(selection, zomms, tune, ani, speed)
    {
        var body = $('body'), sel = $( selection), position;
        sel.size() !== 0 && (
            body.css('zoom',zomms),
            position = sel.position(),
            position = position.top + tune,
            ani == 1 ?
                body.animate({ scrollTop: position * zomms }, speed ) :
                body.scrollTop( position * zomms )
        )
    }

    function inURL(search){
        var winLoc = window.location.href;
        return winLoc.search(search) !== -1;
    }

    function checkDividedIsInteger( num, div ){
        return ( num % div === 0 );
    }

    function isEven(value) {
        return ( value % 2 === 0 );
    }

    String.prototype.inURL = function(){
        var winLoc = window.location.href;
        return winLoc.search(this) !== -1;
    };

    String.prototype.inString = function(string){
        return string !== undefined ? string.search(this) !== -1 : false;
    };

    String.prototype.inElem = function(search){
        return this !== undefined ? this.search(search) !== -1 : false;
    };

    String.prototype.undef = function(replace){
        return this === undefined ? replace : this;
    };

    String.prototype.extract = function( startChar, endChar, inside)
    {
        var str = this,
            startCharIndex = str.indexOf( startChar ),
            endCharIndex = str.indexOf( endChar );

        str = str.substr( startCharIndex, endCharIndex);

        if( inside == 'yes' || inside == 1 || inside === true || inside == 'inside')
        {
            str = str.replace( startChar, '').replace( endChar, '');
        }

        return str;
    };
    /**
     * @return {string}
     */
    function Undefined(check,replace){
        return check === undefined ? replace.toString() : check.toString();
    }
    $.fn.extend({
        exists: function(){
            return this.length === 0 ? 0 : this.length;
        }
    });

    function roundFloat(num,dec){
        var d = 1;
        for (var i=0; i<dec; i++){
            d += "0";
        }
        return Math.round(num * d) / d;
    }

    function refreshElement( elem , speed ) //refreshElement('.videoPlayer','slow');
    {
        var data = $( elem ).html();
        $( elem ).empty().html( data ).fadeIn( speed );
    }

    String.prototype.lpad = function(padString, length) {
        var str = this;
        while ( str.length < length ) {
            str = padString + str; }
        return str;
    };

    String.prototype.formatString = function(){
        return this.toString()
            .split('!').join(' !').split('!;').join("!important;")
            .split(/\s+/g).join(' ')
            .split('{').join('{\n\t')
            .split('; ').join(';')
            .split(';').join(';\n\t')
            .split('*/').join('*/\n')
            .split(')*(').join(') * (')
            .split('}').join('}\n');
    };

    //myplugin.VideoTitleA("div.thumb .title a",'on');
    var VideoTitleA = function( elem , state ){
        $( elem ).each(function()
        {
            var strTitle = $(this).attr('title'),
                strText = $(this).attr('data-text'),
                strHtml = $(this).text();

            state == 'on' ? $(this).text(strTitle).attr('data-text',strHtml) : $(this).text(strText);
        });
    },
    /**
     * @return {string}
     */
    MultiString = function(f) {
        return f.toString().split('\n').slice(1, -1).join('\n');
    },
    c =
    {
        defaultState: 3,
        logState: function(){
            return GM_getValue('LogStateValue');
        },
        infoState: function(){
            return GM_getValue('InfoStateValue');
        },
        l: function( name, fn, showThis ){
            var State = this.logState() || this.defaultState;
            ( State !== 0 && State === (showThis || State) || State === 'all' ) &&
            window.console.log( name, fn !== undefined ? fn : '' );
        },
        i: function( name, fn, showThis ){
            var State = this.infoState() || this.defaultState;
            ( State !== 0 && State === (showThis || State) || State === 'all' ) &&
            window.console.info( name, fn !== undefined ? fn : '' );
        }
    },
    g = {
        winLoc  : window.location.href,
        ms      : 0,
        timer   : function(ms){
            g.ms = ms;
            setTimeout(function(){ g.ms = 0; },ms);
        }
    };

c.i('myFunctionLibrary');