Troubleshooting WebView JavaScript Functionality in Android Development

protected void onCreate(Bundle savedInstanceState) {
    String javaScript = "javascript:(function(){var%20content=document.getElementsByClassName('entry-content')[0].innerHTML;var%20content2=content.replace(/<p>/,'');var%20endpos=content2.search(/<div%20id/);})()";



    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_neugkeiten_rss_item);

    Bundle bundle = getIntent().getExtras();
    String item_link = bundle.getString("link");
    String item_title = bundle.getString("title");

    TextView textViewTitle = (TextView) findViewById(R.id.textViewRssItemTitle);
    textViewTitle.setText(item_title);

    WebView webViewContent = (WebView) findViewById(R.id.webViewNewsContent);
    webViewContent.getSettings().setJavaScriptEnabled(true);
    webViewContent.loadUrl("file:///android_asset/Resources/rsscontent.html");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

public void onbuttonclick (View view) {
    WebView webViewContent = (WebView) findViewById(R.id.webViewNewsContent);
    webViewContent.getSettings().setJavaScriptEnabled(true);
    webViewContent.loadUrl("javascript:(function() {var%20content=document.getElementsByClassName('entry-content')[0].innerHTML;var%20content2=content.replace(/<p>/,'');var%20endpos=content2.search(/<div%20id/);var%20content3=content2.slice(0,endpos);var%20content4=content3.replace(/%20%20%20%20/,'');document.body.innerHTML=content4;document.body.style.backgroundColor='#e9e9e9';})()");
}

Hello Everyone,

I am facing an issue: I am trying to load a webpage into my web view and then execute a JavaScript function that removes irrelevant content and changes the background color. On PC, my JavaScript works fine but not in the Android browser or webview. Can anyone help me identify what's wrong with the code?

Using Api-Level 11 and Targeting 17

If anyone knows of a good tutorial on how to download HTML files, I would greatly appreciate it.

Answer №1

Have you attempted the following steps?

1- Insert this line into your onCreate function

webViewContent.setWebViewClient(new myWebClient());

2- Add this custom class to your code

public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageFinished (WebView view, String url) {
            view.loadUrl("javascript:(function(){var%20content=document.getElementsByClassName('entry-content')[0].innerHTML;var%20content2=content.replace(/<p>/,'');var%20endpos=content2.search(/<div%20id/);})()");
        }         
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unique Symbols and Characters in JavaScript

My JavaScript code looks like this: confirm("You are selecting to start an Associate who is Pending Red (P RD) status. Is this your intent?") I am encountering a strange issue where I get an alert with special characters, even though my code does not con ...

Receiving error messages about missing images in my React project

I am new to programming and I have encountered an issue while running my React project. When I use the command npm start, I noticed that some image resources are not being packaged properly, resulting in certain images disappearing when the website is run ...

Weak Password Alert

After spending hours writing code for form validation and password strength checking, I encountered a roadblock. The form validates successfully, but the password strength indicator is not updating as expected. Despite double-checking the logic in my code ...

Identifying particular frequencies/timbre in unprocessed audio data (PCM)

I need to analyze an audio recording that includes two similar tones with the same frequency but spaced a few seconds apart. My goal is to determine the exact number of samples between the end of the first tone and the beginning of the second tone in this ...

Jquery Click function malfunctioning on testing environment

I'm facing a bit of challenge and could really use some assistance. I have this code snippet that functions perfectly in my test document, but once it's uploaded to the live server, everything loads correctly except for the fadeOut click function ...

"Unlocking the power of your device's microphone with

Hello, I'm wondering if it's possible to utilize a device's microphone through trigger.io. Perhaps by integrating the native plugin functionality and creating a wrapper for microphone access using a forge native API, or exploring alternative ...

What is the best way to switch an element's attributes using only JavaScript?

As I crafted this inquiry, I noticed that most of the analogous questions were along the lines of this one (where the user seeks to toggle an element's class using pure JS) or that one (where the user wants to toggle other attributes with jQuery). My ...

Windows encountering a Node npm error while searching for tools version

Currently, I am encountering an issue while trying to use node on my Windows 8.1 operating system. The problem arises when I attempt to install npm packages using 'npm install package.json'. It all started when I began using Redis, but I'm n ...

Activate Bootstrap modal using an anchor tag that links to a valid external URL as a fallback option

To ensure accessibility for users who may have javascript disabled, we follow a company standard of developing our web pages accordingly. Our target demographic still includes those familiar with VCRs blinking 12:00. One particular challenge involves a te ...

What steps should I take to incorporate this feature into my Meteor project?

After successfully installing the type.js package, I discovered that the code works fine when directly executed in the console. Here is the code snippet: $(function(){ $(".typedelement").typed({ strings: ["You don&apo ...

The data under the key "name" in the JSONObject is not in string format

After spending the past few hours trying to troubleshoot this issue, I've come to realize that my Java skills are a bit rusty. Nevertheless, I am determined to complete this method where I need to extract the name of a map from a JSON object. private ...

Issues encountered with AngularJs filter search functionality

Why am I having trouble adapting this search filter from here? This is what my code looks like: controllers.js angular.module('starter.controllers', ['starter.services']) .controller('AppCtrl', function($scope, $ionicModal ...

Ways to minimize the json data size

let c=[ { "eventid": 29096475, "eventname": "Fortuna Dusseldorf v Stuttgart", "opendate": "2019-02-09T18:30:00.000Z", "eventtypeid": 1, "eventtypename": "Soccer", "marketname": "Match Odds", "ma ...

When a div in jQuery is clicked, it binds an effect to a textbox that is

HTML: <div class="infoBox2 blackBoxHover"> <h2>Link: </h2> <input class="fileInput" type="text" id="linkText" name="linkText" /> </div> JAVASCRIPT: $('#linkText').parent('div').click(function () ...

Toggle display of fields based on radio button selection in ASP.NET

I am new to C# asp.net and I am working on creating an online registration form. I am facing a challenge with the following issue. Could someone assist me, please? There are 5 fields in this problem: 1) Category: radio button with options for St ...

Is there a way for me to achieve a vertical page turning effect on a single page?

I am seeking to develop a unique calendar display consisting of 12 images that give the illusion of flipping up when clicked. While I am aware of turn.js, my knowledge of javascript is limited and I need guidance on how to proceed. Despite having a program ...

Cannot adjust dimensions of Chart.js

Struggling to resize chart.js on an HTML page. Have attempted various methods like adjusting the canvas parameters, defining section and div tags, explicitly setting height and width in <style>, as well as using {responsive: true, maintainAspectRatio ...

Transforming Java Web Project into Angular using Java

I'm currently working on a Java web project that uses JSP for the frontend and Java for the backend. I'm looking to convert this project to have an Angular frontend and keep the Java backend. Despite my efforts in searching online, I haven't ...

Exploring the implementation of window.addEventListener within an Angular project

I am currently working on testing a method in Angular using Jasmine. However, I am running into an issue with triggering the mouse event (specifically when the browser back button is clicked). Below is the code snippet I'm working with: navigate() { ...

Leveraging Socket.IO server and client on separate subdomains

I currently have two subdomains set up for my domain: socket.mydomain.com - This is where the Socket.IO server is located app.mydomain.com - A web application that I want to connect to my WebSocket server. On the landing page of app.mydomain.com, I have ...