Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

save web as mht format file

Post Reply
coater
Posts: 135
Joined: Sat Sep 29, 2018 1:51 pm

save web as mht format file

Post by coater »

IS this the only way?
1.save html first(sometimes picture only appear after browsing )
2.change html to mht.

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  Lobjimsg   : OleVariant;
  Lobjiconf  : OleVariant;
  LobjStream : OleVariant;
begin
  Lobjimsg   := CreateOleObject('CDO.Message');
  Lobjiconf  := CreateOleObject('CDO.Configuration');
  LobjStream := CreateOleObject('ADODB.Stream');

  try
    LObjimsg.Configuration := Lobjiconf;  //
    Lobjimsg.CreateMHTMLBody('C:\temp\1.html', 0, '', '');  //0: cdoSuppressNone  do not use  cdoSuppressAll  pictures will not be included.
    LobjStream := Lobjimsg.GetStream;
    LobjStream.SaveToFile('C:\temp\fileName.mht', 2);
  finally
    Lobjimsg   := Unassigned;
    Lobjiconf  := Unassigned;
    LobjStream := Unassigned;
  end;
end;
ref:http://mrxray.on.coocan.jp/Delphi/plSamples/764_SaveWebPage_MHT.htm

first: import component new lib C:\Windows\System32\cdosys.dll
uses CDO_TLB, ADODB_TLB, ComObj

Is there another easy way to do this?
coater
Posts: 135
Joined: Sat Sep 29, 2018 1:51 pm

Re: save web as mht format file

Post by coater »

from selenium import webdriver driver = webdriver.Chrome()
driver.get(‘https://www.bing.com/‘)
res = driver.execute_cdp_cmd(‘Page.captureSnapshot‘, {})
with open(‘q.mhtml‘, ‘w‘) as f: f.write(res[‘data‘]) driver.quit()
------------
Is it(like .execute_cdp_cmd) possible to be used in cef4?
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: save web as mht format file

Post by salvadordf »

Please, download CEF4Delphi from GitHub again.

I just added a new menu option to the MiniBrowser demo that shows how to save the current page in MHTML format using the DevTools method you gave.

Thanks for the method! :D
I didn't know it existed.
coater
Posts: 135
Joined: Sat Sep 29, 2018 1:51 pm

Re: save web as mht format file

Post by coater »

:D
Nice news!
Thank you very much!
coater
Posts: 135
Joined: Sat Sep 29, 2018 1:51 pm

Re: save web as mht format file

Post by coater »

marks my learning notes
1.when we click saveasmhtml, run Chromium1.ExecuteDevToolsMethod. (see the SaveasMHTML1Click procedure.)
2.after executing, OnDevToolsMethodResult of Chromium1 will be trigered, and we send PostMessage named MINIBROWSER_DTDATA_AVLBL and data was stored with FDevToolsMsgValue. see procedure TMiniBrowserFrm.Chromium1DevToolsMethodResult.
3.deal with MINIBROWSER_DTDATA_AVLBL Message. In this procedure to save file. see procedure DevToolsDataAvailableMsg(var aMessage : TMessage); message MINIBROWSER_DTDATA_AVLBL

4.definition(all global):
1)We must define FDevToolsMsgValue to store result when result data was available(TMiniBrowserFrm.Chromium1DevToolsMethodResult) and used in the DevToolsDataAvailableMsg procedure.

2)we must know DevToolsMsgID to operate correspond command. So we must define MsgIDs FDevToolsMsgID and FMHTMLMsgID.
FormCreate, set FDevToolsMsgID := 0;
when clicking save mhtml menu: inc(FDevToolsMsgID); set FMHTMLMsgID := FDevToolsMsgID.



5.another:
screenshot,only replace FMHTMLMsgID with FScreenshotMsgID

https://chromedevtools.github.io/devtools-protocol/tot/Page/
Post Reply