I am loading an local disk drive _test.htm
file through IPersistMoniker
Load
method. From what I believe, it is supposed to add the path to the relative URLs as base path. Problem is - it does not do so. Instead, it takes a very long time trying to resolve the path from Internet until it gives up (about 20-30 seconds). What I want is to give up instantly, as soon as the unsolvable path is detected (since it is a local disk file anyway).
This is an example HTML I am loading:
<html>
<head>
<script src="//test/test.js"></script>
<head>
<body>
<img src="image.jpg">
<img src="/image.jpg">
<img src="//image.jpg">
</body>
</html>
Simplified code (C++ Builder) with no error checking:
WideString URL = "file:///" + StringReplace(ExtractFilePath(Application->ExeName), "\\", "/", TReplaceFlags() << rfReplaceAll) + "_test.htm";
TCppWebBrowser* WB = CppWebBrowser1;
DelphiInterface<IMoniker> pMoniker;
OleCheck(CreateURLMonikerEx(NULL, URL.c_bstr(), &pMoniker, URL_MK_UNIFORM));
DelphiInterface<IHTMLDocument2> diDoc2 = WB->Document;
DelphiInterface<IPersistMoniker> pPrstMnkr;
OleCheck(diDoc2->QueryInterface(IID_IPersistMoniker, (LPVOID*)&pPrstMnkr));
DelphiInterface<IBindCtx> pBCtx;
OleCheck(CreateBindCtx(0, &pBCtx));
pPrstMnkr->Load(0, pMoniker, pBCtx, STGM_READWRITE);
Problem - image.jpg
loads fine, but the paths //test/test.js
and /image.jpg
and //image.jpg
take a very long time to resolve/load. From what I understand CreateURLMonikerEx
is supposed to use file:///path/to/executable/
and prepend that automatically to these paths in which case they would fail instantly - file:///path/to/executable//test/test.js
for example. That does not happen.
I additionally tried to move image.jpg
to a subfolder and then create custom IMoniker
interface with the GetDisplayName
and BindToStorage
implementation which loaded the image from a custom path. However it doesn't do the same for paths which start with //
or /
. Even though I output file:///path/to/executable/
in the GetDisplayName
through the *ppszDisplayName
parameter.
How can I avoid extended time loading such unusable links (discard them), or redirect them to local path as above?
I found a partial solution to use about:blank
in the *ppszDisplayName
but then it doesn't load images with the valid path image.jpg
as then it loads them as about:image.jpg
which again is invalid path.
Additionally - I've tried adding IDocHostUIHandler
interface with the implementation of Invoke
method (DISPID_AMBIENT_DLCONTROL
) with the pVarResult->lVal = DLCTL_NO_SCRIPTS | DLCTL_NO_JAVA | DLCTL_NO_RUNACTIVEXCTLS | DLCTL_NO_DLACTIVEXCTLS | DLCTL_NO_FRAMEDOWNLOAD | DLCTL_FORCEOFFLINE;
- it it blocks the download of images entirely, but still does check 20-30 seconds for the links starting with //
or /
.