Skip to content

Instantly share code, notes, and snippets.

@acjh
Last active January 17, 2022 07:21
Show Gist options
  • Save acjh/5fcaa505944bb2cf3c8b5ea530b4e804 to your computer and use it in GitHub Desktop.
Save acjh/5fcaa505944bb2cf3c8b5ea530b4e804 to your computer and use it in GitHub Desktop.
Strip <script> tags in Swift
/*
Original question: https://www.facebook.com/groups/swiftiosdev/permalink/840418429450358/
"Attempting to remove HTML markup using a regular expression is problematic.
You don't know what's in there as script or attribute values.
One way is to insert it as the innerHTML of a div, remove any script elements and return the innerHTML"
- RobG , http://stackoverflow.com/a/6660151
"The trick is to base64 encode the data before passing it as the function parameter"
- LJD, https://forums.developer.apple.com/thread/45643#133329
*/
import Foundation
import WebKit
let webview = WKWebView()
func stripScripts(url: String, completionHandler: @escaping ((Any?, Error?) -> Void)) {
guard let url = URL(string: url) else {
completionHandler(nil, nil)
return
}
let html = try? String(contentsOf: url, encoding: .ascii)
let data = html?.data(using: .utf8)
guard let base64EncodedString = data?.base64EncodedString() else {
completionHandler(nil, nil)
return
}
let javaScriptString = "" +
"(function stripScripts(s) {" +
"var html = window.atob(s);" +
"var div = document.createElement('div');" +
"div.innerHTML = html;" +
"var scripts = div.getElementsByTagName('script');" +
"var i = scripts.length;" +
"while (i--) {" +
"scripts[i].parentNode.removeChild(scripts[i]);" +
"}" +
"return div.innerHTML;" +
"})('\(base64EncodedString)');"
webview.evaluateJavaScript(javaScriptString) { (result, error) in
completionHandler(result, error)
}
}
// Usage
stripScripts(url: "https://gist.github.com") { (result, error) in
if let htmlWithoutScriptTags = result {
print(htmlWithoutScriptTags)
}
}
// Run in Xcode Playground
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment