Skip to content

Commit a85b83c

Browse files
Predrag SamardzicPredrag Samardzic
authored andcommitted
support to add functions to window.onload (javascript)
1 parent 9a8eac7 commit a85b83c

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

PSHTMLView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'PSHTMLView'
11-
s.version = '0.1.2'
11+
s.version = '0.1.3'
1212
s.summary = 'WKWebView wrapper for using in UITableView and UIScrollView'
1313

1414
# This description is used to generate tags and improve search results.

PSHTMLView/Classes/PSHTMLView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public class PSHTMLView: UIView {
103103
controller.addUserScript(PSHTMLViewScripts.viewportScript)
104104
controller.addUserScript(PSHTMLViewScripts.disableSelectionScript)
105105
controller.addUserScript(PSHTMLViewScripts.disableCalloutScript)
106+
controller.addUserScript(PSHTMLViewScripts.addToOnloadScript)
106107

107108
//add contentHeight script and handler
108109
controller.add(self, name: PSHTMLViewScriptMessage.HandlerName.onContentHeightChange.rawValue)
@@ -219,7 +220,8 @@ struct PSHTMLViewScripts {
219220
private static let viewportScriptString = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); meta.setAttribute('initial-scale', '1.0'); meta.setAttribute('maximum-scale', '1.0'); meta.setAttribute('minimum-scale', '1.0'); meta.setAttribute('user-scalable', 'no'); document.getElementsByTagName('head')[0].appendChild(meta);"
220221
private static let disableSelectionScriptString = "document.documentElement.style.webkitUserSelect='none';"
221222
private static let disableCalloutScriptString = "document.documentElement.style.webkitTouchCallout='none';"
222-
private static let heigthOnLoadScriptString = "window.onload= function () {window.webkit.messageHandlers.\(PSHTMLViewScriptMessage.HandlerName.onContentHeightChange.rawValue).postMessage({justLoaded:true,height: document.body.offsetHeight});};"
223+
private static let addToOnloadScriptString = "function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } addLoadEvent(nameOfSomeFunctionToRunOnPageLoad); addLoadEvent(function() { }); "
224+
private static let heigthOnLoadScriptString = "addLoadEvent( function () {window.webkit.messageHandlers.\(PSHTMLViewScriptMessage.HandlerName.onContentHeightChange.rawValue).postMessage({justLoaded:true,height: document.body.offsetHeight});};)"
223225
private static let heigthOnResizeScriptString = "document.body.addEventListener( 'resize', incrementCounter); function incrementCounter() {window.webkit.messageHandlers.\(PSHTMLViewScriptMessage.HandlerName.onContentHeightChange.rawValue).postMessage({height: document.body.offsetHeight});};"
224226

225227
static let getContentHeightScriptString = "document.body.offsetHeight"
@@ -229,6 +231,7 @@ struct PSHTMLViewScripts {
229231
static let viewportScript = WKUserScript(source: viewportScriptString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
230232
static let disableSelectionScript = WKUserScript(source: disableSelectionScriptString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
231233
static let disableCalloutScript = WKUserScript(source: disableCalloutScriptString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
234+
static let addToOnloadScript = WKUserScript(source: addToOnloadScriptString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
232235
static let heigthOnLoadScript = WKUserScript(source: heigthOnLoadScriptString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
233236
static let heigthOnResizeScript = WKUserScript(source: heigthOnResizeScriptString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
234237

0 commit comments

Comments
 (0)