Latest 0.0.1
Homepage https://github.com/StevenPreston/webview-utilities
License MIT
Platforms ios 8.0
Authors

[![CI Status](http://img.shields.io/travis/Steven Preston/WebView-Utilities.svg?style=flat)](https://travis-ci.org/Steven Preston/WebView-Utilities)
Version
License
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

WebView-Utilities is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "WebView-Utilities"

Usage

All functions are exposed through an extension of WKWebView. Don’t forget that the WKWebView should complete navigation before executing any of these. As a result, waiting for didFinishNavigation to fire is recommended.

The following functions are available:

Clear the <head> element

func clearHead(completionHandler: ((AnyObject?, NSError?) -> Void)?)

Removes all HTML from the <head> element of the document currently loaded in the WKWebView.

Add an element to the <head>

func addElementToHead(elementHTML: String, completionHandler: ((AnyObject?, NSError?) -> Void)?)

Add an element to the <head> element. Specify the HTML of the element, for example:

<meta name="viewport" content="width=device-width, initial-scale=1.0" >

Remove an element from the document

func removeElement(elementId: String, completionHandler: ((AnyObject?, NSError?) -> Void)?)

Remove an element with the specified ID from the document.

Add CSS to the document

func addCSS(css: String, completionHandler: ((AnyObject?, NSError?) -> Void)?)

Injects CSS into the document by appending a <style> element to the <head>. For example:

webView.addCSS("body { margin: 20px; } .aClass { margin: 20px; }", completionHandler: nil)

Remove all elements withing <body> except for one

func stripAllOtherElementsFromBody(elementId: String, completionHandler: ((AnyObject?, NSError?) -> Void)?)

Strips all elements from the document <body>, with the exeception of the element with the specified ID.

Chaining methods

You may want to execute multiple functions that manipulate the DOM. Instead of executing multiple functions and chaining them through their completionHandlers, you can access the Javascript for each function and then concatenate each Javascript snippet:

let js = WKWebView.clearHeadJS() + WKWebView.addCSSJS("body { margin: 20px }")

You can then execute this through the provided convenience method:

webView.executeJavaScript(js, completionHandler: nil)

This is preferred over using evaluateJavascript as it prevents the navigation delegate from firing.

Author

Steven Preston, [email protected]

License

WebView-Utilities is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "WebView-Utilities",
    "version": "0.0.1",
    "summary": "A library of helper functions to manipulate the DOM within a WKWebView.",
    "description": "A library of helper functions (available through an extension to WKWebView) to manipulate the DOM within a WKWebView. Functions available:n* stripAllOtherElementsFromBody - removes all except one element from HTML body.n* clearHead - removes all inner HTML from the head element.n* addElementToHead - adds an element to the head.n* removeElement - removes an element from the DOM.n* addCSS - add CSS to the document head.",
    "homepage": "https://github.com/StevenPreston/webview-utilities",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Steven Preston": "[email protected]"
    },
    "source": {
        "git": "https://github.com/StevenPreston/webview-utilities.git",
        "tag": "0.0.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "WebView-Utilities/Classes/**/*"
}

Pin It on Pinterest

Share This