Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Sep 3, 2024
1 parent 4b29348 commit 436459e
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 129 deletions.
172 changes: 135 additions & 37 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.util.ArrayMap;
import android.view.View;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.View;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.PermissionRequest;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;

import androidx.browser.customtabs.CustomTabsCallback;
import androidx.browser.customtabs.CustomTabsClient;
import androidx.browser.customtabs.CustomTabsIntent;
Expand All @@ -31,15 +30,13 @@
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.annotation.Permission;
import com.getcapacitor.annotation.PermissionCallback;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.Semaphore;

import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -72,7 +69,6 @@ public class InAppBrowserPlugin
private WebViewDialog webViewDialog = null;
private String currentUrl = "";


private PermissionRequest currentPermissionRequest;

public void handleMicrophonePermissionRequest(PermissionRequest request) {
Expand Down Expand Up @@ -153,14 +149,14 @@ protected void handleOnActivityResult(
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
String dataString = data.getDataString();
if (data.getClipData() != null) { // If multiple file selected
if (data.getClipData() != null) { // If multiple file selected
int count = data.getClipData().getItemCount();
results = new Uri[count];
for (int i = 0; i < count; i++) {
results[i] = data.getClipData().getItemAt(i).getUri();
}
} else if (dataString != null) { //if single file selected
results = new Uri[]{Uri.parse(dataString)};
} else if (dataString != null) { //if single file selected
results = new Uri[] { Uri.parse(dataString) };
}
}
}
Expand Down Expand Up @@ -307,6 +303,7 @@ public void open(PluginCall call) {

call.resolve();
}

@PluginMethod
public void clearCache(PluginCall call) {
CookieManager cookieManager = CookieManager.getInstance();
Expand Down Expand Up @@ -353,27 +350,35 @@ public void clearCookies(PluginCall call) {
String[] parts = cookie.split("=");
if (parts.length > 0) {
cookiesToRemove.add(parts[0].trim());
CookieManager.getInstance().setCookie(url, String.format("%s=del;", parts[0].trim()));
CookieManager.getInstance()
.setCookie(url, String.format("%s=del;", parts[0].trim()));
}
}
}

StringBuilder scriptToRun = new StringBuilder();
for (String cookieToRemove: cookiesToRemove) {
scriptToRun.append(String.format("window.cookieStore.delete('%s', {name: '%s', domain: '%s'});", cookieToRemove, cookieToRemove, url));
for (String cookieToRemove : cookiesToRemove) {
scriptToRun.append(
String.format(
"window.cookieStore.delete('%s', {name: '%s', domain: '%s'});",
cookieToRemove,
cookieToRemove,
url
)
);
}

Log.i("DelCookies", String.format("Script to run:\n%s", scriptToRun));

this.getActivity()
.runOnUiThread(
new Runnable() {
@Override
public void run() {
webViewDialog.executeScript(scriptToRun.toString());
}
}
);
.runOnUiThread(
new Runnable() {
@Override
public void run() {
webViewDialog.executeScript(scriptToRun.toString());
}
}
);

call.resolve();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import androidx.annotation.Nullable;

import com.getcapacitor.JSObject;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -47,7 +45,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -100,6 +97,7 @@ public void postMessage(String message) {
}

public class PreShowScriptInterface {

@JavascriptInterface
public void error(String error) {
// Handle message from JavaScript
Expand Down Expand Up @@ -139,8 +137,8 @@ public void presentWebView() {
"AndroidInterface"
);
_webView.addJavascriptInterface(
new PreShowScriptInterface(),
"PreShowScriptInterface"
new PreShowScriptInterface(),
"PreShowScriptInterface"
);
_webView.getSettings().setJavaScriptEnabled(true);
_webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
Expand Down Expand Up @@ -287,51 +285,53 @@ private void injectJavaScriptInterface() {
}

private void injectPreShowScript() {
// String script =
// "import('https://unpkg.com/[email protected]/darkreader.js').then(() => {DarkReader.enable({ brightness: 100, contrast: 90, sepia: 10 });window.PreLoadScriptInterface.finished()})";
// String script =
// "import('https://unpkg.com/[email protected]/darkreader.js').then(() => {DarkReader.enable({ brightness: 100, contrast: 90, sepia: 10 });window.PreLoadScriptInterface.finished()})";

if (preShowSemaphore != null) {
return;
}

String script =
"async function preShowFunction() {\n" +
_options.getPreShowScript() + '\n' +
_options.getPreShowScript() +
'\n' +
"};\n" +
"preShowFunction().then(() => window.PreShowScriptInterface.success()).catch(err => { console.error('Preshow error', err); window.PreShowScriptInterface.error(JSON.stringify(err, Object.getOwnPropertyNames(err))) })";

Log.i(
"InjectPreShowScript",
String.format("PreShowScript script:\n%s", script)
"InjectPreShowScript",
String.format("PreShowScript script:\n%s", script)
);

preShowSemaphore = new Semaphore(0);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
_webView.evaluateJavascript(script, null);
activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
_webView.evaluateJavascript(script, null);
}
}
});
);

try {
if (!preShowSemaphore.tryAcquire(10, TimeUnit.SECONDS)) {
Log.e(
"InjectPreShowScript",
"PreShowScript running for over 10 seconds. The plugin will not wait any longer!"
"InjectPreShowScript",
"PreShowScript running for over 10 seconds. The plugin will not wait any longer!"
);
return;
}
if (preshowError != null && !preshowError.isEmpty()) {
Log.e(
"InjectPreShowScript",
"Error within the user-provided preShowFunction: " + preshowError
"InjectPreShowScript",
"Error within the user-provided preShowFunction: " + preshowError
);
}

} catch (InterruptedException e) {
Log.e(
"InjectPreShowScript",
"Error when calling InjectPreShowScript: " + e.getMessage()
"InjectPreShowScript",
"Error when calling InjectPreShowScript: " + e.getMessage()
);
} finally {
preShowSemaphore = null;
Expand Down Expand Up @@ -639,30 +639,42 @@ public void onPageFinished(WebView view, String url) {
isInitialized = true;
_webView.clearHistory();
if (_options.isPresentAfterPageLoad()) {
boolean usePreShowScript = _options.getPreShowScript() != null && !_options.getPreShowScript().isEmpty();
boolean usePreShowScript =
_options.getPreShowScript() != null &&
!_options.getPreShowScript().isEmpty();
if (!usePreShowScript) {
show();
_options.getPluginCall().resolve();
} else {
executorService.execute(new Runnable() {
@Override
public void run() {
if (_options.getPreShowScript() != null && !_options.getPreShowScript().isEmpty()) {
injectPreShowScript();
}

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
show();
_options.getPluginCall().resolve();
executorService.execute(
new Runnable() {
@Override
public void run() {
if (
_options.getPreShowScript() != null &&
!_options.getPreShowScript().isEmpty()
) {
injectPreShowScript();
}
});

activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
show();
_options.getPluginCall().resolve();
}
}
);
}
}
});
);
}
}
} else if (_options.getPreShowScript() != null && !_options.getPreShowScript().isEmpty()) {
} else if (
_options.getPreShowScript() != null &&
!_options.getPreShowScript().isEmpty()
) {
injectPreShowScript();
}

Expand Down
8 changes: 4 additions & 4 deletions ios/Plugin/InAppBrowserPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class InAppBrowserPlugin: CAPPlugin {
}

@objc func clearCache(_ call: CAPPluginCall) {
let clearCache = call.getBool("cache") ?? false
let clearCache = call.getBool("cache") ?? false

DispatchQueue.main.async {
let dataStore = WKWebsiteDataStore.default()
Expand All @@ -86,15 +86,15 @@ public class InAppBrowserPlugin: CAPPlugin {
WKWebsiteDataStore.default().httpCookieStore.getAllCookies { cookies in
for cookie in cookies {

if (cookie.domain == host || cookie.domain.hasSuffix(".\(host)") || host.hasSuffix(cookie.domain)) {
if cookie.domain == host || cookie.domain.hasSuffix(".\(host)") || host.hasSuffix(cookie.domain) {
let semaphore = DispatchSemaphore(value: 1)
WKWebsiteDataStore.default().httpCookieStore.delete(cookie) {
semaphore.signal()
}
semaphore.wait()
}
}

call.resolve()
}
}
Expand All @@ -108,7 +108,7 @@ public class InAppBrowserPlugin: CAPPlugin {
call.reject("Invalid URL")
return
}

DispatchQueue.main.async {
WKWebsiteDataStore.default().httpCookieStore.getAllCookies { cookies in
var cookieDict = [String: String]()
Expand Down
Loading

0 comments on commit 436459e

Please sign in to comment.