如何使用GeckoView获取网页源代码 [英] How to get a web page source code with GeckoView

查看:406
本文介绍了如何使用GeckoView获取网页源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Android开发人员,最近我了解 GeckoView . 我可以通过android Webview获取网页的源代码(java代码).

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).

但是,在我的新网站中,Android Webview无法加载网页,但GeckoView可以加载. 现在,我想通过GeckoView获取网页的源代码. 有没有人知道解决此问题的方法?

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 ?

预先感谢

推荐答案

执行此操作的一个选项可以遵循以下步骤:

One option for doing this could follow these steps:

  1. 检测我的geckoview加载的页面;
  2. 将加载的网址保存到sharedpref中;
  3. 如果用户想查看页面的来源,请加载该sharedpref中保存的网址.

所以...

(1-2),geckoview没有方便的shouldOverrideUrlLoading()方法,因此您必须处理NavigationDelegate类,该类具有,我将这一行放在这里,这无非就是将当前url作为字符串保存到名为"geckoViewUrl"的sharedpref字符串中:

(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();

sharedprefs.edit().putString("geckoViewUrl", url).apply();

之前具有所有设置(sharedprefs等).检测到已加载的页面并将URL保存到sharedpref中,让我们转到最后一步3.

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)供我使用,仅希望允许用户查看(并复制)源,出于最小工作量的定律,我使用了另一种具有普通和旧式Webview的活动来显示它.这非常容易实现,并且不会给用户造成混乱.他/她想查看代码,我在另一个活动中将其显示.完成后,他/她结束了新活动,生活继续.

(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.

那么,用户想要来源吗?加载另一个带有Webview的活动,并使其加载已保存的sharedpref字符串:

So, user wants source? Load another activity with a webview and make it load that saved sharedpref string:

addr2open = pref_out.getString("geckoViewUrl","));

addr2open = pref_out.getString("geckoViewUrl", "");

执行此操作(例如),您使url用户想要查看分配给字符串var的源.最后,您要做的就是使Webview加载此字符串,并在其后加上珍贵的单词view-source:,如下所示:

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);

webView.loadUrl("view-source:" + addr2open);

就是这样.当然,您可以依靠相同的活动或使用geckoView来实现解决方案,该解决方案可以显示多个选项,菜单等.我只想向您展示一种以简单易用的方式解决问题的方法.您要求如何使用GeckoView获取网页源代码".这就是答案.完美的作品.如果这对您有好处,请接受此作为正确答案.谢谢你.编码愉快.

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.

这篇关于如何使用GeckoView获取网页源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆