JavaScript removes // from URL - javascript

URL example = new URL(example.getExampleUrl());
exampleString += "example";
And I have problem with the url that removes // from link. So if I have http://www.google.pl, I get http: www.google.pl instead. I tried with a string, but then I have the same problem. Could anyone tell me how to make this string or url look like a regular link?
its look fine at java http://www.google.pl but at page it is without // so its look http: www.google.pl calendar etc
String test = "http://www.google.pl";
test
Answer to this is that there was a problem with " ' in java, i had to use it like that
onClick='MyWindow=window.open("+ example +")'
String example= "\"" + google.getUrl()+ "\",\""+google.getNameDisplay()+"\",\"width=600,height=600\"";

The toString() method of URL should return just what you want. Try this snippet:
import java.net.*;
import java.io.*;
class Main {
public static void main(String[] args) throws MalformedURLException {
URL example = new URL("http://mostmedia.com");
System.out.println(example.toString());
assert "http://mostmedia.com".equals(example.toString());
}
}
You can run it on repl.it: https://repl.it/CEVw/1

Related

add data to webview html input in android

i have a webview, am trying to load a webpage as shown in figure attached below, there is a dropdown and two inputs in need to get it loaded by filling these values given by us in android webview
I have tried this code but its not working
final String sectionName = getIntent().getStringExtra("sectionName");
final String consumerNumber = getIntent().getStringExtra("consumerNumber");
final String billNumber = getIntent().getStringExtra("billNumber");
this.web = (WebView) findViewById(R.id.web);
this.web.loadUrl(Constants.Payment_URL);
this.web.getSettings().setJavaScriptEnabled(true);
this.web.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Log.e("TAG", "sectionName:" + sectionName);
view.loadUrl("javascript:$($(\"#sectionCode option\")[" + sectionName + "]).prop('selected','selected');$('#ConsumerNo').val('" + consumerNumber + "');$('#billNo').val('" + billNumber + "');");
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
I need the web view get loaded with those values given in programenter image description here
Yes you can do that with the help of javascript.
You can inject javascript into your webview in many ways.
I would advice you to use JavaSCript injector library. You can find it here.
https://github.com/henrychuangtw/WebView-Javascript-Inject

Scrape HTML after JavaScript has modified it in Swift?

I'm trying to program my first website scraper, and my first step is to save the HTML to a string. However, from what I can tell, the data that I need to get is not in the HTML code per se, but rather is added after JavaScript executes some stuff.
My current code is this:
let myURLString = "Example URL"
let myURL = URL(string: myURLString)
var myHTMLString = ""
do {
myHTMLString = try String(contentsOf: myURL!)
} catch let error {
print("Error: \(error)")
}
But this doesn't seem to execute the javascript and instead just gives me the 'unprocessed' HTMl.
I read this answer here, but it's written in Swift 2.0 and since I, to be honest, didn't really understand what was going on ( I don't have much programming experience ): I couldn't get to work in Swift 3.
So, Is there a way to take the HTML from a website, run the JavaScript and then save that as a String in Swift 3? And if so, how do you do it?
Thanks!
After some digging I got something that worked:
import Cocoa
import WebKit
class ViewController: NSViewController, WebFrameLoadDelegate {
#IBOutlet var myWebView: WebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.myWebView.frameLoadDelegate = self
let urlString = "YOUR HTTPS URL"
self.myWebView.mainFrame.load(NSURLRequest(url: NSURL(string: urlString)! as URL) as URLRequest!)
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func webView(_ sender: WebView!, didFinishLoadFor frame: WebFrame!) {
let doc = myWebView.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")! //get it as html
//doc now has the 'processed HTML'
}
}

Server-side rendering of Java, ReactJs code on a web browser

What do I need to do to render this on browser? The code below currently works and renders on Eclipse Console. I need to use this code with a server like Tomcat and display it on browser with localhost. Please advice.
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
public class Test {
private ScriptEngine se;
// Constructor, sets up React and the Component
public Test() throws Throwable {
ScriptEngineManager sem = new ScriptEngineManager();
se = sem.getEngineByName("nashorn");
// React depends on the "global" variable
se.eval("var global = this");
// eval react.js
se.eval(new FileReader("../react-0.14.7/build/react.js"));
// This would also be an external JS file
String component =
"var MyComponent = React.createClass({" +
" render: function() {" +
" return React.DOM.div(null, this.props.text)" +
" }" +
"});";
se.eval(component);
}
// Render the component, which can be called multiple times
public void render(String text) throws Throwable {
String render =
"React.renderToString(React.createFactory(MyComponent)({" +
// using JSONObject here would be cleaner obviously
" text: '" + text + "'" +
"}))";
System.out.println(se.eval(render));
}
public static void main(String... args) throws Throwable {
Test test = new Test();
test.render("I want to Display");
test.render("This on a Browser like google chrome, using Tomcat server with Eclipse, currently it displays on Console in Eclipse.");
}
}
Many possibilities are there. One quite commonly used will be to go for REST services. You can host REST services using JAX-RS or Spring REST support. Put your web pages as a simple html page. Once this page will be loaded, it will make a REST call, will get the data and will show it to the user.
You can use JSP to do that..
Create a JSP page. Import the Test class.
You can check http://www.tutorialspoint.com/articles/run-your-first-jsp-program-in-apache-tomcat-server to know how to use JSP.

Passing value from activity to Android webview for Javascript method

I'm trying to pass an array from my activity to javascript method in html file located in Assets dir.
I'm using JavascriptInterface passing my int array like JSONArray:
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public JSONArray getValues() {
String values = "[100,133,'',120,122,132,133]";
JSONArray jsonarr = null;
try {
jsonarr = new JSONArray(values);
}
catch(JSONException e) {
e.printStackTrace();
}
return jsonarr;
}
}
In javascript method, I take the values thus:
var data = JSON.parse(js.getValues());
Now, I'm testing my project on different devices and AVD:
the code works fine on Samsung Note 2 (JB 4.2.1) and on AVD with target Google APIs (API level 8) while on Asus Nexus 7 (JB 4.2.2) and others AVD with JB 4.2 the code stops working returns an Web Console error:
03-25 16:35:12.809: E/Web Console(11352): Uncaught SyntaxError:
Unexpected token o at file:///android_asset/data/test.html:1
I need these values for represent a chart using a Javascript library.
In addition, I modified the file proguard-project.txt denying the javascript code obfuscation:
keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
-keep public class com.XXX.XXX.DataReportActivity$JavaScriptInterface
-keep public class * implements com.XXX.XXX.DataReportActivity$JavaScriptInterface
-keep classmembers class com.XXX.XXX.DataReportActivity$JavaScriptInterface {
<fields>;
<methods>;
}
-keepattributes JavascriptInterface
does anyone have any idea about solve it?
please, any help is welcome!
Change your Javascript Interface to return a String. JSON.parse is expecting a string, not a JSON object.
#JavascriptInterface
public String getValues() {
String values = "[100,133,'',120,122,132,133]";
return values;
}
The values string might need to be in quotes for the javascript function to be able to parse it too. In other words:
return "'" + values + "'";

Extending Selenium RC with new methods

I am extending the selenium RC by using user-extension.js.
It is able to call the new method function, but throwing following error message.
*ERROR: Command execution failure. Please search the forum at http://clearspace.openqa.org for error details from the log window.
The error message is: Object doesn't support this property or
method*
As this program is executed on Google.com, any one can copy the sample code and execute on their respective PCs.
package package1;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class Sample2
{
private static final String Timeout = "30000";
private static final String BASE_URL = "http://google.com/";
private static final String BASE_URL_1 = "/";
private Selenium selenium;
private HttpCommandProcessor proc;
#BeforeClass
protected void setUp()throws Exception
{
proc = new HttpCommandProcessor("localhost", 4444, "*iexplore", BASE_URL);
selenium = new DefaultSelenium(proc);
selenium.start();
selenium.windowFocus();
selenium.windowMaximize();
selenium.windowFocus();
}
#AfterClass(alwaysRun=true)
protected void tearDown() throws Exception
{
selenium.stop();
}
#Test(groups="search")
public void test_GoogleSearch() throws Exception
{
selenium.open(BASE_URL_1);
selenium.type("name=q", "Bharath Marrivada");
//selenium.click("btnG");
proc.doCommand("myMethod",new String[] {"btnG"}); //user extension
Thread.sleep(5000);
}
}
user-extension.js
Selenium.prototype.doMyMethod = function(inputParams)
{
this.browserbot.click("btnG");
return null;
};
.js and Selenium JAR are in the same folder and executing the Selenium JAR using following command.
java -jar selenium-server.jar -userExtensions user-extensions.js
Any help on this issue?
It means that your command in user-extension file is not locating the element. Try running that in the IDE and check if it works fine
It works for me. Here is the modified user-extensions.js file code:
Selenium.prototype.doMyMethod = function(locator) {
var element = this.page().findElement(locator);
element.click();
};
Rest all remains the same. Hope this helps!!!

Categories

Resources