Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
magic url redirects
Browse files Browse the repository at this point in the history
supports youtube and vimeo
  • Loading branch information
Jaden Geller committed Apr 13, 2015
1 parent bdd5b23 commit 2d824ce
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
9 changes: 8 additions & 1 deletion Helium/Helium/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var magicURLMenu: NSMenuItem!

func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application

magicURLMenu.state = NSUserDefaults.standardUserDefaults().boolForKey("disabledMagicURLs") ? NSOffState : NSOnState

}

func applicationWillTerminate(aNotification: NSNotification) {
Expand All @@ -24,6 +27,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
NSNotificationCenter.defaultCenter().postNotificationName("HeliumReload", object: nil)

}
@IBAction func magicURLRedirectToggled(sender: NSMenuItem) {
sender.state = (sender.state == NSOnState) ? NSOffState : NSOnState
NSUserDefaults.standardUserDefaults().setBool((sender.state == NSOffState), forKey: "disabledMagicURLs")
}

@IBAction func clearPress(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("HeliumClear", object: nil)
Expand Down
4 changes: 4 additions & 0 deletions Helium/Helium/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<items>
<menuItem title="Magic URL Redirects" state="on" id="Vn0-wi-SSU">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="magicURLRedirectToggled:" target="Voe-Tx-rLC" id="OyL-id-RDj"/>
</connections>
</menuItem>
</items>
</menu>
Expand Down Expand Up @@ -226,6 +229,7 @@ CA
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Helium" customModuleProvider="target">
<connections>
<outlet property="allTranslucencyValues" destination="5ao-Bb-P10" id="NDg-n5-yLb"/>
<outlet property="magicURLMenu" destination="Vn0-wi-SSU" id="jow-sG-Fmx"/>
</connections>
</customObject>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
Expand Down
46 changes: 43 additions & 3 deletions Helium/Helium/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Cocoa
import WebKit

class WebViewController: NSViewController {
class WebViewController: NSViewController, WKNavigationDelegate {

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -18,15 +18,20 @@ class WebViewController: NSViewController {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "requestedReload", name: "HeliumReload", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "clear", name: "HeliumClear", object: nil)

webView.configuration.preferences.plugInsEnabled = true

// Layout webview
view.addSubview(webView)
webView.frame = view.bounds
webView.autoresizingMask = NSAutoresizingMaskOptions.ViewHeightSizable | NSAutoresizingMaskOptions.ViewWidthSizable

// Allow plug-ins such as silverlight
webView.configuration.preferences.plugInsEnabled = true

// Netflix support via Silverlight (HTML5 Netflix doesn't work for some unknown reason)
webView._customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/600.5.17 (KHTML, like Gecko) Version/7.1.5 Safari/537.85.14"

// Setup magic URLs
webView.navigationDelegate = self

clear()
}

Expand All @@ -49,5 +54,40 @@ class WebViewController: NSViewController {

var webView = WKWebView()

var shouldRedirect: Bool {
get {
return !NSUserDefaults.standardUserDefaults().boolForKey("disabledMagicURLs")
}
}

// Redirect Hulu and YouTube to pop-out videos
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {

if shouldRedirect, let url = navigationAction.request.URL, let urlString = url.absoluteString {
var modified = urlString
modified = modified.replacePrefix("https://www.youtube.com/watch?", replacement: "https://www.youtube.com/watch_popup?")
modified = modified.replacePrefix("https://vimeo.com/", replacement: "http://player.vimeo.com/video/")

if urlString != modified {
decisionHandler(WKNavigationActionPolicy.Cancel)
loadURL(NSURL(string: modified)!)
return

}
}

decisionHandler(WKNavigationActionPolicy.Allow)
}
}

extension String {
func replacePrefix(prefix: String, replacement: String) -> String {
if hasPrefix(prefix) {
return replacement + substringFromIndex(prefix.endIndex)
}
else {
return self
}

}
}

0 comments on commit 2d824ce

Please sign in to comment.