Page 1 of 1

save web as mht format file

Posted: Fri May 07, 2021 2:58 am
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?

Re: save web as mht format file

Posted: Fri May 07, 2021 7:58 am
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?

Re: save web as mht format file

Posted: Fri May 07, 2021 2:31 pm
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.

Re: save web as mht format file

Posted: Sat May 08, 2021 12:12 am
by coater
:D
Nice news!
Thank you very much!

Re: save web as mht format file

Posted: Sat May 08, 2021 1:31 am
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/