js редактор кук — есть ещё порох в пороховницах

За пару часов сегодня накатал безделицу — user js для редактирования кук текущей страницы. Поскольку с хостингом у меня временные разногласия о сроках оплаты, кладу под кат. Кстати, без prototype.js работать не будет.

window.addEventListener( "load", function() {  
    var styles = {  
        editor: "position: fixed; top: 0; left: 0; width: 95%; background-color: white; color: black; padding: .5em",  
        cookiesList: "padding: 2px;",  
        cookie: "margin: 0; padding: 0;",  
        cookieCheckbox: "margin-left: 20px",  
        cookieLabel: "margin: 0; padding: 0;",  
        cookieEdit: "margin: 0; margin-left: 20px",  
        editName: "margin: 0; padding: 0;",  
        editValue: "margin: 0; padding: 0; display: block; width: 100%;",  
        editAdd: "margin: 0;",  
        closer: "margin: 0;"  
    }  
    var showCookiesEditor = function() {  
        if ( $( editor ) ) return;   
        var editor = document.createElement( 'div' );  
        editor.id = 'cookieEditor';  
        editor.style = styles.editor;  
        editor.appendChild( document.createElement( 'fieldset' ) );  
        editor.firstChild.appendChild( document.createElement( 'legend' ) );  
        editor.firstChild.firstChild.appendChild( document.createTextNode( 'Cookie editor' ) );

        var cookiesList = document.createElement( 'div' );  
        cookiesList.style = styles.cookiesList;  
        editor.firstChild.appendChild( cookiesList );

        var name = document.createElement( 'input' );  
        name.type = "text";  
        name.style = styles.editName;  
        var value = document.createElement( 'textarea' );  
        value.rows = 3;  
        value.style = styles.editValue;  
        var button = document.createElement( 'input' );  
        button.type = "button";  
        button.value = "Add";  
        button.style = styles.editAdd;  
        var closer = document.createElement( 'input' );  
        closer.type = "button";  
        closer.value = "Close";  
        closer.style = styles.closer;  
        closer.onclick = function() {  
            editor.parentNode.removeChild( editor );  
        }

        var cookies = [];

        var showCookie = function( cookie ) {  
            var tag = document.createElement( 'p' );  
            tag.style = styles.cookie;  
            var checkbox = document.createElement( 'input' );  
            checkbox.type = "checkbox"  
            checkbox.checked = 'checked';  
            checkbox.id = 'cookieEditorCheckbox' + cookie.name;  
            checkbox.style = styles.cookieCheckbox  
            checkbox.onclick = function() {   
                if ( checkbox.checked ) {  
                    document.cookie = cookie.name + '=' + cookie.value;  
                } else {  
                    document.cookie = cookie.name + '= 1;expires=Thu, 01-Jan-1970 00:00:01 GMT' ;  
                }  
            }

            var label = document.createElement( 'label' );  
            label.setAttribute( 'for', 'cookieEditorCheckbox' + cookie.name );  
            label.innerHTML = cookie.name + '=' + cookie.value;  
            label.style = styles.cookieLabel;

            var editButton = document.createElement( 'input' );  
            editButton.type = "button";  
            editButton.value = "Edit";  
            editButton.style = styles.cookieEdit  
            editButton.onclick = function() {  
                name.value = cookie.name  
                value.value = cookie.value  
            }

            tag.appendChild( checkbox );  
            tag.appendChild( label );  
            tag.appendChild( editButton );  
            cookiesList.appendChild( tag );  
        }

        $A( document.cookie.split( /;/ ) ).each( function( cookieString ) {  
            if ( !cookieString ) return;  
            var cookie = {   
                name: cookieString.substr( 0, cookieString.indexOf( '=' ) ),  
                value: cookieString.substr( cookieString.indexOf( '=' ) + 1 )  
            }  
            cookies.push( cookie )  
            showCookie( cookie );  
        });

        button.onclick = function() {  
            var existingCookie = $A( cookies ).find( function( cookie ) {   
                return cookie.name == name.value   
            })   
            var existingLabel = $A( cookiesList.getElementsByTagName( 'label' ) ).find( function( label ) {   
                return label.innerHTML.indexOf( name.value + '=' ) > -1  
            })

            document.cookie = name.value + '=' + value.value;  
            if ( existingCookie && existingLabel ) {  
                existingCookie.value = value.value;  
                existingLabel.innerHTML = name.value + '=' + value.value;  
                existingLabel.previousSibling.checked = "checked";  
            } else {  
                var cookie = { 'name': name.value, 'value': value.value };  
                cookies.push( cookie );  
                showCookie( cookie );  
            }  
        }

        editor.firstChild.appendChild( name );  
        editor.firstChild.appendChild( value );  
        editor.firstChild.appendChild( button );  
        editor.firstChild.appendChild( closer );  
        document.getElementsByTagName('body')[0].appendChild( editor )  
    }

    document.addEventListener( "keypress", function(e) {  
        if ( e.keyCode == '\\'.charCodeAt(0) && e.ctrlKey ) {  
            showCookiesEditor();  
        }  
    }, false )  
}, false )

Артемий Трегубенко,
,

комментарии