Skip to content

Commit

Permalink
Change WebPage.onInitialized to be called after the page is created w…
Browse files Browse the repository at this point in the history
…hile the document is loading using the Navigated event. Resolves sdesalas#47
  • Loading branch information
tejacques committed Feb 23, 2015
1 parent 9703cba commit b42d8e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
38 changes: 38 additions & 0 deletions API/Modules/WebPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -462,6 +464,22 @@ public void _open(string url, string method, string data, string callbackId)
}
}

/// <summary>
/// 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.
/// </summary>
private string _onInitializedCallbackId = null;

/// <summary>
/// Sets the onInitialized callback to the corresponding callbackId
/// </summary>
/// <param name="callbackId"></param>
public void _onInitialized(string callbackId)
{
_onInitializedCallbackId = callbackId;
}

/// <summary>
/// A stack of callbacks in the V8 context that need to
/// be executed (or are in the process of being executed)
Expand Down Expand Up @@ -521,6 +539,26 @@ public void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArg
}
}

/// <summary>
/// Event handler for actions needed after the webpage is created
/// and the document is loading.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
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);
}
}
}

/// <summary>
/// Navigation (back)
/// </summary>
Expand Down
13 changes: 9 additions & 4 deletions includes/trifle/modules/WebPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
},
Expand Down

0 comments on commit b42d8e5

Please sign in to comment.