Skip to content

Commit

Permalink
注入js和调用js方法完成
Browse files Browse the repository at this point in the history
以及webview的安全漏洞设置完成
  • Loading branch information
JakePrim committed May 16, 2018
1 parent 9cf4324 commit f680396
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 123 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
*.iml
*.apk
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
/.idea/compiler.xml
/.idea/gradle.xml
/.idea/runConfigurations.xml
/.idea/misc.xml
/.idea/modules.xml

.DS_Store
/build
/captures
.externalNativeBuild
/.idea/markdown-navigator.xml
67 changes: 0 additions & 67 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ PrimWeb primWeb = PrimWeb.with(this)
### API 详解
1. 动态切换X5和Android 的webview
```
动态的设置webview的父view
.setWebParent(frameLayout, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
public enum WebViewType {
Android, X5
}
Expand Down
2 changes: 1 addition & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
# proguardFiles setting in buildWeb.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
Expand Down
48 changes: 39 additions & 9 deletions app/src/main/java/com/prim/web/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.prim.primweb.core.PrimWeb;
import com.prim.primweb.core.client.MyX5WebChromeClient;
import com.prim.primweb.core.client.WebViewClient;
import com.prim.primweb.core.webview.IAgentWebView;

Expand All @@ -18,22 +20,23 @@ public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private PrimWeb primWeb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.fl_web);
PrimWeb primWeb = PrimWeb.with(this)
primWeb = PrimWeb.with(this)
.setWebParent(frameLayout, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
.setWebViewType(PrimWeb.WebViewType.Android)
.addJavascriptInterface("jsAgent", new MyJavaObject())
.setWebViewType(PrimWeb.WebViewType.X5)
.setModeType(PrimWeb.ModeType.Normal)
.setAgentWebViewClient(new MyWebViewClient(this))
.build()
.setX5WebChromeClient(new MyX5WebChromeClient())
.addJavascriptInterface("nativeBridge", new MyJavaObject())
.buildWeb()
.ready()
.launch("https://blog.csdn.net/yy1300326388/article/details/43965493");

primWeb.callJsLoader().callJS("jsMethod");
.launch("http://front.52yingzheng.com/test/shiluTest/h5-standard/h5-standard.html");
}

/** 使用代理的WebViewClient */
Expand All @@ -53,12 +56,39 @@ public boolean shouldOverrideUrlLoading(IAgentWebView view, String url) {
public class MyJavaObject {

@JavascriptInterface
public void login(String data) {
public void closeWebView(String data) {
finish();
}

@JavascriptInterface
public void signIn(String data) {
primWeb.getCallJsLoader().callJS("nativeBridgeCallback['networkRequestCallback']", "'测试调用h5端的Js方法'");
Toast.makeText(MainActivity.this, "登录", Toast.LENGTH_LONG).show();
}

@JavascriptInterface
public void bindTel(String data) {
Toast.makeText(MainActivity.this, "绑定手机号", Toast.LENGTH_LONG).show();
}

@JavascriptInterface
public void camera(String data) {
Toast.makeText(MainActivity.this, "调取摄像头", Toast.LENGTH_LONG).show();
}

@JavascriptInterface
public void photos(String data) {
Toast.makeText(MainActivity.this, "调取相册", Toast.LENGTH_LONG).show();
}

public void medth() {
@JavascriptInterface
public void geographicPosition(String data) {
Toast.makeText(MainActivity.this, "获取地理位置", Toast.LENGTH_LONG).show();
}

@JavascriptInterface
public void copyText(String data) {
Toast.makeText(MainActivity.this, "复制文本", Toast.LENGTH_LONG).show();
}

}
Expand Down
2 changes: 1 addition & 1 deletion primweb-core/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
# proguardFiles setting in buildWeb.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
Expand Down
91 changes: 69 additions & 22 deletions primweb-core/src/main/java/com/prim/primweb/core/PrimWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class PrimWeb {

private ICallJsLoader callJsLoader;

public static boolean DEBUG = false;

private IAgentWebView webView;
private ViewGroup mViewGroup;
private ViewGroup.LayoutParams mLayoutParams;
Expand Down Expand Up @@ -121,14 +123,15 @@ public static void init(Application application) {

/** webview 安全检查 */
private void doCheckSafe() {
if (null == webView) {
if (null == webView) {//webview 不能为空
webView = new PrimAgentWebView(context.get());
mView = webView.getAgentWebView();
}
webView.removeRiskJavascriptInterface();
}

/** 调用js方法 */
public ICallJsLoader callJsLoader() {
/** 获取调用js方法 */
public ICallJsLoader getCallJsLoader() {
if (null == webView) {
throw new NullPointerException("webView most not be null,please check your code!");
}
Expand All @@ -138,7 +141,7 @@ public ICallJsLoader callJsLoader() {
return callJsLoader;
}

/** 注入js脚本 */
/** 获取注入js脚本方法 */
public IJsInterface getJsInterface() {
if (null == webView) {
throw new NullPointerException("webView most not be null,please check your code!");
Expand All @@ -149,30 +152,44 @@ public IJsInterface getJsInterface() {
return mJsInterface;
}

/** 准备阶段 */
void ready() {
// 加载设置
/** 获取websettings, Object具体的是android webSetting 还是x5 webSetting 自己判断强转*/
public Object getWebSettings() {
if (null == setting) {
if (webViewType == WebViewType.Android) {
setting = new DefaultWebSetting(context.get());
} else {
setting = new X5DefaultWebSetting(context.get());
}
}
setting.setSetting(webView);
return setting.getWebSetting();
}

// 加载url加载器
/** 获取url加载器 加载URL和刷新url操作 */
public IUrlLoader getUrlLoader() {
if (null == webView) {
throw new NullPointerException("webView most not be null,please check your code!");
}
if (null == urlLoader) {
urlLoader = new UrlLoader(webView);
}
return urlLoader;
}

// 加载js脚本注入
if (null == mJsInterface) {
mJsInterface = SafeJsInterface.getInstance(webView, modeType);
/** 准备阶段 */
void ready() {
// 加载webview设置
if (null == setting) {
if (webViewType == WebViewType.Android) {
setting = new DefaultWebSetting(context.get());
} else {
setting = new X5DefaultWebSetting(context.get());
}
}
setting.setSetting(webView);

if (mJavaObject != null && !mJavaObject.isEmpty()) {
mJsInterface.addJavaObjects(mJavaObject);
// 加载url加载器
if (null == urlLoader) {
urlLoader = new UrlLoader(webView);
}

// 加载webViewClient 系统设置的优先
Expand All @@ -191,16 +208,27 @@ void ready() {
webView.setAgentWebViewClient(agentWebViewClient);
}

//加载webChromeClient
//加载webChromeClient 系统设置的优先
if (webChromeClient != null || x5WebChromeClient != null) {
if (webChromeClient != null) {
webView.setAndroidWebChromeClient(webChromeClient);
}
if (x5WebChromeClient != null) {
webView.setX5WebChromeClient(x5WebChromeClient);
}
} else {
//加载代理的webChromeClient

}

// 加载js脚本注入
if (null == mJsInterface) {
mJsInterface = SafeJsInterface.getInstance(webView, modeType);
}

if (mJavaObject != null && !mJavaObject.isEmpty()) {
mJsInterface.addJavaObjects(mJavaObject);
}
}

/** 发起阶段 */
Expand Down Expand Up @@ -331,12 +359,6 @@ public CommonBuilder setModeType(ModeType modeType) {
return this;
}

/** 注入js */
public CommonBuilder addJavascriptInterface(@NonNull String name, @NonNull Object o) {
primBuilder.addJavaObject(name, o);
return this;
}

/** 设置WebView的类型 如果设置了setAgentWebView 此方法最好不要调用 */
public CommonBuilder setWebViewType(WebViewType webViewType) {
primBuilder.setWebViewType(webViewType);
Expand Down Expand Up @@ -370,12 +392,37 @@ public CommonBuilder setX5WebChromeClient(com.tencent.smtt.sdk.WebChromeClient w
return this;
}

/** 注入js脚本 */
public JsInterfaceBuilder addJavascriptInterface(@NonNull String name, @NonNull Object o) {
return new JsInterfaceBuilder(primBuilder).addJavascriptInterface(name, o);
}

/** 设置完成开始建造 */
public PerBuilder build() {
public PerBuilder buildWeb() {
return primBuilder.build();
}
}

public static class JsInterfaceBuilder {
private PrimBuilder primBuilder;

public JsInterfaceBuilder(PrimBuilder primBuilder) {
this.primBuilder = primBuilder;
}

/** 注入js脚本 */
public JsInterfaceBuilder addJavascriptInterface(@NonNull String name, @NonNull Object o) {
primBuilder.addJavaObject(name, o);
return this;
}

/** 设置完成开始建造 */
public PerBuilder buildWeb() {
return primBuilder.build();
}

}

/** 设置完成准备发射 */
public static class PerBuilder {
private PrimWeb primWeb;
Expand Down
Loading

0 comments on commit f680396

Please sign in to comment.