Create a find text function using webkitbrowser - c#

I'm trying to implement a find and highlight function using webkitbrowser (instead of default c# webbrowser).
Basically I need to do something like the Ctrl + F functionality.
I had something working in default webbrowser (example below), but this doesn't work in webKitBrowser.
IHTMLDocument2 doc = (IHTMLDocument2)this.webBrowser1.Document;
IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
sel.empty();
IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
if (rng.findText(text, 1000000000, 0))
{
rng.select();
return true;
}
return false;
Thanks all in advance!!

Related

Get selected text of a PDF file in winform webbrowser

I'm trying to get a selected line of a pdf document which i've displayed in Webbrowser control (c# winform)
This is my code
IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject currentSelection = htmlDocument.selection;
if (currentSelection != null)
{
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
MessageBox.Show(range.text);
}
}
i get the following output
It doesn't seem to work as i'm getting an empty message box. I've got to know online that in order to edit/highlight text, i might have to use an SDK!
I'd really appreciate any kind of help/guidence here!

how to set value to textarea in webbrowser c# app

I want to fill a textarea with c# webbrowser , but the textarea is created with "jhtmlarea.js".
This code does not work:
HtmlElement textArea = webBrowser1.Document.All["message"];
if (textArea != null)
{
textArea.InnerText = "This is a test";
}
How do I set the string to javascript editor like "jhtmlarea.js"?
Please help me.
The Webbrowser-control contains a method called "InvokeScript". You can use it to invoke a piece of JavaScript in your document. Simply call
browser.InvokeScript("myFunction", new object[] { arg1, arg2,});
to invoke the function.
See here for further reference.
HtmlElement ele = webBrowser1.Document.GetElementById("message");
if (ele != null)
ele.InnerText = "This is a test";

GetElementById without id and name HOW?

I need to click the button "Add" in the post new wordpress form, this button is to add tags to the post , the trouble is that button don't have the value and id propertie. Is just like that
the html for the button
input type="button" class="button tagadd" value="Add" tabindex="3"
my tries
webBrowser1.Document.GetElementById("button tagadd").InvokeMember("click");
webBrowser1.Document.GetElementById("Add").InvokeMember("click");
"GetElementById without id"
:-)
Unless you can change the markup for the button
What you need now is to traverse the entire DOM and look for a button in a known place. I'd suggest adding jquery if not already exist to be able for easier dom manipulation/search.
If you add jquery you could do something like $(".tagadd").click()
You could try doing
webBrowser1.document.getElementsByClassName("tagadd")
EDIT: Here is a script to create the getElementsByClassName function if it's not available http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
There is also this http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx but I've never used it.
Add an ID. Even if you're dynamically generating the buttons this should be trivial.
If you're using jQuery,
$('.tagadd')
will return a collection of everything with the tagadd class applied. You can further filter this by the other classes (button, etc)
Use this:
onload=function(){
if (document.getElementsByClassName == undefined) {
document.getElementsByClassName = function(className)
{
var hasClassName = new RegExp("(?:^|\s)" + className + "(?:$|\s)");
var allElements = document.getElementsByTagName("*");
var results = [];
var element;
for (var i = 0; (element = allElements[i]) != null; i++) {
var elementClass = element.className;
if (elementClass
&& elementClass.indexOf(className) != -1
&& hasClassName.test(elementClass))
results.push(element);
}
return results;
}
}
}
and another
Some browsers provide the method getElementsByClassName() which lets you select by class without using jQuery (which is a bit heavy if this is all you need). I haven't tested this so I'm not sure how widely it's supported.
Did I mention that you should give everything an ID?
use TagName isteed for example
var elems = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("class") == "button tagadd")
{
elem.InvokeMember("click");
}
}

How to retrieve the scrollbar position of the webbrowser control in .NET

I tried using webBrowser1.Document.Body.ScrollTop and webBrowser1.Document.Body.ScrollLeft, but they don't work. They always return 0 and I can't access webBrowser1.Document.documentElement.ScrollTop and .ScrollLeft.
OK, I solved it:
Dim htmlDoc As HtmlDocument = wb.Document
Dim scrollTop As Integer = htmlDoc.GetElementsByTagName("HTML")(0).ScrollTop
To actually scroll, we found that the ScrollIntoView method worked nicely. For example, to scroll to the top-left of the page.
this.webBrowser.Document.Body.FirstChild.ScrollIntoView(true);
However, we were not successful in actually getting the scroll position (that said, we didn't spend long trying). If you are in control of the HTML content, you might consider using some javascript to copy the scroll position into a hidden element and then read that value out using the DOM.
ScrollTop and ScrollLeft merely allow an offset to be provided between the boundary of an element and its content. There appears to be no way to manipulate the scroll by those values. Instead, you have to use ScrollIntoView.
For anyone interested, here's the C# code equivalent to Marc's answer:
System.Windows.Forms.HtmlDocument htmlDoc = webBrowser.Document;
if (htmlDoc != null)
{
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
int scrollLeft = htmlDoc.GetElementsByTagName("HTML")[0].ScrollLeft;
}
I was able to query the scroll position using this
if (this.webBrowser.Document != null)
{
int scrollPosition = this webBrowser.Document.Body.ScrollTop;
}
You can check documentElement
IHTMLElement2 page =
(wb.Document.DomDocument as IHTMLDocument3).documentElement as IHTMLElement2;
int pos = page.scrollTop;
Accepted answer is VB. For C# WPF WebBrowser, I had to write the following. No idea if I really need all those casts or not. If one can get rid of any of those casts, that would be terrific.
using mshtml;
int? GetScrollTop(System.Windows.Controls.WebBrowser browser)
{
object doc = browser.Document;
HTMLDocument castDoc = doc as HTMLDocument;
IHTMLElementCollection elements = castDoc?.getElementsByTagName("HTML");
IEnumerator enumerator = elements?.GetEnumerator();
enumerator?.MoveNext();
var first = enumerator?.Current;
IHTMLElement2 castFirst = first as IHTMLElement2;
int? top = castFirst?.scrollTop;
return top;
}
I found kurt's answer almost worked but had to change the array reference as follows:
var document = (HTMLDocument)Browser.Document;
var scrollTop = (int)document.getElementsByTagName("HTML").item(0).ScrollTop;

How to set a checkbox to "checked" using mshtml?

I can do most things I need to with mshtml, but I'm a bit stuck with how to set a checkbox input element to "checked". Here's the situation...
IHTMLElementCollection inputElements = (IHTMLElementCollection)doc.all.tags("input");
foreach (IHTMLElement el in inputElements)
{
string elementHtml = el.outerHTML;
string termsOfServiceIdentifier = "id=chkUTOS_ver2";
// select the Terms of Service checkbox
if (elementHtml.Contains(termsOfServiceIdentifier))
{
HTMLInputElement chkTOS = (HTMLInputElement)el;
chkTOS.#checked = true; // that's the solution. Thanks Wayne.
}
else
{
// do nothing - we're not interested in this element
}
}
Thanks in advance for any help!
Gregg
HTMLInputElement exposes the Checked property as a Boolean
In plain JavaScript, checkbox elements have a checked property. So [in plain JavaScript] you might write:
document.getElementById("myCheckbox").checked = true;
I don't know .NET or whatever you're using there, but they may do it in a similar way.
Steve

Categories

Resources