The story of Heracles being swallowed by the sea-monster appears in the Alexandra or Cassandra attributed to Lycophron:
αἰαῖ, τάλαινα θηλαμών, κεκαυμένη 
καὶ πρόσθε μὲν πεύκαισιν οὐλαμηφόροις
τριεσπέρου λέοντος, ὅν ποτε γνάθοις
Τρίτωνος ἠμάλαψε κάρχαρος κύων:
ἔμπνους δὲ δαιτρὸς ἡπάτων φλοιδούμενος 
τινθῷ λέβητος ἀφλόγοις ἐπ᾽ ἐσχάραις
σμήριγγας ἐστάλαξε κωδείας πέδῳ,
ὁ τεκνοραίστης, λυμεὼν ἐμῆς πάτρας
Woe! luckless town, my nurse, already once 
Fired by the foemen from the fleet of one
Begotten in three nights, that lion whom
The jaws of Triton’s sharp-fanged hound consumed:
Living he carved its vitals, but, being burnt 
By steam from cauldron on a fireless hearth,
Dropped to the ground the bristles from his head,
That child-destroyer, ruin of my land.
Pseudo-Lycophron (2nd century BCE). Alexandra, lines 31–38. Translated by George W. Mooney (1921). London: G. Bell.
This poem takes the form of a prophecy spoken by Cassandra of Troy, and so the contents are, in the usual manner of prophecies, somewhat riddling and obscure. However, the 12th century Byzantine scholars John and Isaac Tzetzes gathered interpretations of the difficult lines, which subsequent writers have followed. (Since I can’t read the Tzetzes’ Greek, I’m using John Potter’s Latin version of the commentaries.)
“luckless town, my nurse” — Troy, in which Cassandra, daughter of Priam and Hecuba, was born and raised.
“fired by the foemen from the fleet of one” — Potter comments:
Cassandra, who is going to expound the various miseries and calamities of Troy, begins with a passage about the expedition of Heracles, who, having raised a force of six ships, sacked and plundered Troy under King Laomedon.
John Potter (1697). Commentarius in Lycophronis Cassandram, pp. 1384–1385. In Christian Gottfried Müller, ed. (1811). ΣΧΟΛΙΑ ΕΙΣ ΛΥΚΟΦΡΟΝΑ, volume 3. Leipzig: Vogel. My translation.
and refers us to this speech by Tlepolemus in the Iliad:
Far other was Herakles, my own brave and lion-hearted father, who came here for the horses of Laomedon, and though he had six ships only, and few men to follow him, sacked the city of Ilion and made a wilderness of her highways.
Homer. Iliad V.638–642. Translated by Samuel Butler (1898). London: Longmans, Green and Co.
“begotten in three nights” — that is, Heracles spent three nights inside the sea-monster. I am not sure why Mooney chose “begotten” here (other than for reasons of prosody): the Greek is τριεσπέρου, “in three successive nights”.
“that lion” — Heracles, from the skin he wore after killing the Nemean Lion.
“Triton’s sharp-fanged hound” — the sea monster sent by Poseidon after Laomedon refused to pay him for building the walls of Troy. Triton was the son of Poseidon.
“cauldron on a fireless hearth” — Potter comments:
that is, the belly of the sea monster, whose natural heat, neither kindled nor increased by external fire, boiled away Heracles’ hair
Potter, pp. 1385–1386.
- “child-destroyer” — Heracles killed his children by Megara in a fit of madness brought on by Hera.
Christian commenters have generally assumed that pseudo-Lycophron got the story from Jonah, but their arguments are not very convincing, for example:
There is a fable, most probably of Phoenician origin, which, bearing some similitude to the history of Jonah, may have been taken from this book. Laomedon, king of Troy, having displeased Neptune, to appease him, was required to expose his daughter Hesione to be devoured by a sea-monster. She was chained to a rock, and was awaiting her fate at the next flux of the tide. In the interim Hercules slew the sea-monster, and delivered the princess. To this Lycophron, in his Cassandra, ver. 33, &c., is supposed to allude:—
τριεσπερου λεοντος, ον ποτε γναθοις
Τριτωνος ημαλαψε καρχαρος κυων.
“Of the lion the offspring of three nights, which the fierce dog of Triton swallowed down greedily.”
The scholiasts explain this in the following manner: While the princess was standing chained to the rock, expecting the greedy dog (καρχαρος κυων, the shark) to come and devour her, Hercules stood by ready armed; and, when the monster came forward with open mouth, he jumped directly down his throat, and spent three days in cutting and hacking his entrails; and afterwards came out of the monster, with the loss of all the hair on his bead. Cyril, in his comment, says this was occasioned by the incredible heat of the monster’s stomach.
This fable might have been easily taken from the true history; though some have been ready enough to intimate that the history of the prophet was taken firom the fable.
The appeal made to the main facts of this history by our Lord, proves that we are to admit of no allegorical exposition of these facts. 1. There was such a person as Jonah. 2. He was swallowed by a sea-monster, in whose belly he was miraculously preserved three days and three nights. 3. This same prophet preached to the Ninevites; and they repented and turned from their sins, under his ministry. This testimony puts an end to all mythological, allegorical, and hypothetical interpretations of those great facts. And in its literal sense alone, I undertake the interpretation of this book.
Adam Clarke (1836). ‘Introduction to the Book of Jonah’. In The Holy Bible, Containing the Old and New Testaments, volume 4, p. 3338. London: Thomas Tegg.
When Pseudo-Lycophron comes to the myth of Perseus and Andromeda, it is very similar to his version of the myth of Heracles and Hesione. I guess the story was too good not to use again.
δισσάς τε πέτρας, κέπφος αἷς προσήλατο 
δαιτὸς χατίζων. ἀντὶ θηλείας δ᾽ ἔβη
τὸν χρυσόπατρον μόρφνον ἁρπάσας γνάθοις,
τὸν ἡπατουργὸν ἄρσεν᾽ ἀρβυλόπτερον.
πεφήσεται δὲ τοῦ θεριστῆρος ξυρῷ 
φάλαινα δυσμίσητος ἐξινωμένη
And those two rocks, on which that stupid gull 
Leaped, seeking food, when with his jaws he snatched
No maiden, but the eagle golden-sired,
The liver-rending hero with winged shoes.
The loathed sea-monster, all its sinews cut, 
Slain by that reaper’s razor then shall be
Pseudo-Lycophron, lines 836–841.
- “stupid gull” — the Greek is κέπφος meaning “storm petrel” or (figuratively) “simpleton”. Mooney suggests this puns on κῆτος meaning “sea-monster”.
- “the eagle golden-sired” — Perseus, who flew on the winged sandals of Hermes, and was fathered by Zeus when he visited Danaë in a shower of gold.