Kirix Support Forums

How to intercept clicks on A tag?

Please post all general questions, comments, bug reports, and any other wxWebConnect feedback here.

How to intercept clicks on A tag?

Postby T-Rex on Thu Aug 20, 2009 9:09 am

Hello.

I'm trying to modify wxWebConnect sample application so that if user clicks on link with property target="_blank", this link is opened in wxAuiNotebook tab. So I have a few questions regarding this point:

  • Which event should I use to intercept click on A tag?
  • How can I determine that target attribute has value _blank?
  • Is there any other way to control navigation or if link opens in new window, get that new control and place to notebook?

Thanks in advance.
[url=http://wxwidgets.info]wxwidgets.info - Articles and Tutorials
wxToolBox - wxWidgets components that works like VisualStudio ToolBox
T-Rex
Registered User
 
Posts: 10
Joined: Sun Dec 25, 2005 6:57 am
Location: Ukraine

Re: How to intercept clicks on A tag?

Postby T-Rex on Thu Aug 20, 2009 10:14 am

Thanks to everyone. I solved the problem. :D
[url=http://wxwidgets.info]wxwidgets.info - Articles and Tutorials
wxToolBox - wxWidgets components that works like VisualStudio ToolBox
T-Rex
Registered User
 
Posts: 10
Joined: Sun Dec 25, 2005 6:57 am
Location: Ukraine

Re: How to intercept clicks on A tag?

Postby T-Rex on Thu Aug 20, 2009 4:34 pm

Have to return back to this question. Currently I intercept browser creation event in such manner:
Code: Select all
void MyFrame::OnCreateBrowser(wxWebEvent & evt)
{
   wxWebControl * m_browser = (wxWebControl *)wxDynamicCast(evt.GetEventObject(), wxWebControl);
   if(!m_browser) return;
   wxWebControl * new_browser = new wxWebControl(m_notebook, m_browser->GetId());
   m_notebook->AddPage(new_browser, m_browser->GetLabel());
   m_notebook->SetSelection(m_notebook->GetPageIndex(new_browser));
   evt.SetCreateBrowser(new_browser);
   evt.Skip();
}

So each new browser instance will be created as notebook tab.
But I need to create tabs only for browser instances whixh are created by clicking on links with target="_blank", but e.g. popup windows should be created as separate windows using wxWebFrame (e.g. when we go to about:config and try to change value, popup window appears. I need this popup as separate window, not become notebook tab).

So question is: How can I determine that browser instance was created by clicking on link and it is not popup window?
[url=http://wxwidgets.info]wxwidgets.info - Articles and Tutorials
wxToolBox - wxWidgets components that works like VisualStudio ToolBox
T-Rex
Registered User
 
Posts: 10
Joined: Sun Dec 25, 2005 6:57 am
Location: Ukraine

Re: How to intercept clicks on A tag?

Postby qbin on Sat Aug 22, 2009 8:13 am

Корявое решение, но можно попробовать отлавливать BeforeNavigatе и смотреть урл, затем в html искать <a и глядеть есть ли blank_ .

(Moderator Edit, Google Translate -- "Clumsy solution, but you can try to catch BeforeNavigate and watch the URL, then look for html <a and looking whether there is blank_.")
qbin
Registered User
 
Posts: 10
Joined: Thu Jul 30, 2009 8:05 am

Re: How to intercept clicks on A tag?

Postby T-Rex on Tue Aug 25, 2009 1:15 pm

Any example of how to find <a> tag with specified href attribute?
And how to handle the situation when we have 2 links on page with same href but one with target="_blank" and others without it?
[url=http://wxwidgets.info]wxwidgets.info - Articles and Tutorials
wxToolBox - wxWidgets components that works like VisualStudio ToolBox
T-Rex
Registered User
 
Posts: 10
Joined: Sun Dec 25, 2005 6:57 am
Location: Ukraine

Re: How to intercept clicks on A tag?

Postby qbin on Tue Aug 25, 2009 4:27 pm

To moderator: he russian too ;) okey I use it.
And how to handle the situation when we have 2 links on page with same href but one with target="_blank" and others without it?

Я же говорю плохое решение, тут подсказать не могу.
Any example of how to find <a> tag with specified href attribute?

тут тебе в помощь регекспы, примерный код будет таким: (выдираем все a таги с target=_blank)
Кстате я не уверен настчет квантификатора "*?" по идее должен быть ленивым( по спецификации) но работает или нет - не проверял.
Code: Select all
wxString text; // тут будет содерться найденная подстрока(в данном регекспе это таг ссылки с бланком)
wxRegEx qRegex(_T("(?p)<a[[:space:]][[:cntrl:][:print:]йцукенгшщзхъэждлорпавыфячсмитьбюёЁЙЦУКЕНГШЩЗХЪЭЖДЛОРПАВЫФЯЧСМИТЬБЮ]*\\?target[[:space:]]{0,1}=[[:space:]]{0,1}['\"]{0,1}_blank['\"]{0,1}[^>]*\\?>"), wxRE_ADVANCED + wxRE_ICASE);
size_t offset = 0;
size_t len = wxStrlen(html);
int flags = 0;
while (len > 0 && qRegex.Matches(&html[offset], flags, len)) {
  size_t matchoffset, matchlen;
  qRegex.GetMatch(&matchoffset, &matchlen);
   text = html.Mid(offset + matchoffset, matchlen);
/* теперь работаем с тагом(text) можно одним регекспом ограничится, и это будет правильно, но я хз что там тебе для твоих целей понадобится, поэтому для пример привел такой*/
 

  if (matchlen > 0) {
    offset += matchoffset + matchlen;
    len -= matchoffset + matchlen;
  } else {
    // Zero-width match
    offset++;
    len--;
  }
  flags = wxRE_NOTBOL;
}






--
And how to handle the situation when we have 2 links on page with same href but one with target="_blank" and others without it?

I say a bad decision, then I can not tell.

Any example of how to find <a> tag with specified href attribute?

Something like this:
Code: Select all
wxString text;
wxRegEx qRegex(_T("(?p)<a[[:space:]][[:cntrl:][:print:]йцукенгшщзхъэждлорпавыфячсмитьбюёЁЙЦУКЕНГШЩЗХЪЭЖДЛОРПАВЫФЯЧСМИТЬБЮ]*\\?target[[:space:]]{0,1}=[[:space:]]{0,1}['\"]{0,1}_blank['\"]{0,1}[^>]*\\?>"), wxRE_ADVANCED + wxRE_ICASE);
size_t offset = 0;
size_t len = wxStrlen(html);
int flags = 0;
while (len > 0 && qRegex.Matches(&html[offset], flags, len)) {
  size_t matchoffset, matchlen;
  qRegex.GetMatch(&matchoffset, &matchlen);
  text = html.Mid(offset + matchoffset, matchlen)
// work with tag <a...target=_blank>(string text)
  if (matchlen > 0) {
    offset += matchoffset + matchlen;
    len -= matchoffset + matchlen;
  } else {
    // Zero-width match
    offset++;
    len--;
  }
  flags = wxRE_NOTBOL;
}

---
Отредактированно:
Ой, нафиг эти регекспы писал))) тебе же найти с определенным урлом надо, но смысл тот же - урл в регексп и смотреть есть ли совпадения(matches), можно наоборот использовать то что я написал и создать масив того что открывается в новом табе, ну и соответственно после нажатия ссылки сравнивать с массивом. Но тут тоже свои но, если хранить массив, то на каждый таб памяти не наберешься(хотя хранить достаточно урлы ведь?!), а если нет, то столкнешься с замедлением работы, т.к. поиск то время занимает. ( так что тут решай сам, как будет оптимальнее)
Edited:
You also find a certain urlom necessary, but the meaning is the same - the URL in the Extended version and see whether there is a match (matches), you can use on the contrary what I wrote and create an array that is revealed in a new tab , well, respectively, after pressing the link to compare with the array. But here too, but its, if you store an array, then each tab rack memory does not (although the store is enough URLs you?!), And if not, then stolkneshsya with a slowdown of work, because search takes time. (So that then resolves itself, as would be the best)
qbin
Registered User
 
Posts: 10
Joined: Thu Jul 30, 2009 8:05 am

Re: How to intercept clicks on A tag?

Postby T-Rex on Wed Aug 26, 2009 2:50 am

In fact I need not only <A> tag with specified URL, but exact <A> tag which was clicked by user. Page can contain <A> tags with same URL but which have different target atribute. I was thinking not to parse HTML itself but catch OnClick event on tag, then check if it is <A> tag, check if it has target="_blank" attribute, store that info somewhere and in wxEVT_WCB_CREATEBROWSER check if href is same as i strored and if yes then create new tab, otherwise create new window.
[url=http://wxwidgets.info]wxwidgets.info - Articles and Tutorials
wxToolBox - wxWidgets components that works like VisualStudio ToolBox
T-Rex
Registered User
 
Posts: 10
Joined: Sun Dec 25, 2005 6:57 am
Location: Ukraine

Re: How to intercept clicks on A tag?

Postby qbin on Wed Aug 26, 2009 9:29 am

впадлу переводить,пиши еще русский вариант. в beforenavigate можешь урл куда переход идет взять?
---
write copy in russian text too.can u get url where browser go?
qbin
Registered User
 
Posts: 10
Joined: Thu Jul 30, 2009 8:05 am

Return to wxWebConnect Questions, Thoughts & Feedback