Recent

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

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #30 on: December 03, 2011, 02:38:12 pm »
Quote
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)
The site is using frames inside iframes. So you need to get the document that corresponds with the iframe first. Hardcoded solution:
Code: [Select]
  s:=newstring('iframe');
  NL:=GeckoBrowser1.ContentWindow.GetDocument().GetElementsByTagName(s.AString);
  s:=newstring(widestring('FRAME'));
  NL:=(NL.Item(1) as nsIDOMHTMLIFrameElement).ContentDocument.GetElementsByTagName(s.AString);
  s:=newstring('');
  (NL.Item(0) as nsIDOMHTMLFrameElement).SetSrc(s.AString);
The first iframe is the top window containing adds. The second iframe contains the frames.
Note that the GeckoBrowser.OnDocumentComplete will be triggered for the main document and every iframe. Iframes are independent documents. Make sure you have all documents ( all 3 ) completed before trying to access the iframe documents.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #31 on: December 03, 2011, 03:38:56 pm »

**************************

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 found ^_^



procedure TForm1.GeckoBrowser1NewWindow(Sender: TObject;
  aChromeFlags: Longword; var newWindow: TCustomGeckoBrowser);
begin

  newWindow:=geckobrowser2;
end; 


What about 2 different cookies sessions for geckoBrowser1 and GeckoBrowser2.
« Last Edit: December 03, 2011, 03:43:11 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #32 on: December 03, 2011, 04:29:35 pm »
Quote
What about 2 different cookies sessions for geckoBrowser1 and GeckoBrowser2.
Not sure what you mean. You want geckoBrowser2 to ignore cookies received by geckoBrowser1?

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #33 on: December 03, 2011, 04:43:29 pm »
Quote
What about 2 different cookies sessions for geckoBrowser1 and GeckoBrowser2.
Not sure what you mean. You want geckoBrowser2 to ignore cookies received by geckoBrowser1?
For example I have 2 google mail accounts. I want  to open in GeckoBrowser1 gmail.com and logon 1st account and open in GeckoBrowser2 gmail.com and logon 2nd account in same time.

Now when I open gmail.com in GeckoBrowser2 I see that I'm already  logined in with 1st account.

But if I click a link which (i think it is site dependent behaviour) creates new browser window, new window of course must keep old cookies.
« Last Edit: December 03, 2011, 04:47:27 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #34 on: December 03, 2011, 05:15:03 pm »
From what I can see, cookies are shared between GeckoBrowser1 and GeckoBrowser2 in the same process but separated when you run them from a different process. So if you run 2 processes you can use separate gmail accounts. Cookies are not permanent neither. Restart the app and you have to log in again. This is different from firefox that re-uses the same cookie store (a sqlite database) for all firefox instances.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #35 on: December 04, 2011, 04:33:43 am »
From what I can see, cookies are shared between GeckoBrowser1 and GeckoBrowser2 ...
But what purpose nsICookie  for?

Quote
...in the same process but separated when you run them from a different process. ...
Is it possible in single process? Mayby I must use threads?
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #36 on: December 04, 2011, 09:38:45 am »
Quote
But what purpose nsICookie  for?
That is the interface for the Cookie object. All properties are read only. nsICookieManager is what you need to manage the cookies. This allows to remove the cookies but not to restore them. nsICookieManager2 has an add method but that interface isn't defined in the Gecko port. Here is how you create a nsICookieManager and access/delete all cookies:
Code: [Select]
procedure TForm1.RemoveCookies;
var CM:nsICookieManager;
    C:nsICookie;
    E:nsISimpleEnumerator;
    chost:IInterfacedUTF8String;
    cname,cpath:IInterfacedCString;
begin
  NS_GetService(NS_ICOOKIEMANAGER_IID,CM,CM);
  E:=CM.GetEnumerator;
  chost:=NewUTF8String();
  cname:=NewCString();
  cpath:=NewCString();
  if E.HasMoreElements then
    begin
    C:=(E.GetNext as nsICookie);
    C.GetHost(chost.AUTF8String);
    C.GetName(cname.ACString);
    C.GetPath(cpath.ACString);
    CM.Remove(chost.AUTF8String,cname.ACString,cpath.ACString,false);
    end;
end;
This is just a demo how to access all cookies because the following would do the same but much shorter
Code: [Select]
procedure TForm1.RemoveCookies;
var CM:nsICookieManager;
begin
  NS_GetService(NS_ICOOKIEMANAGER_IID,CM,CM);
  CM.RemoveAll();
end;

Quote
Is it possible in single process? Mayby I must use threads?
There is only one Gecko Engine per processes. See GeckoInit.GeckoComponentsStartup. Using threads would still use the same engine.


anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #37 on: December 04, 2011, 10:44:28 am »
It grieves me to hear this info. Well then I shall start to develope a some kind of process manager...

Thanks so much.
« Last Edit: December 04, 2011, 10:47:07 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #38 on: December 05, 2011, 05:18:54 am »

Set btransferring and bcleaningdone to false before loaduri.
The exact status message is 'Transferring data from xxx'
I have some problem with it. Some times I load page not throw address bar, but throw clicking link on the current page , so loaduri is not used. Does TGeckoBrowser have some event for this (e.g. OnLoad)?

Can I use OnLocationCange for this?

********************************


Does TGeckoBrowser support user-styles? How to implement this mechanism? For example in Opera@USB 9.27 there is style file opera927en.zip\profile\styles\user\_bantiad.css which delete ads:
@charset "UTF-8";
/*
Name: Grafik Blocker
Version: 1
Beschreibung: Grafik Blocker
*/

[height="60"][width="468"], [height="60px"][width="468px"],
[height="31"][width="88"], [height="31px"][width="88px"],
[height="336"][width="280"], [height="336px"][width="280px"],
[height="300"][width="250"], [height="300px"][width="250px"],
[height="250"][width="250"], [height="250px"][width="250px"],
[height="400"][width="240"], [height="400px"][width="240px"],
[height="120"][width="240"], [height="120px"][width="240px"],
[height="60"][width="234"], [height="60px"][width="234px"],
[height="150"][width="180"], [height="150px"][width="180px"],
[height="600"][width="160"], [height="600px"][width="160px"],
[height="125"][width="125"], [height="125px"][width="125px"],
[height="600"][width="120"], [height="600px"][width="120px"],
[height="90"][width="120"], [height="90px"][width="120px"],
[height="60"][width="120"], [height="60px"][width="120px"]
{display: none !important;}
« Last Edit: December 05, 2011, 05:35:21 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #39 on: December 05, 2011, 09:17:53 am »
Quote
Can I use OnLocationCange for this?
Yes, or GeckoBrowser.OnDocumentBegin which is fired a little earlier. But be aware that both can be fired multiple times fe. when the page contains iframes or when the page is redirected to another page.

Quote
Does TGeckoBrowser support user-styles? How to implement this mechanism?
In xulrunner\defaults\profile\chrome you'll find userContent-example.css. Edit the file and safe as userContent.css.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #40 on: December 06, 2011, 05:41:53 am »
Quote from: ludob
Please can you help to understand, why frames do not cleared here:
http://itanfrorthroup.narod2.ru/k0.htm

I have traced code and:

procedure TForm1.cleanframe(sender:tgeckobrowser);
var s,s2:IInterfacedString;
  NL:nsIDOMNodeList;
  NNM:nsIDOMNamedNodeMap;
  N:nsIDOMNode;
  i,j:integer;
  sName:string;
begin
  s:=newstring('frame');
  s2:=newstring;
  NL:=sender.ContentWindow.GetDocument().GetElementsByTagName(s.AString);
  i:=NL.GetLength();
{when execution is stoped next line , then i = 0}
  while i>1 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;*)

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




narod.ru added some scripts, clean page has next html-code:
<html>

<frameset cols="25%,*,25%">
  <frame src="http://www.w3schools.com/tags/frame_a.htm" />
  <frame src="http://www.w3schools.com/tags/frame_b.htm" />
  <frame src="http://www.w3schools.com/tags/frame_c.htm" />
</frameset>

</html>

And if I open this page from my harddrive by address file://localhost/I:/html_test/k0.htm , then I see only FRAME A, but first StatusChange's message is 'Transferring data from www.w3schools.com…'. But as I understood for good working your trick, first message must be 'Transferring data from localhost isn't it?
« Last Edit: December 06, 2011, 06:10:21 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #41 on: December 06, 2011, 08:51:14 am »
Quote
Please can you help to understand, why frames do not cleared here:
http://itanfrorthroup.narod2.ru/k0.htm
The problem here is that after "Transferring data from itanfrorthroup.narod2.ru" you get a "Connecting to mc.yandex.ru". This is where it loads the script and the img. At this point the DOM hasn't been created yet. In this case you'll probably have to wait for the "Connecting to www.w3schools.com" message.

Quote
And if I open this page from my harddrive by address file://localhost/I:/html_test/k0.htm , then I see only FRAME A, but first StatusChange's message is 'Transferring data from www.w3schools.com…'. But as I understood for good working your trick, first message must be 'Transferring data from localhost isn't it?
Opening a file:// URI apparently doesn't trigger the 'Transferring data ' message.

You correctly quoted "trick". I suggested this hack to help you block a particular frame on a particular site. You'll need to tune this for every situation. There are far more complex situations where you get fe. client side re directions before getting the final page. So there will not be a universal solution for this type of problem.

Add a memo to your form and log all GeckoBrowser.StatusChange messages and GeckoBrowser.LocationChange URI's. This will make it easier to see what happens and decide what will perhaps be the best moment to intervene and modify the page.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #42 on: December 06, 2011, 02:59:59 pm »
...
 So there will not be a universal solution for this type of problem.
...
I agree with you. When I was hacking opera.dll, I patched library''s receive procedure. After receiving html raw data I  force library to change '<FRAME' on '<BR   '. I might inject something same string replacement, if nspr4.dll was written on pascal, but someone writes it on C++. C++ is absolutely uncompatible with pascal  (;_;)

... I suggested this hack to help you block a particular frame on a particular site.
...
Your method is working fine on that site , which load my CPU to 100%. I'm really grateful to you.
« Last Edit: December 06, 2011, 03:22:22 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #43 on: December 07, 2011, 06:37:46 pm »
How click a link ? Anchor element (nsIDOMHTMLAnchorElement) does not have Click procedure...

This anchor is actually javascript long line, so it's impossible to extract href and loaduri(href). I need exactly  human ckick (but programmatically).
« Last Edit: December 07, 2011, 06:44:11 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TGeckoBrowser's GetElementsByTagName func.
« Reply #44 on: December 07, 2011, 08:47:22 pm »
Quote
How click a link ?
The difficult stuff starts  ;)

The following code simulates a mouse click on the "login" link on http://lazarus.freepascal.org/
Code: [Select]
var
  s,s2:IInterfacedString;
  EVT:nsIDOMEvent;
  DV:nsIDOMAbstractView;
begin
//create a mouse event
  s2:=newstring('MouseEvents');
  EVT:=(GeckoBrowser1.ContentDocument as nsIDOMDocumentEvent).CreateEvent(s2.AString);
//get window AbstractView
  DV:=(GeckoBrowser1.ContentDocument as nsIDOMDocumentView).DefaultView;
//make the event a 'click' event
  s2:=newstring('click');
  (EVT as nsIDOMMouseEvent).InitMouseEvent(s2.AString,true,false,DV,0,0,0,0,0,false, false, false, false, 0, GeckoBrowser1.ContentDocument as nsIDOMEventTarget );
//look for <li id="button_login">, our target <a> is 2nd child, cast to event target and dispatch event
  s:=newstring(widestring('button_login'));
  (GeckoBrowser1.ContentWindow.GetDocument().GetElementById(s.AString).GetChildNodes.Item(1) as nsIDOMEventTarget).DispatchEvent(EVT);
end;
The code hard codes a lot but you'll get the idea behind it.
I didn't find a way to pas a null XPCOM interface. The last parameter of InitMouseEvent is a secondary event target and not used for 'click'. So normally I should have passed null but fpc doesn't know how to cast a null (variant) to a nsIDOMEventTarget. I simply passed the document as secondary target and it works fine.

The <a> on this page doesn't contain javascript but since the click event is triggered explicitly, I would be surprised if an onclick wouldn't be triggered.

 

TinyPortal © 2005-2018