Skip to content

Commit 53f9714

Browse files
committed
Added support for custom CSS & Javascript & More // Fixes #36 #53 and #47
1 parent 53d8047 commit 53f9714

5 files changed

+64
-3
lines changed

WebShell.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
6E6055711C5EA6DC0099AD18 /* WebshellViewDid.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E6055701C5EA6DC0099AD18 /* WebshellViewDid.swift */; };
2323
6EA8D1571C5C0BCA004534F9 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EA8D1561C5C0BCA004534F9 /* StringExtension.swift */; };
2424
6EA8D1591C5C1240004534F9 /* navigator_geolocation_getCurrentPosition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EA8D1581C5C1240004534F9 /* navigator_geolocation_getCurrentPosition.swift */; };
25+
6EFEB6A21CC00FB1001CF349 /* WebShellCustomInject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EFEB6A11CC00FB1001CF349 /* WebShellCustomInject.swift */; };
2526
/* End PBXBuildFile section */
2627

2728
/* Begin PBXFileReference section */
@@ -43,6 +44,7 @@
4344
6E6055701C5EA6DC0099AD18 /* WebshellViewDid.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebshellViewDid.swift; sourceTree = "<group>"; };
4445
6EA8D1561C5C0BCA004534F9 /* StringExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = "<group>"; };
4546
6EA8D1581C5C1240004534F9 /* navigator_geolocation_getCurrentPosition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = navigator_geolocation_getCurrentPosition.swift; sourceTree = "<group>"; };
47+
6EFEB6A11CC00FB1001CF349 /* WebShellCustomInject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebShellCustomInject.swift; sourceTree = "<group>"; };
4648
/* End PBXFileReference section */
4749

4850
/* Begin PBXFrameworksBuildPhase section */
@@ -108,6 +110,7 @@
108110
6E60556C1C5EA5D00099AD18 /* WebShellCore.swift */,
109111
6E60556E1C5EA6810099AD18 /* WebViewFunctions.swift */,
110112
6E6055701C5EA6DC0099AD18 /* WebshellViewDid.swift */,
113+
6EFEB6A11CC00FB1001CF349 /* WebShellCustomInject.swift */,
111114
);
112115
name = "WebShell Core";
113116
sourceTree = "<group>";
@@ -182,6 +185,7 @@
182185
buildActionMask = 2147483647;
183186
files = (
184187
6E6055671C5EA2050099AD18 /* WebShellFileHandler.swift in Sources */,
188+
6EFEB6A21CC00FB1001CF349 /* WebShellCustomInject.swift in Sources */,
185189
6E6055621C5EA0870099AD18 /* Notifications.swift in Sources */,
186190
6E60556D1C5EA5D00099AD18 /* WebShellCore.swift in Sources */,
187191
6EA8D1571C5C0BCA004534F9 /* StringExtension.swift in Sources */,

WebShell/ViewController.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate, Web
6767

6868
// run the app in debug mode? (Default: false)
6969
// will be overridden by xCode (runs with -NSDocumentRevisionsDebugMode YES)
70-
"debugmode": false
70+
"debugmode": false,
71+
72+
// Please paste here the JavaScript you want to load on a website
73+
"JSInject": "alert('x!=y');",
74+
75+
// Please paste here the CSS you want to load on a website
76+
"CSSInject": "body{background:orange !important;}"
7177
]
7278
}

WebShell/WebShellCustomInject.swift

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// WebShellCustomInject.swift
3+
// WebShell
4+
//
5+
// Created by Wesley de Groot on 14-04-16.
6+
// Copyright © 2016 RandyLu. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import WebKit
11+
12+
extension ViewController {
13+
/**
14+
_WSInjectJS
15+
16+
Injects JavaScript in to a frame, or other position
17+
18+
- Parameter jsContext: JSContext!
19+
20+
- Note: @wdg #36
21+
*/
22+
internal func _WSInjectJS(jsContext: JSContext!) {
23+
// JSInject
24+
if (SETTINGS["JSInject"] as! String != "") {
25+
jsContext.evaluateScript(SETTINGS["JSInject"] as! String)
26+
}
27+
}
28+
29+
/**
30+
_WSInjectCSS
31+
32+
Injects CSS in to a frame, or other position
33+
34+
- Parameter jsContext: JSContext!
35+
36+
- Note: @wdg #36
37+
*/
38+
internal func _WSInjectCSS(jsContext: JSContext!) {
39+
// CSSInject
40+
if (SETTINGS["CSSInject"] as! String != "") {
41+
let css:String = (SETTINGS["CSSInject"] as! String)
42+
.stringByReplacingOccurrencesOfString("\n", withString: "")
43+
.stringByReplacingOccurrencesOfString("\r", withString: "")
44+
.stringByReplacingOccurrencesOfString("'", withString: "\\'")
45+
46+
jsContext.evaluateScript("var css='\(css)',head=document.head,style=document.createElement('style');style.type='text/css';if (style.styleSheet){style.styleSheet.cssText = css;}else{style.appendChild(document.createTextNode(css));}head.appendChild(style);")
47+
}
48+
}
49+
}

WebShell/WebShellInjector.swift

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ extension ViewController {
173173
let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]
174174
jsContext.evaluateScript("window.webshell={version:'\(nsObject as! String)'};webshell=window.webshell;")
175175

176+
_WSInjectJS(jsContext)
177+
_WSInjectCSS(jsContext)
176178
}
177179

178180
// @wdg Add Localstorage Support

WebShell/WebShellPageActions.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ extension ViewController {
111111
mainWebview.mainFrame.loadRequest(NSURLRequest(URL: URL!))
112112

113113
// Inject Webhooks
114-
self.injectWebhooks(mainWebview.mainFrame.javaScriptContext)
115-
self.loopThroughiFrames()
114+
// self.injectWebhooks(mainWebview.mainFrame.javaScriptContext)
115+
// self.loopThroughiFrames()
116116
}
117117

118118
// @wdg Add Print Support

0 commit comments

Comments
 (0)