Sure, here you go

// begin code //

var cookie_name = 'WP_LiveSaver',
        element = 'wpTextbox1';

var LiveSaver = {
        init: function() {
                this.lastSave = (new Date()).getTime();

                var wikiSection = window.location.href.match(/section=\d+/),
                        wikiPage = wgTitle;

                if(!wikiPage) {
                        return false;
                } else {
                        if(wikiSection) {
                                wikiPage += '&' + wikiSection[0];
                        }
                }

                this.page = wikiPage;

                this.data = {};

                // Add icon to the top left corner of window
                $('#p-personal div.pBody ul').append('<li id="LiveSaverIcon"></
li>');

                this.cookie = new LiveSaverCookie();
                var tmpData = JSON.parse(this.cookie.load());
                if(tmpData) {
                        this.data = tmpData;
                }

                // Check if extension is disabled or enabled
                if(typeof this.data.enabled === 'undefined' || 
this.data.enabled ===
true) {
                        this.enable();
                } else {
                        this.disable();
                }
        },

        enable: function() {
                // Set save threshold to 2.5 seconds
                this.saveThreshold = 2500;

                $('#LiveSaverIcon')
                        .css('background-position', '0 center')
                        .attr('title', 'LiveSaver ' + LiveSaver_version + ' 
Click to
disable');

                if(!this.data[this.page]) {
                        this.data[this.page] = {};
                }
                if(!this.data[this.page][element]) {
                        this.data[this.page][element] = '';
                }
                if(!this.data[this.page]['rev']) {
                        this.data[this.page]['rev'] = '';
                }

                this.data.enabled = this.enabled = true;
                this.save();
        },

        disable: function() {

                // Set very high save threshold to save browser resources
                this.saveThreshold = 5000000;

                $('#LiveSaverIcon')
                        .css('background-position', '-16px center')
                        .attr('title', 'LiveSaver ' + LiveSaver_version + ' 
Click to
enable');

                // Remove all existing data
                this.data = {};

                this.data.enabled = this.enabled = false;
                this.save();
        },

        register: function() {

                // The readonly attribute is set when the current user doesn't 
have
access to editing the current page.
                if($('#' + element).attr('readonly') === true) {
                        return;
                }

                // Check if this page has been previously saved in the cookie
                if(this.enabled === true && this.has(element)) { //get saved 
data
                        var value = this.get(element);

                        if($('#' + element).val() != value) {
                                if(this.data[this.page]['rev'] != 
wgCurRevisionId) {
                                        $('#editform').before('<div 
id="notice"><p>This page has changed
since you started editing it.</p></div>');
                                        return;
                                } else {

                                        // Append warning message
                                        $('#editform').before('<div 
id="notice"><p>There is an autosave
of this page that is more recent than the version below. <a href=""
id="LiveSaver_Restore">Restore old version</a>.</p></div>');

                                        // Wait for user to decide to move to 
new version.
                                        
$('#LiveSaver_Restore').click(function(e) {
                                                // Prevent link from being 
followed
                                                e.preventDefault();

                                                // Replace text
                                                $('#' + 
element).val(LiveSaver.get(element));

                                                // Remove notice
                                                $('#notice').remove();
                                        });
                                }
                        }
                }

                // Watch for multiple events in the textbox
                $('#' + element).bind('keyup focus blur', function() {

                        // Check if enough time has passed
                        if(LiveSaver.enabled === true && ((new 
Date()).getTime() -
LiveSaver.lastSave) > LiveSaver.saveThreshold) {
                                // Update last saved time
                                LiveSaver.lastSave = (new Date()).getTime();

                                // Only save if textbox has changed since last 
save
                                if($('#' + element).val() != 
LiveSaver.data[LiveSaver.page]
[element]) {
                                        LiveSaver.add(element, $('#' + 
element).val());
                                        LiveSaver.save();
                                }
                        }
                });
        },

        save: function() {
                var json = JSON.stringify(this.data);
                this.cookie.save(json);
        },

        add: function(key, val) {
                // Save everythng that is in the form
                this.data[this.page][key] = val;

                // Save revision id
                this.data[this.page]['rev'] = wgCurRevisionId;
        },

        get: function(key) {
                if(this.has(key)) {
                        return this.data[this.page][key];
                } else {
                        return false;
                }
        },

        reset: function() {
                if(this.data[this.page][element]) {
                        this.data[this.page][element] = '';
                }
                if(this.data[this.page]['rev']) {
                        this.data[this.page]['rev'] = '';
                }

                this.save();
        },

        has: function(key) {
                if(this.data[this.page][key].length > 0) {
                        return true;
                } else {
                        return false;
                }
        }
};

var LiveSaverCookie = function(e) {
        this.save = function(data) {
                $.cookie(cookie_name, escape(data), { expires: 1000 });
        };

        this.load = function() {
                return unescape($.cookie(cookie_name));
        };
};

function LiveSaver_init() {
        LiveSaver.init();

        LiveSaver.register();

        $('#LiveSaverIcon').live('click', function(event) {
                // event.which tells us which mouse button was used, 1 is the 
left
mouse button
                if(event.which === 1) {
                        if(LiveSaver.enabled === true) {
                                LiveSaver.disable();
                        } else {
                                LiveSaver.enable();
                        }
                }
        });

        // Reset when the Cancel or submit button is clicked
        $('#wpSave, #editform .editHelp a:contains("Cancel")').click(function
() {
                LiveSaver.reset();
        });

        // Save changes before moving to next page
        $('#wpPreview, #wpDiff').click(function() {
                LiveSaver.add(element, $('#' + element).val());
                LiveSaver.save();
        });
}

// Set LiveSaver_init to run after page has finished loading
addOnloadHook(LiveSaver_init);

// End code //

On Feb 3, 3:04 am, Stephan Veigl <stephan.ve...@gmail.com> wrote:
> Hi Caleb,
>
> Could you please post your LiveSaver object and the element variable
> initialization as well.
> I tested your code with a more or less meaningful dummy implementation
> of LiveSaver and element and it works for me in: FF3, Chrome1, Opera9,
> IE8
>
> I guess the problem is either in LiveSaver.add() or LiveSaver.save().
>
> by(e)
> Stephan
>
> 2009/2/3 Caleb Morse <morse.ca...@gmail.com>:
>
> > I am having trouble getting this piece of code to work correctly in Chrome.
> > It works just fine in in IE7, IE8, and FF. Watching the Chrome javascript
> > console doesn't reveal anything useful.
> > I manually checked each value in the if statements, and they are all coming
> > back true.
> > The textbox is not created or changed using javascript so I shouldn't need
> > to use .live.
>
> > $('#textbox').bind('keyup focus blur', function() {
> > if(LiveSaver.enabled === true && ((new Date()).getTime() -
> > LiveSaver.lastSave) > LiveSaver.saveThreshold) {
>
> > LiveSaver.lastSave = (new Date()).getTime();
> > if($('#' + element).val() != LiveSaver.data[LiveSaver.page][element]) {
> > LiveSaver.add(element, $('#' + element).val());
> > LiveSaver.save();
> > }
> > }
> > });
> > -- Caleb

Reply via email to