Skip to content

Commit

Permalink
Added support for custom CSS & Javascript & More // Fixes #36 #53 and #…
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWDG committed Apr 14, 2016
1 parent 53d8047 commit 53f9714
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
4 changes: 4 additions & 0 deletions WebShell.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
6E6055711C5EA6DC0099AD18 /* WebshellViewDid.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E6055701C5EA6DC0099AD18 /* WebshellViewDid.swift */; };
6EA8D1571C5C0BCA004534F9 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EA8D1561C5C0BCA004534F9 /* StringExtension.swift */; };
6EA8D1591C5C1240004534F9 /* navigator_geolocation_getCurrentPosition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EA8D1581C5C1240004534F9 /* navigator_geolocation_getCurrentPosition.swift */; };
6EFEB6A21CC00FB1001CF349 /* WebShellCustomInject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EFEB6A11CC00FB1001CF349 /* WebShellCustomInject.swift */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -108,6 +110,7 @@
6E60556C1C5EA5D00099AD18 /* WebShellCore.swift */,
6E60556E1C5EA6810099AD18 /* WebViewFunctions.swift */,
6E6055701C5EA6DC0099AD18 /* WebshellViewDid.swift */,
6EFEB6A11CC00FB1001CF349 /* WebShellCustomInject.swift */,
);
name = "WebShell Core";
sourceTree = "<group>";
Expand Down Expand Up @@ -182,6 +185,7 @@
buildActionMask = 2147483647;
files = (
6E6055671C5EA2050099AD18 /* WebShellFileHandler.swift in Sources */,
6EFEB6A21CC00FB1001CF349 /* WebShellCustomInject.swift in Sources */,
6E6055621C5EA0870099AD18 /* Notifications.swift in Sources */,
6E60556D1C5EA5D00099AD18 /* WebShellCore.swift in Sources */,
6EA8D1571C5C0BCA004534F9 /* StringExtension.swift in Sources */,
Expand Down
8 changes: 7 additions & 1 deletion WebShell/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class ViewController: NSViewController, WebFrameLoadDelegate, WebUIDelegate, Web

// run the app in debug mode? (Default: false)
// will be overridden by xCode (runs with -NSDocumentRevisionsDebugMode YES)
"debugmode": false
"debugmode": false,

// Please paste here the JavaScript you want to load on a website
"JSInject": "alert('x!=y');",

// Please paste here the CSS you want to load on a website
"CSSInject": "body{background:orange !important;}"
]
}
49 changes: 49 additions & 0 deletions WebShell/WebShellCustomInject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// WebShellCustomInject.swift
// WebShell
//
// Created by Wesley de Groot on 14-04-16.
// Copyright © 2016 RandyLu. All rights reserved.
//

import Foundation
import WebKit

extension ViewController {
/**
_WSInjectJS
Injects JavaScript in to a frame, or other position
- Parameter jsContext: JSContext!
- Note: @wdg #36
*/
internal func _WSInjectJS(jsContext: JSContext!) {
// JSInject
if (SETTINGS["JSInject"] as! String != "") {
jsContext.evaluateScript(SETTINGS["JSInject"] as! String)
}
}

/**
_WSInjectCSS
Injects CSS in to a frame, or other position
- Parameter jsContext: JSContext!
- Note: @wdg #36
*/
internal func _WSInjectCSS(jsContext: JSContext!) {
// CSSInject
if (SETTINGS["CSSInject"] as! String != "") {
let css:String = (SETTINGS["CSSInject"] as! String)
.stringByReplacingOccurrencesOfString("\n", withString: "")
.stringByReplacingOccurrencesOfString("\r", withString: "")
.stringByReplacingOccurrencesOfString("'", withString: "\\'")

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);")
}
}
}
2 changes: 2 additions & 0 deletions WebShell/WebShellInjector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ extension ViewController {
let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]
jsContext.evaluateScript("window.webshell={version:'\(nsObject as! String)'};webshell=window.webshell;")

_WSInjectJS(jsContext)
_WSInjectCSS(jsContext)
}

// @wdg Add Localstorage Support
Expand Down
4 changes: 2 additions & 2 deletions WebShell/WebShellPageActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ extension ViewController {
mainWebview.mainFrame.loadRequest(NSURLRequest(URL: URL!))

// Inject Webhooks
self.injectWebhooks(mainWebview.mainFrame.javaScriptContext)
self.loopThroughiFrames()
// self.injectWebhooks(mainWebview.mainFrame.javaScriptContext)
// self.loopThroughiFrames()
}

// @wdg Add Print Support
Expand Down

0 comments on commit 53f9714

Please sign in to comment.