Recent

Author Topic: TGeckoBrowser's GetElementsByTagName func.  (Read 42009 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #15 on: December 02, 2011, 05:37:37 pm »
... Is there something you want to do in particular that can't be done with the current implementation?
To normal work with some multy-frame (<frameset>) site, I need to clear content inside the second <frame> -tag.
Following example will clear the RECIPES frame in the page http://www.htmlcodetutorial.com/frames/nestedfs.html
Code: [Select]
procedure TForm1.cleanframe;
var s,s2:IInterfacedString;
  NL:nsIDOMNodeList;
  NNM:nsIDOMNamedNodeMap;
  N:nsIDOMNode;
  i,j:integer;
  sName:string;
begin
  s:=newstring('frame');
  s2:=newstring;
  NL:=GeckoBrowser1.ContentWindow.GetDocument().GetElementsByTagName(s.AString);
  i:=NL.GetLength();
  while i>0 do
    begin
    NNM:=NL.Item(i-1).GetAttributes();
    j:=NNM.GetLength();
    while j>0 do
      begin
      NNM.Item(j-1).GetNodeName(s2.AString);
      sName:=s2.ToString;
      if sName='name' then
        begin
        NNM.Item(j-1).GetNodeValue(s2.AString);
        sName:=s2.ToString;
        if sName='RECIPES' then
          begin
          N:=NL.Item(i-1);
          NL.Item(i-1).ParentNode.RemoveChild(N);
          exit;
          end;
        end;
      j:=j-1;
      end;
    i:=i-1;
    end;
end;

Thank you a lot , but the reason why I want clear frame , is the content in frame loads my CPU to 100%. You code first load web-page in normal mode, so my CPU is knock-downed.


Also I would like to know can I click the button (input -submit) in logon form on http://wiki.freepascal.org/index.php?title=Special:Userlogin&returnto=Main_Page
by lazarus' gecko?
« Last Edit: December 02, 2011, 05:50:29 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #16 on: December 02, 2011, 07:32:19 pm »
Quote
Thank you a lot , but the reason why I want clear frame , is the content in frame loads my CPU to 100%. You code first load web-page in normal mode, so my CPU is knock-downed.]Thank you a lot , but the reason why I want clear frame , is the content in frame loads my CPU to 100%. You code first load web-page in normal mode, so my CPU is knock-downed.
I don't see a solution for that with this Gecko Port. There is the nsIContentPolicy interface that allows to block scripts, images, etc. or nsIDocShell.allowJavascript but they aren't implemented. I assume a script is loading the CPU.

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #17 on: December 02, 2011, 08:40:37 pm »
Quote
Also I would like to know can I click the button (input -submit) in logon form on http://wiki.freepascal.org/index.php?title=Special:Userlogin&returnto=Main_Page
by lazarus' gecko?
That's a short one  ;)
Code: [Select]
  s:=newstring('form');
  (GeckoBrowser1.ContentDocument.GetElementsByTagName(s.AString).Item(1) as nsIDOMHTMLFormElement).Submit;
I omitted searching for the correct form, checking if the number of items, etc. I believe you got the basics from previous examples.
The "trick" here is the "as nsIDOMHTMLFormElement". Node elements can be anything and are all descendants of nsIDOMElement. Here we now it is a form since we did a GetElementsByTagName("form") and can "cast" it to a nsIDOMHTMLFormElement.
So if we take my previous login example it can be rewritten as follows:
Code: [Select]
procedure TForm1.Login;
var s,s2:IInterfacedString;
  NE:nsIDOMElement;
begin
  s:=newstring('wpName1');
  NE:=GeckoBrowser1.ContentDocument.GetElementById(s.AString);
  if assigned(NE) then
    begin
    s:=newstring('XPCOM');
    (NE as nsIDOMHTMLInputElement).SetValue(s.AString);
    (NE as nsIDOMHTMLInputElement).GetForm().Submit;
    end;
end;
Casting the input element to nsIDOMHTMLInputElement allows to call its GetForm method that returns the form containing that input and submit it directly.



anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #18 on: December 03, 2011, 08:20:31 am »

I don't see a solution for that with this Gecko Port.
...
In disassembler I have found a procedure, which receive a data:
Code: [Select]
02E31577  |.  FF15 A0914203 CALL DWORD PTR DS:[<&nspr4.PR_Read>]     ;  nspr4.PR_Read
PR_Read is declared in  nspr4.dll
02E31577 is an address in xul.dll



Where can I find source of nspr4.dll?
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #19 on: December 03, 2011, 09:31:51 am »
Quote
Where can I find source of nspr4.dll?
nspr4.dll is the Netscape Portable Runtime. That is like the RTL for fpc. Don't know what you hope to find there but the code can be downloaded here: https://ftp.mozilla.org/pub/mozilla.org/nspr/releases/. Doc for PR_Read here: http://www.mozilla.org/projects/nspr/reference/html/priofnc.html#17910.
If you want to go that route, on "hacking" sites you'll find some code that hooks the PR_Read/PR_Write functions and sniffs/modifies the data between browser and server. I can even share some pascal code I wrote to hook dll functions (memory management functions in my case  :D ).

Another approach that I haven't tried but is much easier to test is to use the OnStatusChange of TGeckoBrowser. Frames are loaded individually and you'll get a 'loading from xxx' event for the main page and every frame loaded. You could try to modify the page after the first 'loading from xxx' event in the hope it will stop loading the frame that is causing a problem.




ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #20 on: December 03, 2011, 09:57:58 am »
Quote
Another approach that I haven't tried ...

This works for me with http://www.htmlcodetutorial.com/frames/nestedfs.html:

Code: [Select]
var btransferring:boolean;
    bcleaningdone:boolean;
procedure TForm1.GeckoBrowser1StatusChange(Sender: TObject; aMessage: WideString
  );
begin
  if pos('transferring',LowerCase(aMessage))>0 then
    btransferring:=true   // first transfer
  else
    if btransferring and not bcleaningdone then  //just after first transfer
      begin
      cleanframe;
      bcleaningdone:=true;
      end;
end;

Set btransferring and bcleaningdone to false before loaduri.
The exact status message is 'Transferring data from xxx'

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #21 on: December 03, 2011, 10:17:35 am »
A cleaner way to remove a frame without messing up the page layout is to set its source to empty. So instead of removing the frame with
Code: [Select]
          N:=NL.Item(i-1);
          NL.Item(i-1).ParentNode.RemoveChild(N);
you can do
Code: [Select]
          s:=newstring('');
          (NL.Item(i-1) as nsIDOMHTMLFrameElement).SetSrc(s.AString);

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #22 on: December 03, 2011, 10:36:53 am »
A cleaner way to remove a frame without messing up the page layout is to set its source to empty. So instead of removing the frame with
Code: [Select]
          N:=NL.Item(i-1);
          NL.Item(i-1).ParentNode.RemoveChild(N);
you can do
Code: [Select]
          s:=newstring('');
          (NL.Item(i-1) as nsIDOMHTMLFrameElement).SetSrc(s.AString);

My <frame> has no attribute 'name', it has only one attribute - SRC . What I must do? SCR always different every page reloading :(

Well I recode cleanframe procedure as next:

procedure TForm1.cleanframe;
var s,s2:IInterfacedString;
  NL:nsIDOMNodeList;
  NNM:nsIDOMNamedNodeMap;
  N:nsIDOMNode;
  i,j:integer;
  sName:string;
begin
  s:=newstring('frame');
  s2:=newstring;
  NL:=GeckoBrowser1.ContentWindow.GetDocument().GetElementsByTagName(s.AString);
  i:=NL.GetLength();

          s:=newstring('');
          (NL.Item(i-1) as nsIDOMHTMLFrameElement).SetSrc(s.AString);


end; 

It helps ^_^
« Last Edit: December 03, 2011, 10:59:32 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #23 on: December 03, 2011, 10:54:20 am »
Quote
My <frame> has no attribute 'name', it has only one attribute - SRC . What I must do?
Test the value of attribute 'src'  ;)
If I understood you correctly, you would like to intercept one particular frame on one particular page. Why not hard-code the frame number?
Code: [Select]
(NL.Item(TheIndexOfTheNastyFrame) as nsIDOMHTMLFrameElement).SetSrc(s.AString);

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #24 on: December 03, 2011, 11:21:34 am »

If I understood you correctly, you would like to intercept one particular frame on one particular page. Why not hard-code the frame number?
Yup! As a girl i am not always so clever. Thanks (^3^)
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #25 on: December 03, 2011, 11:29:32 am »
When my lazarus app make get request in my sniffer i see :

- Http: Request, GET /index.php, Query:title=Special:Userlogin&returnto=Main_Page
    Command: GET
  - URI: /index.php?title=Special:Userlogin&returnto=Main_Page
     Location: /index.php
   - Parameters: 0x1
      title: Special:Userlogin
      returnto: Main_Page
    ProtocolVersion: HTTP/1.1
    Host:  wiki.freepascal.org
    UserAgent:  Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.19) Gecko
    Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language:  en-us,en;q=0.5
    Accept-Encoding:  gzip,deflate
    Accept-Charset:  ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive:  300
    Connection:  keep-alive
    HeaderEnd: CRLF



How to  substitute UserAgent . I want that site could not identify me .

And one more question. Does current gecko can work throw proxy?
« Last Edit: December 03, 2011, 11:36:21 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #26 on: December 03, 2011, 12:23:42 pm »
Quote
How to  substitute UserAgent . I want that site could not identify me .
Add the following line to xulrunner\greprefs\all.js
Code: [Select]
pref("general.useragent.override", "My Browser");
Quote
And one more question. Does current gecko can work throw proxy?
Yes. If standard settings don't work you can change them in the file above. Just search for 'proxy'.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #27 on: December 03, 2011, 12:29:20 pm »

Yes. If standard settings don't work ...
What do you mean about "standard settings"? I don't find in TGeckoBrowser any  suitable property for proxy specifying.

Quote
Add the following line to xulrunner\greprefs\all.js
Will it work if GeckoBrowser1.DisableJavaScript = true ?
**************************

If a web-page opens a link in new window , my gecko generates error http://img59.imageshack.us/img59/3509/96134173.png
How to prevent this?

**************************
I faced another  challenge. If I have 2 GeckoBrowser (GeckoBrowser1 and GeckoBrowser2) and if I logon on some site in GeckoBrowser1, then by opening same site in GeckoBrowser2 i have already logined in. How to make different cookie  for each GeckoBrowser object?
« Last Edit: December 03, 2011, 12:50:14 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #28 on: December 03, 2011, 01:07:37 pm »
Quote
What do you mean about "standard settings"?
Standard settings depends on your gecko version (ie. no proxy or use system settings). If you want to change the proxy setting you can do that by changing "network.proxy.type" to one of the values found here http://kb.mozillazine.org/Network.proxy.type and, if manual proxy, set the values "network.proxy.http" and "network.proxy.http_port" or whatever protocol setting you need. Search for "network.proxy.type" in all.js and you'll find all network.proxy settings grouped together.
Quote
I don't find in TGeckoBrowser any  suitable property for proxy specifying.
xulrunner\greprefs\all.js stores the preferences for the gecko engine. They are not configurable with TGeckoBrowser. If you are using firefox and want to use the same network config, open about:config and filter on network or network.proxy and you'll find all the relevant settings that you can copy in all.js.
Quote
Will it work if GeckoBrowser1.DisableJavaScript = true ?
Yes. Settings are loaded when the object is created.




anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #29 on: December 03, 2011, 01:51:45 pm »
hard-code the frame number?
Code: [Select]
(NL.Item(TheIndexOfTheNastyFrame) as nsIDOMHTMLFrameElement).SetSrc(s.AString);
Does not work in page: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols
I see all frames no matter what nsIDOMHTMLFrameElement is specified
(T_T)
WinXP SP3 Pro Russian 32-bit (5.1.2600)

 

TinyPortal © 2005-2018