feat: 新增网页复制授权

This commit is contained in:
HuanChengFly 2020-12-05 16:17:04 +08:00
parent ca40f123d4
commit ef707e5e16
4 changed files with 38 additions and 6 deletions

View File

@ -0,0 +1,5 @@
/**
* @name: ClipboardGuard
* @author: huanchengfly
**/
"use strict";function _toConsumableArray(e){if(Array.isArray(e)){for(var n=0,r=Array(e.length);n<e.length;n++)r[n]=e[n];return r}return Array.from(e)}function eventHandler(e){console.log(e.path.length),e.path.length>=threshold||confirm("ClipboardGuardCopyRequest")||(e.preventDefault(),e.stopPropagation())}var threshold=7,key=encodeURIComponent("ClipboardGuardRunning");!function(){if(!window[key])try{window[key]=!0,[document].concat(_toConsumableArray(Array.from(document.getElementsByTagName("iframe")).map(function(e){return e.contentDocument}))).forEach(function(e){e.addEventListener("copy",eventHandler,{passive:!1,capture:!0})})}catch(e){console.log("ClipboardGuard: "+e)}}();

View File

@ -132,5 +132,6 @@ public class PermissionDialog extends AlertDialog implements View.OnClickListene
public static class CustomPermission {
public static final int PERMISSION_LOCATION = 0;
public static final int PERMISSION_START_APP = 1;
public static final int PERMISSION_CLIPBOARD_COPY = 2;
}
}

View File

@ -70,6 +70,7 @@ public class WebViewFragment extends BaseFragment implements DownloadListener {
private String tbliteJs;
private String nightJs;
private String aNightJs;
private String clipboardGuardJs;
private WebView mWebView;
private NavigationHelper navigationHelper;
private ValueCallback<Uri> uploadMessage;
@ -190,6 +191,7 @@ public class WebViewFragment extends BaseFragment implements DownloadListener {
tbliteJs = AssetUtil.getStringFromAsset(getAttachContext(), "tblite.js");
nightJs = AssetUtil.getStringFromAsset(getAttachContext(), "night.js");
aNightJs = AssetUtil.getStringFromAsset(getAttachContext(), "anight.js");
clipboardGuardJs = AssetUtil.getStringFromAsset(getAttachContext(), "ClipboardGuard.js");
}
@Override
@ -264,6 +266,7 @@ public class WebViewFragment extends BaseFragment implements DownloadListener {
private void injectJavaScript() {
if (mWebView == null) return;
mWebView.evaluateJavascript(clipboardGuardJs, null);
String nowTheme = ThemeUtil.getTheme(getAttachContext());
String url = mWebView.getUrl();
if (url == null || nowTheme == null) {
@ -415,12 +418,27 @@ public class WebViewFragment extends BaseFragment implements DownloadListener {
}
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
DialogUtil.build(view.getContext())
.setTitle("Confirm")
.setMessage(message)
.setPositiveButton(android.R.string.ok, (dialog, which) -> result.confirm())
.setNegativeButton(android.R.string.cancel, (dialog, which) -> result.cancel())
.create().show();
if ("ClipboardGuardCopyRequest".equalsIgnoreCase(message)) {
Uri uri = Uri.parse(mWebView.getUrl());
if (uri != null && uri.getHost() != null) {
new PermissionDialog(getAttachContext(),
new PermissionBean(PermissionDialog.CustomPermission.PERMISSION_CLIPBOARD_COPY,
uri.getHost(),
getAttachContext().getString(R.string.title_ask_permission_clipboard_copy, uri.getHost()),
R.drawable.ic_round_file_copy))
.setOnGrantedCallback(isForever -> result.confirm())
.setOnDeniedCallback(isForever -> result.cancel())
.show();
}
} else {
DialogUtil.build(view.getContext())
.setTitle("Confirm")
.setMessage(message)
.setPositiveButton(android.R.string.ok, (dialog, which) -> result.confirm())
.setNegativeButton(android.R.string.cancel, (dialog, which) -> result.cancel())
.create()
.show();
}
return true;
}

View File

@ -209,6 +209,7 @@
<string name="button_denied">禁止</string>
<string name="title_not_ask">不再询问</string>
<string name="title_ask_permission">要允许 %1$s 访问您的%2$s吗</string>
<string name="title_ask_permission_clipboard_copy">要允许 %1$s 写入您的剪贴板吗?</string>
<string name="title_start_app_permission">要允许 %1$s 打开您的%2$s吗</string>
<string name="name_multiapp">应用</string>
<string name="settings_status_bar_darker">状态栏遮罩</string>
@ -399,4 +400,11 @@
<string name="title_oksign_fail">签到失败</string>
<string name="text_login_first">请先登录</string>
<string name="update_tip_loading">正在检查更新</string>
<string name="title_thread_post_recommend">建议您使用其他途径发贴</string>
<string name="message_thread_post_recommend">继续使用本软件发贴可能会出现发贴失败、发贴被隐藏甚至封号等异常情况,建议您使用官方客户端或 PC 网页版发贴</string>
<string name="button_official_client_post">使用官方客户端发贴</string>
<string name="toast_start_failed">启动失败</string>
<string name="button_pc_web_post">使用 PC 网页版发贴</string>
<string name="button_lite_post">继续使用本软件发贴</string>
<string name="permission_name_clipboard_copy">写入剪贴板</string>
</resources>