Android - Javascript functions not working in Webview if HTML is hosted - javascript

I have a webview that wants to retrieve certain String value via a javascript function. I use a JavascriptInterface to print that value. This javascript lives inside a tag in a hosted HTML.
Then, when I call view.loadUrl("javascript:JSInterface.print(myFunction());") it just doesn't print anything.
However, I noticed that if I take the HTML source code and put it into my project assets and load it from there then the instruction works ok and print the desired value.
Are there any security reasons that disables calling javascript functions when they are loaded from Internet?

Ok the problem is that if you use webview.loadDataWithBaseUrl() then the javascript functions and variables will be invalidated, thus you won't be able to access them with webview.loadUrl.
Using loadUrl to load the HTML works but it will remove the possibility to alter the HTML before it is loaded.

Related

How to execute onload function javascript from a python file?

I want to get data from html page, but the page has onload functions which aren't executed when I use the get method of requests.Session().
with Session() as s:
s.get('https://o2.amdm.pro/amdm/S/S/S/insure/Portfolio#/entityHandle=01%7CPD%7C00000000000001384776%7C0001%7C0001', stream=True)
My question is, how to execute those functions as if I was in a browser to get the missing data in order to fill the main div ? Or at least, load the page in a browser and get the html from this page fully loaded ?
My question is, how to execute those functions as if I was in a browser to get the missing data in order to fill the main div ?
You need tool with JavaScript support for that, if you want one similar in usage to python-requests I suggest giving a try Requests-HTML.
load the page in a browser and get the html from this page fully loaded
For that you need web automation tool, here I suggest trying Selenium.

Control execution of JavaScript function only once

I have 5 html pages and a JavaScript function DoInitialConfiguration() in a JavaScript File. User can open any of the five html pages and I want that irrespective of which page is opened, I call this function on the first page access. But also want to remember that the function has been called once and not call it in other page load. I only have these 5 html pages and the JavaScript file which has the function. I am owner of the JavaScript file but can do limited change in the html pages (which I don't own) like load the JavaScipt file and call the function DoInitialConfiguration().
Since the JavaScript file will remain in browser cache, is there a way to remember the function has been called once by using any variable in the JS file. It is OK to call DoInitialConfiguration() again if the page is reloaded after clearing browser cache.
how can this functionality be achieved
If your 5 pages are hosted under same site (which probably would be the case), you can use localStorage to add a key to check if your script was called first time or not.
if (localStorage.getItem("firstRun") != null) {
// second run+ code goes here
} else {
localStorage.setItem("firstRun", "ohyes");
// first run code goes here
}
You can possibly use localStorage for this. Once your code executes set a localStorage variable i.e. localStorage.setItem(<key>, <value>) and in the function check if the localStorage has been set i.e. localStorage.getItem("lastname"). If its set do not execute the code.
It would be good to understand you setup and case study better.
If I understand you correctly, you have 5 separate HTML pages (and you are not running a Single Page Application [SPA]) then what you want to do is impossible through browser and cache memory alone. If you want to remember settings you need to save these using localStorage or cookies (as some of the answers popped up have suggested) but as they are 5 different html pages what does the Js do to make you not want to re-run it on a second page load?

How to call javascript function while JSF fragment loading(like body onload) in ADF

Since jsff file is a fragment in the web page; how can we call a javascript function on 'onload' of that jsff file.
Here is my use case -
I have a taskflow with JSF fragments input.jsff & output.jsff.
Flow of my taskflow is starts with input.jsff, which takes some input from user & output.jsff, which shows the output by processing the input data.
Dropped this taskflow into a jspx file.
jspx file has some javascript methods.
I have to call one of the javascript method while loading output.jsff.
Is there any way to do this ?
If you want just call javascript method on page load use
<af:clientListener type="load" method="jsMethod"/>
hooked to the document tag.
If you need to do certain actions while loading some stuff, use taskflows with few views/steps instead. One step showes one thing, calls method or anything, then proceed to another view.
Its always recommended avoiding the use of java script in oracle adf . If you are trying to invoke a method in java , using java script , you should probably try to add a method as a default-activity in the task-flow and invoke the taskflow .
If you could probably tell what you are trying to do with the java script we would be able to help you better.

Get asynchronously loaded html (via ajax) from url in Android

I want to grab data from a webpage and display it in my Android app. The problem is, the elements I want from the HTML must be first created by an ajax call.
Because the data is loaded via Javascript my approach is to use a Webview to return the HTML. I use the method outlined by jluckyiv here : How do I get the web page contents from a WebView?
However, I realized this doesn't work because the ajax calls have not returned by when the javascript has finished running.
Are there any solutions? I don't have the access to modify the code on the webpage.
Do you use setJavaScriptEnabled(true) ?

Accessing property in my struts action from JavaScript

I am still trying to get my head around how client server comms work with JSP/js and Struts.
I have a Struts property in my action that I can display on my page using
As I understand it this works by looking in my Action for a method called getMyMessage(). So this works ok for me.
I now want to be able to access this value from inside my Javascript so I can dynamically populate a test area when the page loads.
I use
$(document).ready(function( data )
{
$('#contextsTextArea').html(data.myMessage);
});
but this does not work. I know I could assign a var in my JSP that stores the struts property and reference this var in my JS but is there a cleaner way to do this?
JSP runs on the server, JavaScript runs in your browser.
You cannot use a JSP variable like 'myMessage' as a variable in your JavaScript. You can, however, turn it into text on the server so it works as JS when it gets to your browser.
So, try something like this:
$('#contextsTextArea').val('<s:property value="myMessage"/>')
When this JSP is processed on the server the JS code will be left as text while the tag will be turned into the text in your myMessage variable.
Note that you really need to JS escape myMessage otherwise, for example, if the message contains a single quote then the JS will break.
Have a look at your page source to understand what's going on here.
If you are talking about textarea you have to use .val() instead. Like the following.
$('#contextsTextArea').val(data.myMessage);

Categories

Resources