Android Studio WebView click on a loaded html page's element - javascript

I am loading a website in my app that contains an element I want to programmatically click (automate the click) as when this is clicked is time sensitive. I have the timing part down but need to trigger this element via an on screen click. I have read that performClick() "should" work but I have no way to verify if its actually clicking in the webview or not. The element in the html I need to trigger with a "touch" is called 'overlay'. Could someone provide me with a template that could do this?

You can use the below snippet to trigger at touch.
Video_Player.my_runnable = new Runnable()
{
#Override
public void run()
{
view.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis() + 50,MotionEvent.ACTION_DOWN,300,300,0));
view.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis() + 50,MotionEvent.ACTION_UP,300,300,0));
}
};

Related

UrbanAirship - HTML Button - set tag to pass to code

What I am wanting to do is simple. I am using UrbanAirship to show a modal view ("In-app Automation"), which is HTML. I have a button in my HTML. The message/view is showing fine when the event is fired. Right now I have in the HTML for the button:
<button class="light" type="button" onclick="UAirship.close()">OK</button>
When the button is tapped, the call to UAirship.close() works fine and closes the message. I setup a listener in my Android code, such as follows:
UAirship.shared()
.getInAppMessagingManager()
.addListener(new InAppMessageListener() {
#Override
public void onMessageDisplayed(#NonNull String scheduleId, #NonNull InAppMessage message) {
// gets here fine
}
#Override
public void onMessageFinished(#NonNull String scheduleId, #NonNull InAppMessage message, #NonNull ResolutionInfo resolutionInfo) {
// gets here fine as well
}
});
It gets into those methods without any issues when the message/view shows and dismisses. However, I want to be able to detect one particular button was tapped vs. others I have in the HTML. Is there a way to go about this easily, so that some custom tag or something can be passed in the "ResolutionInfo" object or "InAppMessage"?
My hopes were that there would be something like "UAirship.close('tag')", where that tag would be passed back. Or even a custom HTML tag I can put on the button that would be recognized in the code. Really just any way of going about it is fine, I just can't seem to find in the documentation how to properly do this with HTML.
I'm looking at the documentation here. I see the UAirship.runAction() method, but it's not really clear to me what a simple implementation would be for this (also, the "CustomAction" class referenced in the action registering doesn't even exist in the library it seems..).
I basically just need to call one line of code in my Activity when the Airship message closes after that button is tapped.
Thoughts?
I've found that this solution works, although there may be a better one out there.
In the HTML, for the button, call the close() and then a runAction() call:
onclick="UAirship.close(); UAirship.runAction('my_action', null, null)"
Then in the code, in my onCreate() in my Activity, I have this:
final String actionName = "my_action";
Action customAction = new Action() {
#NonNull
#Override
public ActionResult perform(#NonNull ActionArguments actionArguments) {
String buttonActionName = actionArguments.getMetadata().getString(ActionArguments.REGISTRY_ACTION_NAME_METADATA);
if (buttonActionName.equals(actionName)) {
// DO WHAT IS NEEDED HERE
}
return ActionResult.newEmptyResult();
}
};
​
UAirship.shared()
.getActionRegistry()
.registerAction(customAction, actionName);

Slight flickering due to jQuery's $(document).ready when manipulating DOM in DocumentCompleted event of WebBrowser

I'm using WebBrowser c# to scrape a website and manipulate it before displaying to user.
Example, I want all the Buttons of the current website to be hidden.
I do so by injecting Javascript, but I believe it's too late as the page is already rendered out, thus, a slight flicker (the flicker website has all the button shown) before all buttons are hidden.
(All buttons shown -> JavaScript .ready(function(){}) inject executed -> All buttons hidden).
private void browser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string js = "$(document).ready(function() { hideButtons(); })";
script = browser1.Document.createElement("script");
script.SetAttribute("text", js);
head.AppendChild(script);
}
Code above : JavaScript inject.
How to solve the flicker (when all the buttons are shown for a quick moment before hiding them all back after jQuery execution)?
I have tried manipulating the DOM object in DocumentCompleted method by setting its OuterHtml to nothing "" or by setting css attribute to display:none; but there is still such slight flicker.
private void browser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var b = browser1.Document.getElementById("btn1");
b.OuterHtml = "";
}
Is there a way to manipulate DOM objects before rendering out to the users?
What event do I call?
The only thing I can think of that would work is to intercept the navigating function, pull the page manually and then render the HTML in the browser window.
private void browser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//Cancel the request
e.Cancel = true;
using (WebClient client = new WebClient())
{
string html = client.DownloadString(e.Url.ToString());
//Find the text that you want to replace here and replace it
browser1.DocumentText = html;
}
}

Android webView disable Wikipedia search bar

I'm building an app which using webView and loading Wikipedia's web pages. I would like to disable this part:
I don't know what is the best way to do this... I have thought of auto scrolling down, but then the user is able to scroll up.
Auto scrolling down is also doesn't accurate and may hide some of the value's information.
Thanks.
By looking into Wikipedia's source code you can see that the search bar is located inside a div container with the class name "header-container header-chrome". You are able to remove it from the view using JavaScripct code and the HTML DOM getElementsByClassName() Method.
The following code might help you with removing the search bar from the HTML page and display the rest.
WebView myWebViewDisply = (WebView) findViewById(R.id.WebViewDisply);
myWebViewDisply.getSettings().setJavaScriptEnabled(true);
myWebViewDisply.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
myWebViewDisply.loadUrl("javascript:(function() { " +
"document.getElementsByClassName
('header-container header-chrome')[0].style.display='none';"
+"})()");
}
});
myWebViewDisply.loadUrl(youUrl);

Suppress Tap Gesture when user clicks on HTML button on UIWebView

I have a view. Over this view i have placed a UIWebView.
Size of UIWebView is exactly same as that of the view.
On the view i have applied TapGesture. Whenever user taps on the web view my tap gesture method gets called which is fine as per my expectations.
Now issue arises when i load an HTML file with some buttons on it. These Buttons are associated with Javascript methods.
If i click on any button in html the javascript method gets called [Perfect] but i want to suppress the Native Tap gesture method on this case.
Can anybody suggest me what should i do to suppress tap gesture method.
I only want to suppress tap gesture method when user clicks on any HTML button that is already associated with some other method.
Waiting for help :)
Tap Gesture Code
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.tapCount ==1 && self.view.superview != nil)
{
NSLog(#"tag :: %ld",(long)touch.view.tag);
NSLog(#"name :: %#",touch.view);
BOOL isYesNo=NO;
if(touch.view.tag!=100)// ([touch.view isKindOfClass:[UIWebView class]])
{
// we did not touched our UIWebView
isYesNo=NO;
}
else
{
// we touched our UIWebView
isYesNo=YES;
}
}
return isYesNo;
}
TAG value is always returned as ZERO

Android - Webview Javascript to call a function for a specific DIV class

I am loading a JS webpage in my WebView. The webpage looks like a calendar and has items on the calendar that, when clicked or double-clicked, call functions defined on the page.
When a user taps once on the item, I want it to call the proper function as well as do the same for tapping twice on it. I know this is android, but users are expecting the function to happen when "double-clicking".
The function for double-clicking the item is:
function openPairingDetails(tripSequenceNumber) {
var url = "/csswa/ea/fa/getPairingDetails.do?popup=true&tripSequenceNumber=" + tripSequenceNumber;
var features = "height=600,width=900,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no";
openWindow(url, features);
return false;
For now at least, all the items that the user will be double-clicking are of the same class:
<div class="boardpiece clickable" onclick="selectPairing(event, this);" ondblclick="openPairingDetails('5545220');"
So, the short version is this: I need to display the webpage (done) and allow the user to single-click to select the item and double-click to open the item. Each of these events link to a defined function. So, do I set up an OnClickListener to initiate a JS function on a single/double click of the DIV CLASS?
edit: I do not have the luxury of altering the website.
edit 2: Here is my logic: Page is displayed with an object. That object has a specific div class. The object ID is dynamic so it cannot be used. The object calls a specific function when either single or double clicked. Can I set a tap listener to call those specific functions? In other words - if the user taps once on one of these objects, the "onClick" function is called. If the user performs a longpress on an object, the "onDblClick" function is called. Can I set these to call the respective functions?
public void onClick(View v)
{
// some other code here
}
public boolean onLongClick(View view)
{
// just showing a Toast here
return false;
}
Thank you
~ Dan
double click cannot handle it in android webview. Any click listener on webview will work complete webview not on any element.
better u need to implement it on div only with this link
link
den use javascript bridge interface if u want to some android native related stuff

Categories

Resources