diff --git a/API/Modules/WebPage.cs b/API/Modules/WebPage.cs index 74f3fe6..faa65e7 100644 --- a/API/Modules/WebPage.cs +++ b/API/Modules/WebPage.cs @@ -450,6 +450,8 @@ public void _open(string url, string method, string data, string callbackId) // Navigate to URL and set handler for completion // Remove any DocumentCompleted listeners from last round browser.Navigate(uri, method, data, customHeaders); + browser.Navigated -= Navigated; + browser.Navigated += Navigated; browser.DocumentCompleted -= DocumentCompleted; browser.DocumentCompleted += DocumentCompleted; // Add callback to execution stack @@ -462,6 +464,22 @@ public void _open(string url, string method, string data, string callbackId) } } + /// + /// This callback is invoked after the web page is + /// created but before a URL is loaded. The callback + /// may be used to change global objects. + /// + private string _onInitializedCallbackId = null; + + /// + /// Sets the onInitialized callback to the corresponding callbackId + /// + /// + public void _onInitialized(string callbackId) + { + _onInitializedCallbackId = callbackId; + } + /// /// A stack of callbacks in the V8 context that need to /// be executed (or are in the process of being executed) @@ -521,6 +539,26 @@ public void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArg } } + /// + /// Event handler for actions needed after the webpage is created + /// and the document is loading. + /// + /// + /// + public void Navigated(object sender, WebBrowserNavigatedEventArgs args) + { + if (browser != null) + { + Console.log(browser.DocumentText); + if (!String.IsNullOrEmpty(_onInitializedCallbackId)) + { + // Set current frame + switchToMainFrame(); + Callback.ExecuteOnce(_onInitializedCallbackId, null); + } + } + } + /// /// Navigation (back) /// diff --git a/includes/trifle/modules/WebPage.js b/includes/trifle/modules/WebPage.js index 228ff7c..10ab95d 100644 --- a/includes/trifle/modules/WebPage.js +++ b/includes/trifle/modules/WebPage.js @@ -30,10 +30,6 @@ this.trifle.modules = this.trifle.modules || {}; console.xdebug("new WebPage()"); // Properties this.objectName = "WebPage"; - // Fire Initialized event - if (this.onInitialized) { - this.onInitialized.call(this); - } }, // Additional methods @@ -82,6 +78,15 @@ this.trifle.modules = this.trifle.modules || {}; return !!callback ? callback.call(page, status) : null; } }; + + // Fire Initialized event + var _this = this; + this._onInitialized(Callback.id(function () { + if (_this.onInitialized) { + _this.onInitialized.call(_this); + } + })); + // Open URL in .NET API return this._open(url, method, data, Callback.id(complete)); },