1 module webkit2.UserContentManager; 2 3 private import glib.ConstructionException; 4 private import glib.Str; 5 private import gobject.ObjectG; 6 private import gobject.Signals; 7 private import std.algorithm; 8 private import webkit2.JavascriptResult; 9 private import webkit2.UserContentFilter; 10 private import webkit2.UserScript; 11 private import webkit2.UserStyleSheet; 12 private import webkit2.c.functions; 13 public import webkit2.c.types; 14 15 16 /** 17 * Using a #WebKitUserContentManager user CSS style sheets can be set to 18 * be injected in the web pages loaded by a #WebKitWebView, by 19 * webkit_user_content_manager_add_style_sheet(). 20 * 21 * To use a #WebKitUserContentManager, it must be created using 22 * webkit_user_content_manager_new(), and then passed to 23 * webkit_web_view_new_with_user_content_manager(). User style 24 * sheets can be created with webkit_user_style_sheet_new(). 25 * 26 * User style sheets can be added and removed at any time, but 27 * they will affect the web pages loaded afterwards. 28 * 29 * Since: 2.6 30 */ 31 public class UserContentManager : ObjectG 32 { 33 /** the main Gtk struct */ 34 protected WebKitUserContentManager* webKitUserContentManager; 35 36 /** Get the main Gtk struct */ 37 public WebKitUserContentManager* getUserContentManagerStruct(bool transferOwnership = false) 38 { 39 if (transferOwnership) 40 ownedRef = false; 41 return webKitUserContentManager; 42 } 43 44 /** the main Gtk struct as a void* */ 45 protected override void* getStruct() 46 { 47 return cast(void*)webKitUserContentManager; 48 } 49 50 /** 51 * Sets our main struct and passes it to the parent class. 52 */ 53 public this (WebKitUserContentManager* webKitUserContentManager, bool ownedRef = false) 54 { 55 this.webKitUserContentManager = webKitUserContentManager; 56 super(cast(GObject*)webKitUserContentManager, ownedRef); 57 } 58 59 60 /** */ 61 public static GType getType() 62 { 63 return webkit_user_content_manager_get_type(); 64 } 65 66 /** 67 * Creates a new user content manager. 68 * 69 * Returns: A #WebKitUserContentManager 70 * 71 * Since: 2.6 72 * 73 * Throws: ConstructionException GTK+ fails to create the object. 74 */ 75 public this() 76 { 77 auto __p = webkit_user_content_manager_new(); 78 79 if(__p is null) 80 { 81 throw new ConstructionException("null returned by new"); 82 } 83 84 this(cast(WebKitUserContentManager*) __p, true); 85 } 86 87 /** 88 * Adds a #WebKitUserContentFilter to the given #WebKitUserContentManager. 89 * The same #WebKitUserContentFilter can be reused with multiple 90 * #WebKitUserContentManager instances. 91 * 92 * Filters need to be saved and loaded from #WebKitUserContentFilterStore. 93 * 94 * Params: 95 * filter = A #WebKitUserContentFilter 96 * 97 * Since: 2.24 98 */ 99 public void addFilter(UserContentFilter filter) 100 { 101 webkit_user_content_manager_add_filter(webKitUserContentManager, (filter is null) ? null : filter.getUserContentFilterStruct()); 102 } 103 104 /** 105 * Adds a #WebKitUserScript to the given #WebKitUserContentManager. 106 * The same #WebKitUserScript can be reused with multiple 107 * #WebKitUserContentManager instances. 108 * 109 * Params: 110 * script = A #WebKitUserScript 111 * 112 * Since: 2.6 113 */ 114 public void addScript(UserScript script) 115 { 116 webkit_user_content_manager_add_script(webKitUserContentManager, (script is null) ? null : script.getUserScriptStruct()); 117 } 118 119 /** 120 * Adds a #WebKitUserStyleSheet to the given #WebKitUserContentManager. 121 * The same #WebKitUserStyleSheet can be reused with multiple 122 * #WebKitUserContentManager instances. 123 * 124 * Params: 125 * stylesheet = A #WebKitUserStyleSheet 126 * 127 * Since: 2.6 128 */ 129 public void addStyleSheet(UserStyleSheet stylesheet) 130 { 131 webkit_user_content_manager_add_style_sheet(webKitUserContentManager, (stylesheet is null) ? null : stylesheet.getUserStyleSheetStruct()); 132 } 133 134 /** 135 * Registers a new user script message handler. After it is registered, 136 * scripts can use `window.webkit.messageHandlers.<name>.postMessage(value)` 137 * to send messages. Those messages are received by connecting handlers 138 * to the #WebKitUserContentManager::script-message-received signal. The 139 * handler name is used as the detail of the signal. To avoid race 140 * conditions between registering the handler name, and starting to 141 * receive the signals, it is recommended to connect to the signal 142 * *before* registering the handler name: 143 * 144 * <informalexample><programlisting> 145 * WebKitWebView *view = webkit_web_view_new (); 146 * WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager (); 147 * g_signal_connect (manager, "script-message-received::foobar", 148 * G_CALLBACK (handle_script_message), NULL); 149 * webkit_user_content_manager_register_script_message_handler (manager, "foobar"); 150 * </programlisting></informalexample> 151 * 152 * Registering a script message handler will fail if the requested 153 * name has been already registered before. 154 * 155 * Params: 156 * name = Name of the script message channel 157 * 158 * Returns: %TRUE if message handler was registered successfully, or %FALSE otherwise. 159 * 160 * Since: 2.8 161 */ 162 public bool registerScriptMessageHandler(string name) 163 { 164 return webkit_user_content_manager_register_script_message_handler(webKitUserContentManager, Str.toStringz(name)) != 0; 165 } 166 167 /** 168 * Registers a new user script message handler in script world with name @world_name. 169 * See webkit_user_content_manager_register_script_message_handler() for full description. 170 * 171 * Registering a script message handler will fail if the requested 172 * name has been already registered before. 173 * 174 * Params: 175 * name = Name of the script message channel 176 * worldName = the name of a #WebKitScriptWorld 177 * 178 * Returns: %TRUE if message handler was registered successfully, or %FALSE otherwise. 179 * 180 * Since: 2.22 181 */ 182 public bool registerScriptMessageHandlerInWorld(string name, string worldName) 183 { 184 return webkit_user_content_manager_register_script_message_handler_in_world(webKitUserContentManager, Str.toStringz(name), Str.toStringz(worldName)) != 0; 185 } 186 187 /** 188 * Removes all content filters from the given #WebKitUserContentManager. 189 * 190 * Since: 2.24 191 */ 192 public void removeAllFilters() 193 { 194 webkit_user_content_manager_remove_all_filters(webKitUserContentManager); 195 } 196 197 /** 198 * Removes all user scripts from the given #WebKitUserContentManager 199 * 200 * Since: 2.6 201 */ 202 public void removeAllScripts() 203 { 204 webkit_user_content_manager_remove_all_scripts(webKitUserContentManager); 205 } 206 207 /** 208 * Removes all user style sheets from the given #WebKitUserContentManager. 209 * 210 * Since: 2.6 211 */ 212 public void removeAllStyleSheets() 213 { 214 webkit_user_content_manager_remove_all_style_sheets(webKitUserContentManager); 215 } 216 217 /** 218 * Removes a filter from the given #WebKitUserContentManager. 219 * 220 * Since 2.24 221 * 222 * Params: 223 * filter = A #WebKitUserContentFilter 224 */ 225 public void removeFilter(UserContentFilter filter) 226 { 227 webkit_user_content_manager_remove_filter(webKitUserContentManager, (filter is null) ? null : filter.getUserContentFilterStruct()); 228 } 229 230 /** 231 * Removes a filter from the given #WebKitUserContentManager given the 232 * identifier of a #WebKitUserContentFilter as returned by 233 * webkit_user_content_filter_get_identifier(). 234 * 235 * Params: 236 * filterId = Filter identifier 237 * 238 * Since: 2.26 239 */ 240 public void removeFilterById(string filterId) 241 { 242 webkit_user_content_manager_remove_filter_by_id(webKitUserContentManager, Str.toStringz(filterId)); 243 } 244 245 /** 246 * Unregisters a previously registered message handler. 247 * 248 * Note that this does *not* disconnect handlers for the 249 * #WebKitUserContentManager::script-message-received signal; 250 * they will be kept connected, but the signal will not be emitted 251 * unless the handler name is registered again. 252 * 253 * See also webkit_user_content_manager_register_script_message_handler(). 254 * 255 * Params: 256 * name = Name of the script message channel 257 * 258 * Since: 2.8 259 */ 260 public void unregisterScriptMessageHandler(string name) 261 { 262 webkit_user_content_manager_unregister_script_message_handler(webKitUserContentManager, Str.toStringz(name)); 263 } 264 265 /** 266 * Unregisters a previously registered message handler in script world with name @world_name. 267 * 268 * Note that this does *not* disconnect handlers for the 269 * #WebKitUserContentManager::script-message-received signal; 270 * they will be kept connected, but the signal will not be emitted 271 * unless the handler name is registered again. 272 * 273 * See also webkit_user_content_manager_register_script_message_handler_in_world(). 274 * 275 * Params: 276 * name = Name of the script message channel 277 * worldName = the name of a #WebKitScriptWorld 278 * 279 * Since: 2.22 280 */ 281 public void unregisterScriptMessageHandlerInWorld(string name, string worldName) 282 { 283 webkit_user_content_manager_unregister_script_message_handler_in_world(webKitUserContentManager, Str.toStringz(name), Str.toStringz(worldName)); 284 } 285 286 /** 287 * This signal is emitted when JavaScript in a web view calls 288 * <code>window.webkit.messageHandlers.<name>.postMessage()</code>, after registering 289 * <code><name></code> using 290 * webkit_user_content_manager_register_script_message_handler() 291 * 292 * Params: 293 * jsResult = the #WebKitJavascriptResult holding the value received from the JavaScript world. 294 * 295 * Since: 2.8 296 */ 297 gulong addOnScriptMessageReceived(void delegate(JavascriptResult, UserContentManager) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 298 { 299 return Signals.connect(this, "script-message-received", dlg, connectFlags ^ ConnectFlags.SWAPPED); 300 } 301 }