fit-webview-bridge 0.2.5a1__tar.gz → 0.2.6a1__tar.gz

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.

Potentially problematic release.


This version of fit-webview-bridge might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: fit-webview-bridge
3
- Version: 0.2.5a1
3
+ Version: 0.2.6a1
4
4
  Summary: Qt native WebView bridge with PySide6 bindings
5
5
  Author: FIT Project
6
6
  License: LGPL-3.0-or-later
@@ -67,6 +67,28 @@ class Main(QMainWindow):
67
67
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15"
68
68
  )
69
69
 
70
+ def on_load_finished():
71
+ print("on_load_finished")
72
+ tok = self.view.evaluateJavaScriptWithResult(
73
+ "(() => ({ y: window.scrollY,"
74
+ " h: Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),"
75
+ " vh: window.innerHeight }))()"
76
+ )
77
+
78
+ def on_js(result, token, error):
79
+ print(f"result: {result}")
80
+ if token != tok:
81
+ return
82
+ if error:
83
+ print("JS error:", error)
84
+ return
85
+ # result è dict serializzato in JSON (se hai scelto la serializzazione)
86
+ # oppure primitive QVariant: gestiscilo e continua il flow di screenshot
87
+
88
+ self.view.javaScriptResult.connect(on_js)
89
+
90
+ self.view.loadFinished.connect(on_load_finished)
91
+
70
92
  # abilita/disabilita i bottoni in base alla navigazione
71
93
  self.btnBack.setEnabled(False)
72
94
  self.btnFwd.setEnabled(False)
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "fit-webview-bridge"
3
- version = "0.2.5a1"
3
+ version = "0.2.6a1"
4
4
  description = "Qt native WebView bridge with PySide6 bindings"
5
5
  requires-python = ">=3.11,<3.14"
6
6
  dependencies = ["PySide6==6.9.0", "shiboken6==6.9.0", "shiboken6-generator==6.9.0"]
@@ -23,6 +23,8 @@ public:
23
23
  Q_INVOKABLE void stop();
24
24
  Q_INVOKABLE void reload();
25
25
  Q_INVOKABLE void evaluateJavaScript(const QString& script);
26
+ Q_INVOKABLE quint64 evaluateJavaScriptWithResult(const QString& script);
27
+
26
28
 
27
29
  Q_INVOKABLE void setDownloadDirectory(const QString& dirPath);
28
30
  Q_INVOKABLE QString downloadDirectory() const;
@@ -49,6 +51,9 @@ signals:
49
51
  void downloadFinished(DownloadInfo* info);
50
52
  void downloadFailed(const QString& filePath, const QString& error);
51
53
 
54
+ void javaScriptResult(const QVariant& result, quint64 token, const QString& error);
55
+
56
+
52
57
  protected:
53
58
  void showEvent(QShowEvent*) override;
54
59
  void resizeEvent(QResizeEvent*) override;
@@ -5,6 +5,11 @@
5
5
  #include "WKWebViewWidget.h"
6
6
  #include "DownloadInfo.h"
7
7
 
8
+ #include <atomic>
9
+
10
+ #include <QPointer>
11
+ static std::atomic<quint64> s_jsToken{0};
12
+
8
13
 
9
14
  static inline void fit_emit_downloadStarted(WKWebViewWidget* owner,
10
15
  const QString& name,
@@ -910,6 +915,45 @@ void WKWebViewWidget::evaluateJavaScript(const QString& script) {
910
915
  }];
911
916
  }
912
917
 
918
+ quint64 WKWebViewWidget::evaluateJavaScriptWithResult(const QString& script) {
919
+ if (!d || !d->wk) return 0;
920
+ const quint64 token = ++s_jsToken;
921
+
922
+ NSString* s = [NSString stringWithUTF8String:script.toUtf8().constData()];
923
+
924
+ // ✅ usa QPointer invece di __weak
925
+ QPointer<WKWebViewWidget> guard(this);
926
+
927
+ [d->wk evaluateJavaScript:s completionHandler:^(id result, NSError* error) {
928
+ WKWebViewWidget* self = guard.data();
929
+ if (!self) return; // l'oggetto Qt è stato distrutto: esci in sicurezza
930
+
931
+ QVariant out;
932
+ if ([result isKindOfClass:NSString.class]) {
933
+ out = QString::fromUtf8([(NSString*)result UTF8String]);
934
+ } else if ([result isKindOfClass:NSNumber.class]) {
935
+ out = QVariant::fromValue([(NSNumber*)result doubleValue]);
936
+ } else if (!result || result == (id)kCFNull) {
937
+ out = QVariant();
938
+ } else {
939
+ NSData* data = [NSJSONSerialization dataWithJSONObject:result options:0 error:nil];
940
+ if (data) out = QString::fromUtf8((const char*)data.bytes, (int)data.length);
941
+ }
942
+
943
+ const QString err = error
944
+ ? QString::fromUtf8(error.localizedDescription.UTF8String)
945
+ : QString();
946
+
947
+ // rimanda sul main loop Qt; se 'self' muore prima della consegna,
948
+ // Qt scarta la chiamata perché il receiver non esiste più
949
+ QMetaObject::invokeMethod(self, [self, out, token, err]{
950
+ emit self->javaScriptResult(out, token, err);
951
+ }, Qt::QueuedConnection);
952
+ }];
953
+
954
+ return token;
955
+ }
956
+
913
957
  // =======================
914
958
  // Download directory API
915
959
  // =======================