User:NeilK/hackpad.js

// Script to include a remote editor (such as Hackpad) and receive the posted message back

// On all wiki pages // Create an editing button that will invoke hackpad // Pass in hackpad invocation URL //  uniqueID 'net:' + canonical URL + rev  -- to identify session //  title                                  -- so they can join sessions //  username                               -- so they can id the user in the interface //  eventPostback                          -- event we will listen for, to get article text via postMessage

// ... how does WYSIFTW do this

// Save the thing

( function( $, mw ) {    // change this if your wiki's domain name is not available on the public internet    var domainNamespace = 'INTERNET';    var configurations = {        hackpad: {             name: 'hackpad',            tabTitle: 'HackPad',            url: 'https://hackpad.com/ep/pad/anon',            protoHostPort: 'https://hackpad.com'        },        test: {            name: 'test',            tabTitle: 'TestRemoteEditor',            url: 'http://www.brevity.org/test/wikieditor.pl',            protoHostPort: 'http://www.brevity.org'        }    };

mw.remoteEditor = function( editorName ) { if ( typeof configurations[editorName] === undefined ) { alert( "don't have config for the remote editor: " + editorName ); return; }       this.editorConfig = configurations[editorName]; this.createTab; this.canonicalUrl = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) + '?' + $.param( {                    'title': mw.config.get( 'wgPageName' )                 } ); this.$form = $( 'form#editform' ); if ( this.isOurEditPage ) { this.launchEditor; }   };    mw.remoteEditor.prototype = { launchEditor: function { console.log( "launch the editor: " + this.editorConfig.name ); this.listenForEdit;

this.$form.hide; var form = this.$form.get(0); var pageTitle = mw.config.get( 'wgPageName' ); var sessionToken = $.map( [ domainNamespace, this.canonicalUrl, form.baseRevId.value ], encodeURIComponent ).join( ':' ); this.$iframe = $( ' ' ).attr( { 'width': '100%', 'height': '600px', 'name': 'remoteEditor' } ); this.$form.before( this.$iframe ); var $postForm = $( ' ' ) .attr( { 'method': 'post', 'action': this.editorConfig.url, 'target': 'remoteEditor' } ) .append(               $( ' ' ).val( sessionToken ),                $( '  ' ).val( mw.config.get( 'wgPageName' ) ),                $( '  ' ).val( mw.config.get( 'wgUserName' ) ),                $( '  ' ).val( form.wpTextbox1.value )            ); this.$form.before( $postForm ); $postForm.submit;

},       listenForEdit: function { var _this = this; function receiveMessage( event ) { if ( event.origin !== _this.editorConfig.protoHostPort ) { console.log( 'unauthorized postmessage -- ' + event.origin ); return; }               var data = JSON.parse( event.data ); _this.$form.get(0).wpTextbox1.value = data.content; _this.$iframe.hide; _this.$form.submit; }           window.addEventListener( "message", receiveMessage, false ); },

isOurEditPage: function { var currentUrl = window.location.href; return currentUrl.indexOf( 'action=edit' ) !== -1 &&                    currentUrl.indexOf( 'editor=' + this.editorConfig.name ) !== -1; },       createTab: function { var editUrl = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) + '?' + $.param( {                    'title': mw.config.get( 'wgPageName' ),                    'action': 'edit',                     'editor': this.editorConfig.name                 } ); $( '#p-views ul' ).prepend(                $( '' ).append( $(' ').append(                         $('' )                            .attr( { href: editUrl } )                            .html( this.editorConfig.tabTitle )                    ) )           );        }    }; } )( jQuery, mediaWiki );

var editor = new mw.remoteEditor( 'hackpad' );