'Delphi and FireMonkey WebBrowser Callback using file://

I am using 10.2.2 Tokyo Enterprise and FireMonkey's TWebBrowser in an App. I wish to receive a callback response, but I don't receive a URL back when the ShouldStartLoadWithRequest fires when I run my code.

I would like to know if this is possible without writing a wrapper. I feel I have missed something simple.

I have checked these sites and many more and have tried many different ways...

Callback from Firemonkey WebBrowser JavaScript code

Adding Javascript processing capability to TWebBrowser in iOS

Callback Delphi function from TWebBrowser by javascript on Delphi XE6 for all platforms (including iOS, ANDROID)?

Here is my simple code version:

index.html

<!DOCTYPE html><html lang="en">
<body>
<h2> Get Callback</h2>
</body>
</html>
procedure TForm2.FormCreate(Sender: TObject);
begin
  WebBrowser1.URL := 'file://' + GetCurrentDir +  '/../../index.html';
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  js : string;
begin
  js := ' var url = "file://' + GetCurrentDir +  '/../../index.html";'+
        'var encodedurl = encodeURIComponent(url);'+
        'window.location.href = encodedurl+"?67";';
  WebBrowser1.EvaluateJavaScript(js);
end;

procedure TForm2.WebBrowser1ShouldStartLoadWithRequest(ASender: TObject;
  const URL: string);
var
  js,TheURL: String;
begin
  TheURL := 'file://' + GetCurrentDir +  '/../../index.html';
  js := URL;
  Fetch(js, TheURL+'?',true,false);  //remove the URL and ?
  js := TIdURI.URLDecode(js, IndyTextEncoding_UTF8);
  Memo1.Lines.Text := js;
end;

Changing the end of the URL should, in principle, trick the WebBrowser into believing that it is a new URL, and I should be able to extract the number 67 while still keeping the page refreshed.

Please can you point me in the right direction?



Solution 1:[1]

I continued to investigate further ...

Normally when sending a Header to a Server, the Server accepts the Header but in the above instance the Header is sent from a local URL to a local URL, the browser will picked this up and flag the Header as ill- formed. That is because CORS policy steps in.

The same-origin request policy can be disabled in Chrome using the --disable-web-security flag. But as I am using the standard Firemonkey Web-browser, the Chrome solution is not fully applicable.

However it seems that the changeable security/zone settings of IE 11 and Edge are completely removed. The only way in which you can access this is via a wrapper.

Refer to Can the same-origin request policy be disabled in Microsoft Edge?

Hopefully Embarcadero will add the callback function into the Firemonkey Web browser sometime soon. So in the interim I discovered an easy workaround. I downloaded ScriptGate 1.0 from the Getit Package Manager and used their wrapper library. Although it is not the real solution to my question, it is a solution for others who are looking for a quick fix.

Here is the callback function that I used to make it happen…

uses
SG.ScriptGate,...
 var
    FScriptGate: TScriptGate;

procedure TForm2.FormCreate(Sender: TObject);
begin
  WebBrowser1.URL := 'file://' + GetCurrentDir +  '/../../index.html';
  FScriptGate := TScriptGate.Create(Self, WebBrowser1, 'YourOrgScheme');
  end;

procedure TForm2.FormShow(Sender: TObject);
begin
//use a timer (set to 200) to call evalfunction if URL not loaded in time 
  evalfunction;
end;

procedure TForm2.Button1Click(Sender: TObject);//callback function
begin
    FScriptGate.CallScript(
    'getvalue()',   // calls the javasacript getvalue function
    procedure(const iResult: String)
    begin
      ShowMessage(iResult); //result will be 67        
  );
end;

procedure TForm2.evalfunction; //populates browser onFormshow
var
 js:string;
begin
    js :=
       'function getvalue(){'+
      ' return "67";'+
       '}';
    WebBrowser1.EvaluateJavaScript(js);
   end; 

Solution 2:[2]

Delphi 10.4.1 iOS ScriptGate Error

https://bitbucket.org/freeonterminate/scriptgate/issues/14/delphi-1041-ios-scriptgate-error

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jonah Zheng