Add a Date Picker to a textbox using Greasemonkey

后端 未结 1 1106
陌清茗
陌清茗 2021-01-15 05:09

There is a textbox that requires a date but has no date picker. I would like to add one with Greasemonkey. I looked for an example but could not find one.

Is this

相关标签:
1条回答
  • 2021-01-15 05:51

    I like to use jQuery UI's datepicker() because I usually have jQuery UI loaded anyway, and it's fairly well-known/supported.

    Unfortunately, the datepicker functionality uses some criminally-bad event code, which requires a little fudging to work in a sandboxed environment.

    Still, its not too difficult. Here is a complete Greasemonkey script:

    // ==UserScript==
    // @name        _Datepicker fun
    // @include     http://YOUR_SERVER/YOUR_PATH/*
    // @include     http://jsbin.com/ukelij*
    // @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
    // @require     https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js
    // @resource    jqUI_CSS  http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css
    // ==/UserScript==
    
    //--- Date picker needs additional CSS
    GM_addStyle (GM_getResourceText ("jqUI_CSS") );
    
    //--- Add datepicker popups to select inputs:
    $("#pickMe").datepicker ();
    $("#pickMe").click ( function () {
        setTimeout (cleanUpCrappyEventHandling, 100);
    } );
    
    /*--- Clean up ultra-crappy event handling ('til dev team eventually fixes).
        This must be done to allow the picker to work in a sandboxed environment.
        Alternately, you can modify the jQuery-UI source ala http://stackoverflow.com/q/2855403
    */
    function cleanUpCrappyEventHandling () {
        var nodesWithBadEvents  = $(
            "div.ui-datepicker td[onclick^='DP'], div.ui-datepicker a[onclick^='DP']"
        );
        nodesWithBadEvents.each ( function () {
            var jThis       = $(this);
            var fubarFunc   = jThis.attr ("onclick");
    
            /*--- fubarFunc will typically be like:
                DP_jQuery_1325718069430.datepicker._selectDay('#pickMe',0,2012, this);return false;
            */
            fubarFunc       = fubarFunc.replace (/return\s+\w+;/i, "");
    
            jThis.removeAttr ("onclick");
            jThis.click ( function () {
                eval (fubarFunc);
                cleanUpCrappyEventHandling ();
            } );
        } );
    }
    


    You can load this and test it against this page at jsBin.

    0 讨论(0)
提交回复
热议问题