In early 19th century use, a stage-play (usually romantic and sentimental in plot and incident) in which songs were interspersed and in which the action was accompanied by orchestral music appropriate to the situations. In later use the musical element gradually ceased to be an essential feature of the 'melodrama', and the name now denotes a dramatic piece characterized by sensational incidents and violent appeals to the emotions, but with a happy ending. [OED]
The shift toward "a romantic and sensational dramatic piece with a happy ending" is evident by 1883. Also from French are Spanish melodrama, Italian melodramma, German melodram. Related: Melodramatize.
The melodramatist's task is to get his characters labelled good & wicked in his audience's minds, & to provide striking situations that shall provoke & relieve anxieties on behalf of poetic justice. [Fowler]