Page MenuHomePhabricator

Provide a hook that would be run just before page is saved by VisualEditor
Closed, DeclinedPublic

Description

Please create a hook in the JS code for VisualEditor that would allow modifying the HTML of the page prior to the save actually happening. Or if such a hook already exists, please point us to it and close this task.

Context: we have this awesome Gadget on fawiki which identifies dictation errors and highlights them in the text. Highlighting is done by wrapping the words or phrases in <span> tags with style attributes for the color. The problem is, when this gadget is enabled and so is the VisualEditor, upon page save, the span tag is also consumed by visual editor and the page output will permanently contain the highlights (see this diff as an example). We would like to change the code such that when a page save is requested from VisualEditor, our gadget would get a callback (via the requested hook) and cleanup all these <span> tags.

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

You could use the same mechanism as we currently use to clean up after browser extensions that add their markup to pages in a similar way to your script. You could override ve.init.mw.Target.prototype.getHtml and add your own cleanup to it, similar to the existing code there.

Try experimenting with this (largely untested):

var old_getHtml = ve.init.mw.Target.prototype.getHtml;
ve.init.mw.Target.prototype.getHtml = function ( newDoc, oldDoc ) {
	$( newDoc ).find( '.CheckDictation-marked' ).contents().unwrap();
	return old_getHtml.call( this, newDoc, oldDoc );
};

I'm not sure how I feel about adding a hook to do this. Ideally, I think, that gadget would be rewritten to integrate with VE in another way, perhaps more similar to how the find-and-replace dialog's highlighting works… Of course that would probably be a lot of work.

Thanks Bartosz. I agree that a rewrite will be necessary down the road, but for now, your proposed approach might be a good starting point.

Adding Ebrhaim too. Between him and Yamaha5, we should be able to evaluate the proposed approach quickly.

Esanders subscribed.

A gadget should be unable to modify the DM HTML. Maybe some bug in the past was allowing changes to view HTML to get through to the DM HTML. Also modifying VE's view HTML is not a good idea either.