Change data from html page with Javascript in WebView - javascript

In my Android application, i need to use a WebView for one function. But i want translate this webpage. So i've make a Javascript code for remplace "MyValueEN" in "MyValueFR" with string.xml but i can't usegetResources.GetString(...).
Exemple of my code:
String js_string = "javascript:\n" +
"document.getElementsByClassName('Div1')[0].style.display='none';\n" +
"document.getElementsByClassName('Div2')[0].style.display='none';void(0)" +
"document.body.innerHTML = document.body.innerHTML.split(\"MyTextToChange\").join("+ getResources().getString(R.string.MyTextToChange) + ");void(0);"; //It's here the error
When i call getResources().getString(R.string.MyTextToChange"), i've got an error :
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test/com.test}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Can i use getRessources() in Javascript code ? Did i have do an error ? Or i need use another function ?
Thanks for your help

you will need to initialise
getResources().getString(R.string.MyTextToChange)
inside onCreate, incase you initialised it outside

Related

How to push in 'class instance' or 'this' into a 'Function' method

i am trying to put the user defined logic (Javascript) in frontend and my backend function execute it with 'Function' method (as below)
let cmdResult = Function('"use strict"; let _AW_DAY_SUMMARY_=' + JSON.stringify(_AW_DAY_SUMMARY_) + ';' + sUserDefinedCmd)()
...
this.alicia.dump(); // run the code correctly if hardcoded in script. But hit error below when defined by user frontpage
The user defined logic work correctly, and i was able to push in object with tons of values with JSON.stringify method. But i have a challenge as i have loaded quite a number of classes
constructor(
private alicia: AliciaService,
private benny: BennyService,
private cass: CassService,
) { }
How could the user defined logic to allow call like this.alicia.dump();
Whenever i use it in my user defined logic, it complained
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'alicia' of undefined
There are many business process intensive method in those classes and i need the user defined logic to call and return rather than re-write from scratch.
The execution context of function that was created through Function constructor is a global object: window in your case.
In order to change this context you specify with each context to execute your function through Function.prototype.call():
Function('...').call(this)
Stackblitz Example

Javascript square bracket notation couldn't be evaluated properly while using an android webview. Any advice to fix it?

I'm using android webview and want to make dynamic function calls over the Javascript Interface. My interface's name is AndroidBridge. And here is my code:
AndroidBridge[key](values[key]);
It works properly on the browser. But when it comes to my webview, is throwing the error below:
Error connecting controller TypeError: AndroidBridge[key] is not a function [object Object]"
Any advice to jump over that issue?
I can give you the two possible reasons for that issue now:
Missing #JavascriptInterface annotation.
The method is missing or params not matching.
I don't know why browser's js interpreter looks to "a square named version of that object", but method names that match should work without that errors.
I can leave here a fully working example:
class JsBridge {
#JavascriptInterface
fun showToast(message: String) {
Log.d("DEBUG", "JsBridge showToast message:$message")
val currentActivity = current ?: return
Toast.makeText(currentActivity, message, Toast.LENGTH_SHORT).show()
}
companion object {
#JvmStatic
val instance: JsBridge = JsBridge()
}
}
// Somewhere in your code
webView.addJavascriptInterface(JsBridge.instance, "AndroidBridge")
Now you can call that method in the js side:
AndroidBridge.showToast('A message');
// or
AndroidBridge['showToast']('A message');
They both should work.

Set an input field within a form, within a frame using javascript in a UWP project

I am trying to autofill a form that is located within a frame. There appears to be a syntax error in the javascript. Any help with the function would be appreciated. Here is the code I am using;
string func = "window.frames['framename'].document.forms[0].getElementsByName('StreetName')[0].value = " + streetname;
await webview1.InvokeScriptAsync("eval", new string[] { func });
The id of the tags is not set, only the names. Any ideas where the syntax error in the JS is?
p.s. If there is a way to get the verbose error message from the injected javascript I would welcome the advice.
'framename'
is highly suspect to me.
Are you sure that is intended to be a string literal? (In that case, I think this is a runtime error and not a syntax error).

nightwatch.js: keyboard action

I am using nightwatch.js to write automation scripts. I want to use keyboard keys but it seems not working.
I have tried:
hitEnter: function () {
this.setValue('#submitButton', this.Keys.ENTER);
}
call this function in test_file.js as
loginPage
.hitEnter();
It gives error TypeError: Cannot read property 'ENTER' of undefined
What am I doing wrong?
this.Keys.ENTER
In this case, this = loginPage , not browser, you should execute with browser object through api :
this.setValue('#submitButton', this.api.Keys.ENTER);
edit:
api will return an object which contains " custom command/assertion" + "core command/assertion" + "global variables". Keys is core command ,based on selenium.

How to instantiate a WinMD file to a Windows Store App Project

Hi have an Windows Store App that need to reference a single Winmd file.
I've added it adding as a normal DLL, that's the correct way?
As my understanding I'm trying to "instantiate" it on my default.js as below:
var helloTest = new WinJS.HelloWR.HelloRT();
With that I'm able to access its methods as:
var ret = helloTest.helloRt();
The problem happens when I try to run it, it raises the following exception:
0x800a138f - JavaScript runtime error: Unable to get property 'HelloRT' of undefined or null reference
It means that my HelloWR namespace is undefined, I've been looking around google for some similar sample but I've found anything.
Let me know if anyone need more info about the content of this winmd.
For those whom faces the same problem, I just did as below:
var helloTest = new HelloWR.HelloRT;
It's not needed to put WinJS as the question at the Top.
Now I'm able to access its methods:
var ret = helloTest.helloRt();
//ret returns 'HelloRT'
If someone explain it better I will check as answer.

Categories

Resources