Reputation: 672
I am an android developer and I knew about GeckoView recently. I can get source code of a web page by android WebView (java code).
However with my new website, android Webview can't load the webpage but GeckoView can. Now I want to get source code of a web page by GeckoView. Is there any body know the solution to resolve this problem ?
Thank in advance
Upvotes: 0
Views: 796
Reputation: 1
By webpage source code, do you mean the html? If so, you can use a combination of an interface to grab the code on completion and the GeckoWebExecutor's resulting input stream. Assuming you want the html as a string, you would do something similar:
public void getHtml(String url) {
GeckoWebExecutor executor = new GeckoWebExecutor(geckoRuntime);
final GeckoResult<WebResponse> result = executor.fetch(
new WebRequest.Builder(url)
.header("Accept", ".html")
.build());
if(result != null){
result.then(response -> {
String htmlAsText = convertStreamToString(response.body);
return htmlAsText;
});
}
}
public String convertStreamToString(InputStream is){
if (is == null){
return null;
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(is, StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
catch (IOException e) {
//do better error handling
return null;
}
}
}
Upvotes: 0
Reputation: 3149
One option for doing this could follow these steps:
So...
(1-2) as you already know, geckoview doesn't have that handy shouldOverrideUrlLoading()
method, so you will have to deal with the NavigationDelegate
class, which has the onLocationChange()
, where i put this line, which does nothing more than save the current url as a string into a sharedpref string named "geckoViewUrl":
sharedprefs.edit().putString("geckoViewUrl", url).apply();
having all setup before (sharedprefs etc). Detected the loaded page and saved the url into a sharedpref, let's go to the final step 3.
(3) for my use, wishing only to allow the user to see (and copy) the source, for the law of the minimum effort i used another activity with a plain and old webview to display it. This is very easy to implement and rises no confusion to the user. He/She wants to see the code, i show it in another activity. When it's done, he/she closes the new activity and life continues.
So, user wants source? Load another activity with a webview and make it load that saved sharedpref string:
addr2open = pref_out.getString("geckoViewUrl", "");
Doing this (for example), you get the url user wants to see source assigned to a string var. To finish, all you have to do is to make the webview load this string preceded by the precious word view-source:
, this way:
webView.loadUrl("view-source:" + addr2open);
That's it. Of course you could implement a solution relying on the same activity, or using geckoView, showing multiple options, menus etc. I only wanted to show you a way to solve your problem in a nice 'n' easy way. You asked for "How to get a web page source code with GeckoView". Here is the answer. Works perfectly. If this is good for you, please, accept this as the correct answer. Thank you. Happy coding.
One example on the use of "view-source", from the creators of GeckoView: https://firefox-source-docs.mozilla.org/devtools-user/view_source/index.html (see item "Link to a line number").
Upvotes: 0
Reputation: 54
As far as I know, you cannot use GeckoView to get the source code, but the geckoview library does have GeckoWebExecutor. Take a look at the fetch method and the WebResponse it returns.
By converting the WebResponse.body
InputStream to a String you can get the source code.
Upvotes: 1