Hello!

I try, I torture TWebBrowser so that he gives me an innerText at the link when I click.

Naryl is the only option that combines JavaScript, which changes the status text, and then event:onStatusTextChange gives us the actual text.

Those. I have javascript:

 function statusChange(text) { window.status = text; } 

In the program it looks like:

 procedure TMain.webBrowserStatusTextChange(Sender: TObject; const Text: WideString); var t: String; begin if pos('CMD:', Text) > 0 then begin t := Text; delete(t, 1, 4); reMessage.Text := t; end; end; 

And here is the link itself:

 <a href="#" onclick="javascript:statusChange(''CMD:'+Text+'''); return false;">Текст</a> 

But it is impossible to do this, as the empty string is constantly returned or about:blank# . What am I doing wrong? Is it possible to do this in a different way?

UPD

The link is taken out of the program context, so the number of quotes seems strange to you.

 Link := '<a href="#" onclick="javascript:statusChange(''CMD:'+Text+'''); return false;">Текст</a>' 

Double quote in Delphi - replaces character escaping. And for the same reason (my mistake in the question) you did not understand what Text for it is a program variable, not a script. In fact, after the link will be:

 <a href="#" onclick="javascript:statusChange('CMD:Текст'); return false;">Текст</a> 

Those. as you wrote, you can replace innerText . But this is not the problem, it works great, but the statuses returned via the TWebBrowser event always return the text when pressed: about:blank# .

UPD2

 procedure TMain.webBrowserBeforeNavigate2(Sender: TObject; const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool); var p: integer; begin p := pos('CMD:', URL); if p > 0 then ShowMessage(copy(URL, p+4, Length(URL)-p-3)); end; procedure TMain.Button1Click(Sender: TObject); begin WebBrowser.Navigate('http://sh4dow.jino.ru/test.html'); end; 

At the exit of your page with the program and a single pop-up message.

  • Uh, are you sure that window.status is the same as TWebBrowser.statusText? o_O In general, I'll try to solve it differently) I just tested it and it gave me all the states of the browser (connection, data reading, etc.), but not the property of the window object. - Sh4dow
  • Yes, it can not be)) Clicked on the links? + What Delphi version? I have 7. - Sh4dow
  • Already better) Well, then we must look for a fundamental difference between the links, logically. By the way, do you have a full document? The doctype, the encoding is indicated, the tags (html, head, body) are in place? Because of this, periodically mysticism occurs. - Sh4dow
  • But I did not think about this mysticism. We will fix the doctype - Dex
  • one
    If you dynamically add JS, keep in mind that in IE (and in its library Delphi) it doesn’t do it that way =) That is, you can make an html file in the resources or in the program folder, which will contain all JS add something dynamically. I've just achieved your result (transition and text blank# ), the function delphiRequest was not defined. I think your solution is somewhere here) Unfortunately, I don’t know the Component. - Sh4dow

1 answer 1

In general, I will take it on faith that changing window.status will cause webBrowserStatusTextChange (there is no time to pote) that I saw right away:

onclick in html is processed like this:

 <a onclick="lalala();ololo();"></a> // эквивалентно: a.onclick = function(){ lalala();ololo(); } 

, i.e. javascript: prefix javascript: - error. It is usually used in the form href="javascript:lalala();" i.e. to transfer the code to the address bar with the subsequent transition-execution.

Another variable Text you have not defined, judging by the formulation of the problem, you need the innerHTML property (in this case, "Text") of the current link this.

That is, it is necessary, as indicated in the following:

 <a href="#" onclick="statusChange('CMD:'+this.innerHTML);return false;">Текст</a> 

either

 <a href="javascript:statusChange('CMD:'+this.innerHTML);">Текст</a> 

Further, quotes, yes) perhaps, the function is simply not executed (and judging by the lattice in the resulting string - the link is followed and return false; does not work).

Next, just a little bit of optimization.

 reMessage.Text := copy(Text, 5, Length(Text)-4); 

If after all this does not work - write observations.

UPDATE Change the approach:

Js:

 function delphiRequest(text) { window.location = '#CMD:'+text; return false; } 

link:

 <a onclick="return delphiRequest(this.innerHTML);">Текст</a> 

Delphi:

 procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject; const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool); var p: integer; begin p := pos('CMD:', URL); if p > 0 then ShowMessage(copy(URL, p+4, Length(URL)-p-3)); end; 
  • Added a comment to the question. Regarding your method: reMessage.Text: = copy (Text, 5, Length (Text) -4); You are undoubtedly right, but these are trifles that I have so far decided not to dwell on. The fact is that I don’t know free components that could: highlight individual words, highlight links and insert images, so I chose TWebBrowser as a temporary solution. - Dex
  • I also tried it, for some reason, if Cancel := false transition is made anyway. Now I have done everything with the same JavaScript functions as before (with minor changes), but I change the title , and accordingly I process onTitleChange it seems, and everything works. But these rakes terribly I do not like. - Dex
  • Cancel := false what are you talking about?) So I inserted my piece in the answer, it’s 100% working right) You copied everything exactly as in the answer? And the lattice is not forgotten before the CMD? ZY: Change of the status - exactly the same rake. I'm just trying to do the least noticeable) - Sh4dow
  • And by the way, @Dex, thanks for the question, Delphi-> JS bridge, I would have been going to do another year without it =) - Sh4dow
  • Goes to a new page with the text: return delphiRequest (this.innerHTML); - Dex