@capgo/inappbrowser 7.6.12 → 7.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,6 +20,9 @@ import android.net.Uri;
20
20
  import android.net.http.SslError;
21
21
  import android.os.Build;
22
22
  import android.os.Environment;
23
+ import android.print.PrintAttributes;
24
+ import android.print.PrintDocumentAdapter;
25
+ import android.print.PrintManager;
23
26
  import android.provider.MediaStore;
24
27
  import android.text.TextUtils;
25
28
  import android.util.Base64;
@@ -214,6 +217,44 @@ public class WebViewDialog extends Dialog {
214
217
  }
215
218
  }
216
219
 
220
+ public class PrintInterface {
221
+
222
+ private Context context;
223
+ private WebView webView;
224
+
225
+ public PrintInterface(Context context, WebView webView) {
226
+ this.context = context;
227
+ this.webView = webView;
228
+ }
229
+
230
+ @JavascriptInterface
231
+ public void print() {
232
+ // Run on UI thread since printing requires UI operations
233
+ ((Activity) context).runOnUiThread(
234
+ new Runnable() {
235
+ @Override
236
+ public void run() {
237
+ // Create a print job from the WebView content
238
+ PrintManager printManager =
239
+ (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
240
+ String jobName = "Document_" + System.currentTimeMillis();
241
+
242
+ PrintDocumentAdapter printAdapter;
243
+
244
+ // For API 21+ (Lollipop and above)
245
+ printAdapter = webView.createPrintDocumentAdapter(jobName);
246
+
247
+ printManager.print(
248
+ jobName,
249
+ printAdapter,
250
+ new PrintAttributes.Builder().build()
251
+ );
252
+ }
253
+ }
254
+ );
255
+ }
256
+ }
257
+
217
258
  @SuppressLint({ "SetJavaScriptEnabled", "AddJavascriptInterface" })
218
259
  public void presentWebView() {
219
260
  requestWindowFeature(Window.FEATURE_NO_TITLE);
@@ -317,6 +358,10 @@ public class WebViewDialog extends Dialog {
317
358
  new PreShowScriptInterface(),
318
359
  "PreShowScriptInterface"
319
360
  );
361
+ _webView.addJavascriptInterface(
362
+ new PrintInterface(this._context, _webView),
363
+ "PrintInterface"
364
+ );
320
365
  _webView.getSettings().setJavaScriptEnabled(true);
321
366
  _webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
322
367
  _webView.getSettings().setDatabaseEnabled(true);
@@ -1037,7 +1082,13 @@ public class WebViewDialog extends Dialog {
1037
1082
  " window.AndroidInterface.close(); " +
1038
1083
  " } " +
1039
1084
  " }; " +
1040
- "}";
1085
+ "} " +
1086
+ // Override the window.print function to use our PrintInterface
1087
+ "window.print = function() { " +
1088
+ " if (window.PrintInterface) { " +
1089
+ " window.PrintInterface.print(); " +
1090
+ " } " +
1091
+ "};";
1041
1092
  _webView.evaluateJavascript(script, null);
1042
1093
  }
1043
1094
 
@@ -8,14 +8,17 @@
8
8
  android:elevation="4dp"
9
9
  app:titleTextColor="#262626">
10
10
 
11
- <ImageButton
12
- android:id="@+id/closeButton"
13
- android:layout_width="wrap_content"
14
- android:layout_height="wrap_content"
15
- android:src="@drawable/ic_clear_24px"
16
- android:layout_gravity="start"
17
- android:background="#eeeeef"
18
- android:contentDescription="@string/close_button" />
11
+ <ImageButton
12
+ android:id="@+id/closeButton"
13
+ android:layout_width="wrap_content"
14
+ android:layout_height="wrap_content"
15
+ android:layout_gravity="start"
16
+ android:background="#eeeeef"
17
+ android:contentDescription="@string/close_button"
18
+ android:minWidth="38dp"
19
+ android:minHeight="38dp"
20
+ android:src="@drawable/ic_clear_24px"
21
+ android:translationX="-8dp" />
19
22
 
20
23
  <ImageButton
21
24
  android:id="@+id/buttonNearDone"
@@ -478,7 +478,20 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
478
478
  semaphore.signal()
479
479
  } else if message.name == "close" {
480
480
  closeView()
481
- }
481
+ } else if message.name == "magicPrint" {
482
+ if let webView = self.webView {
483
+ let printController = UIPrintInteractionController.shared
484
+
485
+ let printInfo = UIPrintInfo(dictionary: nil)
486
+ printInfo.outputType = .general
487
+ printInfo.jobName = "Print Job"
488
+
489
+ printController.printInfo = printInfo
490
+ printController.printFormatter = webView.viewPrintFormatter()
491
+
492
+ printController.present(animated: true, completionHandler: nil)
493
+ }
494
+ }
482
495
  }
483
496
 
484
497
  func injectJavaScriptInterface() {
@@ -496,9 +509,17 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
496
509
  };
497
510
  }
498
511
  """
499
- DispatchQueue.main.async {
500
- self.webView?.evaluateJavaScript(script, completionHandler: nil)
501
- }
512
+ DispatchQueue.main.async {
513
+ self.webView?.evaluateJavaScript(script) { result, error in
514
+ if let error = error {
515
+ print("JavaScript evaluation error: \(error)")
516
+ } else if let result = result {
517
+ print("JavaScript result: \(result)")
518
+ } else {
519
+ print("JavaScript executed with no result")
520
+ }
521
+ }
522
+ }
502
523
  }
503
524
 
504
525
  open func initWebview(isInspectable: Bool = true) {
@@ -513,14 +534,35 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
513
534
  userContentController.add(self, name: "preShowScriptError")
514
535
  userContentController.add(self, name: "preShowScriptSuccess")
515
536
  userContentController.add(self, name: "close")
537
+ userContentController.add(self, name: "magicPrint")
538
+
539
+ // Inject JavaScript to override window.print
540
+ let script = WKUserScript(
541
+ source: """
542
+ window.print = function() {
543
+ window.webkit.messageHandlers.magicPrint.postMessage('magicPrint');
544
+ };
545
+ """,
546
+ injectionTime: .atDocumentStart,
547
+ forMainFrameOnly: false
548
+ )
549
+ userContentController.addUserScript(script)
550
+
516
551
  webConfiguration.allowsInlineMediaPlayback = true
517
552
  webConfiguration.userContentController = userContentController
553
+
518
554
  let webView = WKWebView(frame: .zero, configuration: webConfiguration)
519
555
 
520
- if webView.responds(to: Selector(("setInspectable:"))) {
521
- // Fix: https://stackoverflow.com/questions/76216183/how-to-debug-wkwebview-in-ios-16-4-1-using-xcode-14-2/76603043#76603043
522
- webView.perform(Selector(("setInspectable:")), with: isInspectable)
523
- }
556
+ // if webView.responds(to: Selector(("setInspectable:"))) {
557
+ // // Fix: https://stackoverflow.com/questions/76216183/how-to-debug-wkwebview-in-ios-16-4-1-using-xcode-14-2/76603043#76603043
558
+ // webView.perform(Selector(("setInspectable:")), with: isInspectable)
559
+ // }
560
+
561
+ if #available(iOS 16.4, *) {
562
+ webView.isInspectable = true
563
+ } else {
564
+ // Fallback on earlier versions
565
+ }
524
566
 
525
567
  if self.blankNavigationTab {
526
568
  // First add the webView to view hierarchy
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/inappbrowser",
3
- "version": "7.6.12",
3
+ "version": "7.7.2",
4
4
  "description": "Capacitor plugin in app browser",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",