1 module webkit2webextension.WebPage; 2 3 private import gio.AsyncResultIF; 4 private import gio.Cancellable; 5 private import glib.ErrorG; 6 private import glib.GException; 7 private import glib.PtrArray; 8 private import glib.Str; 9 private import gobject.ObjectG; 10 private import gobject.Signals; 11 private import std.algorithm; 12 private import webkit2webextension.ConsoleMessage; 13 private import webkit2webextension.ContextMenu; 14 private import webkit2webextension.DOMDocument; 15 private import webkit2webextension.DOMElement; 16 private import webkit2webextension.Frame; 17 private import webkit2webextension.URIRequest; 18 private import webkit2webextension.URIResponse; 19 private import webkit2webextension.UserMessage; 20 private import webkit2webextension.WebEditor; 21 private import webkit2webextension.WebHitTestResult; 22 private import webkit2webextension.c.functions; 23 public import webkit2webextension.c.types; 24 25 26 /** */ 27 public class WebPage : ObjectG 28 { 29 /** the main Gtk struct */ 30 protected WebKitWebPage* webKitWebPage; 31 32 /** Get the main Gtk struct */ 33 public WebKitWebPage* getWebPageStruct(bool transferOwnership = false) 34 { 35 if (transferOwnership) 36 ownedRef = false; 37 return webKitWebPage; 38 } 39 40 /** the main Gtk struct as a void* */ 41 protected override void* getStruct() 42 { 43 return cast(void*)webKitWebPage; 44 } 45 46 /** 47 * Sets our main struct and passes it to the parent class. 48 */ 49 public this (WebKitWebPage* webKitWebPage, bool ownedRef = false) 50 { 51 this.webKitWebPage = webKitWebPage; 52 super(cast(GObject*)webKitWebPage, ownedRef); 53 } 54 55 56 /** */ 57 public static GType getType() 58 { 59 return webkit_web_page_get_type(); 60 } 61 62 /** 63 * Get the #WebKitDOMDocument currently loaded in @web_page 64 * 65 * Returns: the #WebKitDOMDocument currently loaded, or %NULL 66 * if no document is currently loaded. 67 */ 68 public DOMDocument getDomDocument() 69 { 70 auto __p = webkit_web_page_get_dom_document(webKitWebPage); 71 72 if(__p is null) 73 { 74 return null; 75 } 76 77 return ObjectG.getDObject!(DOMDocument)(cast(WebKitDOMDocument*) __p); 78 } 79 80 /** 81 * Gets the #WebKitWebEditor of a #WebKitWebPage. 82 * 83 * Returns: the #WebKitWebEditor 84 * 85 * Since: 2.10 86 */ 87 public WebEditor getEditor() 88 { 89 auto __p = webkit_web_page_get_editor(webKitWebPage); 90 91 if(__p is null) 92 { 93 return null; 94 } 95 96 return ObjectG.getDObject!(WebEditor)(cast(WebKitWebEditor*) __p); 97 } 98 99 /** 100 * Get the identifier of the #WebKitWebPage 101 * 102 * Returns: the identifier of @web_page 103 */ 104 public ulong getId() 105 { 106 return webkit_web_page_get_id(webKitWebPage); 107 } 108 109 /** 110 * Returns the main frame of a #WebKitWebPage. 111 * 112 * Returns: the #WebKitFrame that is the main frame of @web_page 113 * 114 * Since: 2.2 115 */ 116 public Frame getMainFrame() 117 { 118 auto __p = webkit_web_page_get_main_frame(webKitWebPage); 119 120 if(__p is null) 121 { 122 return null; 123 } 124 125 return ObjectG.getDObject!(Frame)(cast(WebKitFrame*) __p); 126 } 127 128 /** 129 * Returns the current active URI of @web_page. 130 * 131 * You can monitor the active URI by connecting to the notify::uri 132 * signal of @web_page. 133 * 134 * Returns: the current active URI of @web_view or %NULL if nothing has been 135 * loaded yet. 136 */ 137 public string getUri() 138 { 139 return Str.toString(webkit_web_page_get_uri(webKitWebPage)); 140 } 141 142 /** 143 * Send @message to the #WebKitWebView corresponding to @web_page. If @message is floating, it's consumed. 144 * 145 * If you don't expect any reply, or you simply want to ignore it, you can pass %NULL as @callback. 146 * When the operation is finished, @callback will be called. You can then call 147 * webkit_web_page_send_message_to_view_finish() to get the message reply. 148 * 149 * Params: 150 * message = a #WebKitUserMessage 151 * cancellable = a #GCancellable or %NULL to ignore 152 * callback = (nullable): A #GAsyncReadyCallback to call when the request is satisfied or %NULL 153 * userData = the data to pass to callback function 154 * 155 * Since: 2.28 156 */ 157 public void sendMessageToView(UserMessage message, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 158 { 159 webkit_web_page_send_message_to_view(webKitWebPage, (message is null) ? null : message.getUserMessageStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 160 } 161 162 /** 163 * Finish an asynchronous operation started with webkit_web_page_send_message_to_view(). 164 * 165 * Params: 166 * result = a #GAsyncResult 167 * 168 * Returns: a #WebKitUserMessage with the reply or %NULL in case of error. 169 * 170 * Since: 2.28 171 * 172 * Throws: GException on failure. 173 */ 174 public UserMessage sendMessageToViewFinish(AsyncResultIF result) 175 { 176 GError* err = null; 177 178 auto __p = webkit_web_page_send_message_to_view_finish(webKitWebPage, (result is null) ? null : result.getAsyncResultStruct(), &err); 179 180 if (err !is null) 181 { 182 throw new GException( new ErrorG(err) ); 183 } 184 185 if(__p is null) 186 { 187 return null; 188 } 189 190 return ObjectG.getDObject!(UserMessage)(cast(WebKitUserMessage*) __p, true); 191 } 192 193 /** 194 * Emitted when a message is sent to the console. This can be a message 195 * produced by the use of JavaScript console API, a JavaScript exception, 196 * a security error or other errors, warnings, debug or log messages. 197 * The @console_message contains information of the message. 198 * 199 * Params: 200 * consoleMessage = the #WebKitConsoleMessage 201 * 202 * Since: 2.12 203 */ 204 gulong addOnConsoleMessageSent(void delegate(ConsoleMessage, WebPage) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 205 { 206 return Signals.connect(this, "console-message-sent", dlg, connectFlags ^ ConnectFlags.SWAPPED); 207 } 208 209 /** 210 * Emitted before a context menu is displayed in the UI Process to 211 * give the application a chance to customize the proposed menu, 212 * build its own context menu or pass user data to the UI Process. 213 * This signal is useful when the information available in the UI Process 214 * is not enough to build or customize the context menu, for example, to 215 * add menu entries depending on the #WebKitDOMNode at the coordinates of the 216 * @hit_test_result. Otherwise, it's recommended to use #WebKitWebView::context-menu 217 * signal instead. 218 * 219 * Params: 220 * contextMenu = the proposed #WebKitContextMenu 221 * hitTestResult = a #WebKitWebHitTestResult 222 * 223 * Returns: %TRUE if the proposed @context_menu has been modified, or %FALSE otherwise. 224 * 225 * Since: 2.8 226 */ 227 gulong addOnContextMenu(bool delegate(ContextMenu, WebHitTestResult, WebPage) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 228 { 229 return Signals.connect(this, "context-menu", dlg, connectFlags ^ ConnectFlags.SWAPPED); 230 } 231 232 /** 233 * This signal is emitted when the DOM document of a #WebKitWebPage has been 234 * loaded. 235 * 236 * You can wait for this signal to get the DOM document with 237 * webkit_web_page_get_dom_document(). 238 */ 239 gulong addOnDocumentLoaded(void delegate(WebPage) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 240 { 241 return Signals.connect(this, "document-loaded", dlg, connectFlags ^ ConnectFlags.SWAPPED); 242 } 243 244 /** 245 * Emitted after form elements (or form associated elements) are associated to a particular web 246 * page. This is useful to implement form auto filling for web pages where form fields are added 247 * dynamically. This signal might be emitted multiple times for the same web page. 248 * 249 * Note that this signal could be also emitted when form controls are moved between forms. In 250 * that case, the @elements array carries the list of those elements which have moved. 251 * 252 * Clients should take a reference to the members of the @elements array if it is desired to 253 * keep them alive after the signal handler returns. 254 * 255 * Deprecated: , use #WebKitWebPage::form-controls-associated-for-frame instead. 256 * 257 * Params: 258 * elements = a #GPtrArray of 259 * #WebKitDOMElement with the list of forms in the page 260 * 261 * Since: 2.16 262 */ 263 gulong addOnFormControlsAssociated(void delegate(PtrArray, WebPage) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 264 { 265 return Signals.connect(this, "form-controls-associated", dlg, connectFlags ^ ConnectFlags.SWAPPED); 266 } 267 268 /** 269 * Emitted after form elements (or form associated elements) are associated to a particular web 270 * page. This is useful to implement form auto filling for web pages where form fields are added 271 * dynamically. This signal might be emitted multiple times for the same web page. 272 * 273 * Note that this signal could be also emitted when form controls are moved between forms. In 274 * that case, the @elements array carries the list of those elements which have moved. 275 * 276 * Clients should take a reference to the members of the @elements array if it is desired to 277 * keep them alive after the signal handler returns. 278 * 279 * Params: 280 * elements = a #GPtrArray of 281 * #WebKitDOMElement with the list of forms in the page 282 * frame = the #WebKitFrame 283 * 284 * Since: 2.26 285 */ 286 gulong addOnFormControlsAssociatedForFrame(void delegate(PtrArray, Frame, WebPage) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 287 { 288 return Signals.connect(this, "form-controls-associated-for-frame", dlg, connectFlags ^ ConnectFlags.SWAPPED); 289 } 290 291 /** 292 * This signal is emitted when @request is about to be sent to 293 * the server. This signal can be used to modify the #WebKitURIRequest 294 * that will be sent to the server. You can also cancel the resource load 295 * operation by connecting to this signal and returning %TRUE. 296 * 297 * In case of a server redirection this signal is 298 * emitted again with the @request argument containing the new 299 * request to be sent to the server due to the redirection and the 300 * @redirected_response parameter containing the response 301 * received by the server for the initial request. 302 * 303 * Modifications to the #WebKitURIRequest and its associated 304 * #SoupMessageHeaders will be taken into account when the request 305 * is sent over the network. 306 * 307 * Params: 308 * request = a #WebKitURIRequest 309 * redirectedResponse = a #WebKitURIResponse, or %NULL 310 * 311 * Returns: %TRUE to stop other handlers from being invoked for the event. 312 * %FALSE to continue emission of the event. 313 */ 314 gulong addOnSendRequest(bool delegate(URIRequest, URIResponse, WebPage) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 315 { 316 return Signals.connect(this, "send-request", dlg, connectFlags ^ ConnectFlags.SWAPPED); 317 } 318 319 /** 320 * This signal is emitted when a #WebKitUserMessage is received from the 321 * #WebKitWebView corresponding to @web_page. You can reply to the message 322 * using webkit_user_message_send_reply(). 323 * 324 * You can handle the user message asynchronously by calling g_object_ref() on 325 * @message and returning %TRUE. If the last reference of @message is removed 326 * and the message has been replied, the operation in the #WebKitWebView will 327 * finish with error %WEBKIT_USER_MESSAGE_UNHANDLED_MESSAGE. 328 * 329 * Params: 330 * message = the #WebKitUserMessage received 331 * 332 * Returns: %TRUE if the message was handled, or %FALSE otherwise. 333 * 334 * Since: 2.28 335 */ 336 gulong addOnUserMessageReceived(bool delegate(UserMessage, WebPage) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 337 { 338 return Signals.connect(this, "user-message-received", dlg, connectFlags ^ ConnectFlags.SWAPPED); 339 } 340 341 /** 342 * This signal is emitted to indicate various points during form 343 * submission. @step indicates the current stage of form submission. 344 * 345 * If this signal is emitted with %WEBKIT_FORM_SUBMISSION_WILL_SEND_DOM_EVENT, 346 * then the DOM submit event is about to be emitted. JavaScript code 347 * may rely on the submit event to detect that the user has clicked 348 * on a submit button, and to possibly cancel the form submission 349 * before %WEBKIT_FORM_SUBMISSION_WILL_COMPLETE. However, beware 350 * that, for historical reasons, the submit event is not emitted at 351 * all if the form submission is triggered by JavaScript. For these 352 * reasons, %WEBKIT_FORM_SUBMISSION_WILL_SEND_DOM_EVENT may not 353 * be used to reliably detect whether a form will be submitted. 354 * Instead, use it to detect if a user has clicked on a form's 355 * submit button even if JavaScript later cancels the form 356 * submission, or to read the values of the form's fields even if 357 * JavaScript later clears certain fields before submitting. This 358 * may be needed, for example, to implement a robust browser 359 * password manager, as some misguided websites may use such 360 * techniques to attempt to thwart password managers. 361 * 362 * If this signal is emitted with %WEBKIT_FORM_SUBMISSION_WILL_COMPLETE, 363 * the form will imminently be submitted. It can no longer be 364 * cancelled. This event always occurs immediately before a form is 365 * submitted to its target, so use this event to reliably detect 366 * when a form is submitted. This event occurs after 367 * %WEBKIT_FORM_SUBMISSION_WILL_SEND_DOM_EVENT if that event is 368 * emitted. 369 * 370 * Params: 371 * form = the #WebKitDOMElement to be submitted, which will always correspond to an HTMLFormElement 372 * step = a #WebKitFormSubmissionEventType indicating the current 373 * stage of form submission 374 * sourceFrame = the #WebKitFrame containing the form to be 375 * submitted 376 * targetFrame = the #WebKitFrame containing the form's target, 377 * which may be the same as @source_frame if no target was specified 378 * textFieldNames = names of 379 * the form's text fields 380 * textFieldValues = values 381 * of the form's text fields 382 * 383 * Since: 2.20 384 */ 385 gulong addOnWillSubmitForm(void delegate(DOMElement, WebKitFormSubmissionStep, Frame, Frame, PtrArray, PtrArray, WebPage) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 386 { 387 return Signals.connect(this, "will-submit-form", dlg, connectFlags ^ ConnectFlags.SWAPPED); 388 } 389 }